Пример #1
0
func TestNotFound(t *testing.T) {

	req, err := client.BuildRequest("GET", "/nonexistentpage", "")
	if err != nil {
		t.Fatalf("Error building not found request")
	}

	c := &http.Client{}

	resp, err := c.Do(req)
	if err != nil {
		t.Fatalf("Error sending not found request")
	}

	if resp.StatusCode != 404 {
		t.Fatalf("Incorrect status code from not found request")
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("Could not read not found response body")
	}

	if string(body) != "404 Not Found" {
		t.Fatalf("Not found body not set correctly")
	}

}
func TestBuildRequestNoData(t *testing.T) {

	req, err := client.BuildRequest("DELETE", "/funtime", "")
	if err != nil {
		t.Fatalf("Should not error")
	}

	//should be not set
	if req.Header.Get("Content-Type") != "" {
		t.Fatalf("Content type should not be set for dataless request")
	}

	if req.Method != "DELETE" {
		t.Fatalf("Request method not set properly")
	}

	if req.URL.Path != "/funtime" {
		t.Fatalf("Request url/uri not set properly")
	}

	//not equality because req.URL.Host drops the http/https
	if !strings.Contains(DefaultAPIAddr, req.URL.Host) {
		t.Fatalf("Request url not set properly %s : %s", req.URL.Host, DefaultAPIAddr)
	}
}
func TestBuildRequestWithData(t *testing.T) {

	req, err := client.BuildRequest("GET", "/test", "non-empty")
	if err != nil {
		t.Fatalf("should not error making basic call")
	}

	if req.Header.Get("Content-Type") != "application/json" {
		t.Fatalf("Content-Type header not set properly")
	}

	if req.Header.Get("User") != DefaultUser {
		t.Fatalf("User header not set properly")
	}

	if req.Header.Get("Secret") != DefaultSecret {
		t.Fatalf("Secret header not set properly")
	}

	if req.URL.Path != "/test" {
		t.Fatalf("Url/uri not built properly")
	}

	//not equality because req.URL.Host drops http/https
	if !strings.Contains(DefaultAPIAddr, req.URL.Host) {
		t.Fatalf("Url not set properly")
	}

	if req.Method != "GET" {
		t.Fatalf("Request method not set properly")
	}
}
Пример #4
0
func TestHealthz(t *testing.T) {

	req, err := client.BuildRequest("GET", "/healthz", "")
	if err != nil {
		t.Fatalf("Error making healthz request")
	}

	c := &http.Client{}

	resp, err := c.Do(req)
	if err != nil {
		t.Fatalf("Error sending healthz request %s\n", err)
	}

	if resp.StatusCode != 200 {
		t.Fatalf("Incorrect status code from healthz")
	}

	if resp.Header.Get("Server-Status") != "OK" {
		t.Fatalf("Healthz not returning ok server status: %s", resp.Header.Get("Server-Status"))
	}

}