Skip to content

Commit

Permalink
test: increase coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Dec 4, 2020
1 parent 8fce1c8 commit 63a9a67
Showing 1 changed file with 141 additions and 8 deletions.
149 changes: 141 additions & 8 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,88 @@ func TestNewJsonResponse(t *testing.T) {
if checkBody.Hello != body.Hello {
t.FailNow()
}

// Error case
response, err = NewJsonResponse(200, func() {})
if response != nil {
t.Fatal("response is not nil")
}
if err == nil {
t.Fatal("no error occurred")
}
}

func TestNewXmlResponse(t *testing.T) {
type schema struct {
Hello string `xml:"hello"`
func checkResponder(t *testing.T, r Responder, expectedStatus int, expectedBody string) {
helper(t).Helper()

req, _ := http.NewRequest(http.MethodGet, "/foo", nil)
resp, err := r(req)
if err != nil {
t.Errorf("An error occurred: %s", err)
return
}

body := &schema{"world"}
if resp == nil {
t.Error("Responder returned a nil response")
return
}

if resp.StatusCode != expectedStatus {
t.Errorf("Status code mismatch: got=%d expected=%d",
resp.StatusCode, expectedStatus)
}

assertBody(t, resp, expectedBody)
}

func TestNewJsonResponder(t *testing.T) {
t.Run("OK", func(t *testing.T) {
r, err := NewJsonResponder(200, map[string]int{"foo": 42})
if err != nil {
t.Error(err)
return
}
checkResponder(t, r, 200, `{"foo":42}`)
})

t.Run("Error", func(t *testing.T) {
r, err := NewJsonResponder(200, func() {})
if r != nil {
t.Error("responder is not nil")
}
if err == nil {
t.Error("no error occurred")
}
})

t.Run("OK don't panic", func(t *testing.T) {
panicked, str := catchPanic(
func() {
r := NewJsonResponderOrPanic(200, map[string]int{"foo": 42})
checkResponder(t, r, 200, `{"foo":42}`)
},
)
if panicked {
t.Errorf("A panic occurred: <%s>", str)
}
})

t.Run("Panic", func(t *testing.T) {
panicked, _ := catchPanic(
func() { NewJsonResponderOrPanic(200, func() {}) },
)
if !panicked {
t.Error("no panic occurred")
}
})
}

type schemaXML struct {
Hello string `xml:"hello"`
}

func TestNewXmlResponse(t *testing.T) {
body := &schemaXML{"world"}
status := 200

response, err := NewXmlResponse(status, body)
Expand All @@ -187,14 +261,73 @@ func TestNewXmlResponse(t *testing.T) {
t.FailNow()
}

checkBody := &schema{}
checkBody := &schemaXML{}
if err := xml.NewDecoder(response.Body).Decode(checkBody); err != nil {
t.Fatal(err)
}

if checkBody.Hello != body.Hello {
t.FailNow()
}

// Error case
response, err = NewXmlResponse(200, func() {})
if response != nil {
t.Fatal("response is not nil")
}
if err == nil {
t.Fatal("no error occurred")
}
}

func TestNewXmlResponder(t *testing.T) {
body := &schemaXML{"world"}

b, err := xml.Marshal(body)
if err != nil {
t.Fatalf("Cannot xml.Marshal expected body: %s", err)
}
expectedBody := string(b)

t.Run("OK", func(t *testing.T) {
r, err := NewXmlResponder(200, body)
if err != nil {
t.Error(err)
return
}
checkResponder(t, r, 200, expectedBody)
})

t.Run("Error", func(t *testing.T) {
r, err := NewXmlResponder(200, func() {})
if r != nil {
t.Error("responder is not nil")
}
if err == nil {
t.Error("no error occurred")
}
})

t.Run("OK don't panic", func(t *testing.T) {
panicked, str := catchPanic(
func() {
r := NewXmlResponderOrPanic(200, body)
checkResponder(t, r, 200, expectedBody)
},
)
if panicked {
t.Errorf("A panic occurred: <%s>", str)
}
})

t.Run("Panic", func(t *testing.T) {
panicked, _ := catchPanic(
func() { NewXmlResponderOrPanic(200, func() {}) },
)
if !panicked {
t.Error("no panic occurred")
}
})
}

func TestNewErrorResponder(t *testing.T) {
Expand Down Expand Up @@ -259,7 +392,7 @@ func TestResponder(t *testing.T) {
resp := &http.Response{}

chk := func(r Responder, expectedResp *http.Response, expectedErr string) {
//t.Helper // Only available since 1.9
helper(t).Helper()
gotResp, gotErr := r(req)
if gotResp != expectedResp {
t.Errorf(`Response mismatch, expected: %v, got: %v`, expectedResp, gotResp)
Expand All @@ -275,14 +408,14 @@ func TestResponder(t *testing.T) {
called := false
chkNotCalled := func() {
if called {
//t.Helper // Only available since 1.9
helper(t).Helper()
t.Errorf("Original responder should not be called")
called = false
}
}
chkCalled := func() {
if !called {
//t.Helper // Only available since 1.9
helper(t).Helper()
t.Errorf("Original responder should be called")
}
called = false
Expand Down

0 comments on commit 63a9a67

Please sign in to comment.