Skip to content

Commit

Permalink
Merge pull request prometheus#870 from prometheus/fabxc/ctype
Browse files Browse the repository at this point in the history
api/v1: fix response format tests
  • Loading branch information
fabxc committed Jul 2, 2015
2 parents 78f4da8 + 8f904d6 commit 8510076
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions web/api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -321,20 +322,31 @@ func TestEndpoints(t *testing.T) {
}

func TestRespondSuccess(t *testing.T) {
w := httptest.NewRecorder()
respond(w, "test")
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
respond(w, "test")
}))
defer s.Close()

if w.Code != 200 {
t.Fatalf("Return code %d expected in success response but got %d", 200, w.Code)
resp, err := http.Get(s.URL)
if err != nil {
t.Fatalf("Error on test request: %s", err)
}
var res response
err := json.Unmarshal([]byte(w.Body.String()), &res)
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
t.Fatal(err)
t.Fatalf("Error reading response body: %s", err)
}

if h := w.Header().Get("Content-Type"); h != "application/json" {
t.Fatalf("expected Content-Type %q but got %q", "application/json", h)
if resp.StatusCode != 200 {
t.Fatalf("Return code %d expected in success response but got %d", 200, resp.StatusCode)
}
if h := resp.Header.Get("Content-Type"); h != "application/json" {
t.Fatalf("Expected Content-Type %q but got %q", "application/json", h)
}

var res response
if err = json.Unmarshal([]byte(body), &res); err != nil {
t.Fatalf("Error unmarshaling JSON body: %s", err)
}

exp := &response{
Expand All @@ -347,20 +359,31 @@ func TestRespondSuccess(t *testing.T) {
}

func TestRespondError(t *testing.T) {
w := httptest.NewRecorder()
respondError(w, &apiError{errorTimeout, errors.New("message")}, "test")
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
respondError(w, &apiError{errorTimeout, errors.New("message")}, "test")
}))
defer s.Close()

if w.Code != 422 {
t.Fatalf("Return code %d expected in success response but got %d", 422, w.Code)
resp, err := http.Get(s.URL)
if err != nil {
t.Fatalf("Error on test request: %s", err)
}
var res response
err := json.Unmarshal([]byte(w.Body.String()), &res)
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
t.Fatal(err)
t.Fatalf("Error reading response body: %s", err)
}

if h := w.Header().Get("Content-Type"); h != "application/json" {
t.Fatalf("expected Content-Type %q but got %q", "application/json", h)
if resp.StatusCode != 422 {
t.Fatalf("Return code %d expected in error response but got %d", 422, resp.StatusCode)
}
if h := resp.Header.Get("Content-Type"); h != "application/json" {
t.Fatalf("Expected Content-Type %q but got %q", "application/json", h)
}

var res response
if err = json.Unmarshal([]byte(body), &res); err != nil {
t.Fatalf("Error unmarshaling JSON body: %s", err)
}

exp := &response{
Expand Down

0 comments on commit 8510076

Please sign in to comment.