Exemple #1
0
// /services: list name
func TestServicesList(t *testing.T) {
	cases := []struct {
		expected int // expected result
	}{
		{http.StatusOK},
	}

	url := serverURL + amalgam8.ServiceNamesURL()
	c := defaultServerConfig()
	c.Registry.(*mockCatalog).prepopulateServices(services)
	handler, err := setupServer(c)
	assert.Nil(t, err)

	for _, tc := range cases {
		recorder := httptest.NewRecorder()
		req, err := http.NewRequest("GET", url, nil)
		assert.Nil(t, err)
		req.Header.Set("Content-Type", "application/json")
		handler.ServeHTTP(recorder, req)
		assert.Equal(t, tc.expected, recorder.Code)
		if tc.expected == http.StatusOK {
			list := amalgam8.ServicesList{}

			err = json.Unmarshal(recorder.Body.Bytes(), &list)
			assert.NoError(t, err)
			assert.NotNil(t, list.Services)
			assert.Equal(t, len(services), len(list.Services))
		}
	}
}
Exemple #2
0
//---------------
// secure access
//---------------
func TestRequireHTTPS(t *testing.T) {
	url := serverURL + amalgam8.ServiceNamesURL()
	c := defaultServerConfig()
	handler, err := setupServer(c)
	assert.Nil(t, err)

	recorder := httptest.NewRecorder()
	req, err := http.NewRequest("GET", url, nil)
	assert.Nil(t, err)
	req.Header.Set("Content-Type", "application/json")
	handler.ServeHTTP(recorder, req)
	assert.NotEqual(t, recorder.Code, http.StatusMovedPermanently)

	// Now, set up a a new handler, which requires HTTPS
	c.RequireHTTPS = true
	secureHandler, err := setupServer(c)
	assert.Nil(t, err)

	recorder = httptest.NewRecorder()
	secureHandler.ServeHTTP(recorder, req)
	assert.Equal(t, recorder.Code, http.StatusMovedPermanently)

	req.Header.Set("X-Forwarded-Proto", "https")
	recorder = httptest.NewRecorder()
	secureHandler.ServeHTTP(recorder, req)
	assert.NotEqual(t, recorder.Code, http.StatusMovedPermanently)
}
Exemple #3
0
func (client *client) ListServices() ([]string, error) {
	body, err := client.doRequest("GET", amalgam8.ServiceNamesURL(), nil, http.StatusOK)
	if err != nil {
		return nil, err
	}

	s := struct {
		Services []string `json:"services"`
	}{}
	err = json.Unmarshal(body, &s)
	if err != nil {
		return nil, newError(ErrorCodeInternalClientError, "error unmarshaling HTTP response body", err, "")
	}
	return s.Services, nil
}
Exemple #4
0
// services/<name>:methods
func TestServicesListMethods(t *testing.T) {
	var methods = []string{"CONNECT", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "TRACE"}

	url := serverURL + amalgam8.ServiceNamesURL()
	c := defaultServerConfig()
	handler, err := setupServer(c)
	assert.Nil(t, err)

	for _, method := range methods {
		recorder := httptest.NewRecorder()
		req, err := http.NewRequest(method, url, nil)
		assert.Nil(t, err)
		handler.ServeHTTP(recorder, req)
		assert.Equal(t, http.StatusMethodNotAllowed, recorder.Code, method+":"+url)
	}
}
Exemple #5
0
func list(t *testing.T, port uint16) int {
	url := fmt.Sprintf("http://localhost:%d%s", port, amalgam8.ServiceNamesURL())
	req, err1 := http.NewRequest("GET", url, nil)
	assert.NoError(t, err1)
	assert.NotNil(t, req)

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-Forwarded-Proto", "https")

	res, err2 := http.DefaultClient.Do(req)
	assert.NoError(t, err2)
	assert.NotNil(t, res)
	assert.EqualValues(t, 200, res.StatusCode)

	data, err3 := ioutil.ReadAll(res.Body)
	assert.NoError(t, err3)
	res.Body.Close()

	var slist amalgam8.ServicesList
	err1 = json.Unmarshal(data, &slist)
	assert.NoError(t, err1)
	return len(slist.Services)
}