Beispiel #1
0
func TestDelete(t *testing.T) {
	assert := assert.New(t)

	status := "ok"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Header.Get("Accept") != "application/json" {
			http.Error(w, "accept missing", http.StatusBadRequest)
			return
		}

		if r.Header.Get("Content-Type") != "" {
			http.Error(w, "content-type should not be present", http.StatusBadRequest)
			return
		}
		if r.Method != "DELETE" {
			http.Error(w, "no", http.StatusMethodNotAllowed)
			return
		}

		fmt.Fprintf(w, `{"status": "%s"}`, status)
	}))
	defer ts.Close()

	client := api.New(mockConfig())
	client.Token = api.Token{Token: "valid"}

	resp := &Response{}
	err := client.Delete(ts.URL, resp)

	assert.Nil(err)
	assert.Equal(status, resp.Status)
}
Beispiel #2
0
func TestAuthAlaias(t *testing.T) {
	assert := assert.New(t)

	actualUser := &api.User{}
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.Error(w, "no", http.StatusMethodNotAllowed)
			return
		}

		json.NewDecoder(r.Body).Decode(actualUser)

		fmt.Fprintf(w, `{"userName":"******","accountAlias":"ALIAS","locationAlias":"DC1","roles":["AccountAdmin","ServerAdmin"],"bearerToken":"[LONG TOKEN VALUE]"}`)
	}))
	defer ts.Close()

	config := genConfig(ts)
	// override alias should be preserved regardless of auth response
	config.Alias = "ABCD"
	client := api.New(config)
	err := client.Auth()

	assert.Nil(err)
	assert.Equal("ABCD", client.Config().Alias)
}
Beispiel #3
0
func TestNewClient(t *testing.T) {
	assert := assert.New(t)

	config := api.Config{
		User: api.User{Username: "******", Password: "******"},
	}
	client := api.New(config)

	assert.NotNil(client)
	assert.Equal(config.User, client.Config().User)
}
Beispiel #4
0
func TestAuth_SerializationErr(t *testing.T) {
	assert := assert.New(t)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "nope", http.StatusInternalServerError)
	}))
	defer ts.Close()

	config := genConfig(ts)
	client := api.New(config)
	err := client.Auth()

	assert.NotNil(err)
}
Beispiel #5
0
func New(config api.Config) *Client {
	c := &Client{
		client: api.New(config),
	}

	c.Server = server.New(c.client)
	c.Status = status.New(c.client)
	c.AA = aa.New(c.client)
	c.Alert = alert.New(c.client)
	c.LB = lb.New(c.client)
	c.Group = group.New(c.client)
	c.DC = dc.New(c.client)

	return c
}
Beispiel #6
0
func TestDoWithAuth(t *testing.T) {
	assert := assert.New(t)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" && strings.HasSuffix(r.URL.RequestURI(), "login") {
			fmt.Fprintf(w, `{"userName":"******","accountAlias":"ALIAS","locationAlias":"DC1","roles":["AccountAdmin","ServerAdmin"],"bearerToken":"[LONG TOKEN VALUE]"}`)
		}
	}))
	defer ts.Close()

	config := genConfig(ts)
	client := api.New(config)
	err := client.DoWithAuth("GET", ts.URL, nil, nil)

	assert.Nil(err)
}
Beispiel #7
0
func TestPatch(t *testing.T) {
	assert := assert.New(t)

	status := "ok"
	actualReq := &Request{}
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Header.Get("Accept") != "application/json" {
			http.Error(w, "accept missing", http.StatusBadRequest)
			return
		}

		if r.Header.Get("Content-Type") != "application/json" {
			http.Error(w, "content-type missing", http.StatusBadRequest)
			return
		}

		if r.Header.Get("User-Agent") != "sdk-client" {
			http.Error(w, "user-agent mismatch", http.StatusBadRequest)
			return
		}

		if r.Method != "PATCH" {
			http.Error(w, "no", http.StatusMethodNotAllowed)
			return
		}

		json.NewDecoder(r.Body).Decode(actualReq)

		fmt.Fprintf(w, `{"status": "%s"}`, status)
	}))
	defer ts.Close()

	client := api.New(mockConfig())
	client.Token = api.Token{Token: "valid"}

	req := &Request{Status: "do stuff"}
	resp := &Response{}
	err := client.Patch(ts.URL, req, resp)

	assert.Nil(err)
	assert.Equal(req, actualReq)
	assert.Equal(status, resp.Status)
}