-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsuccess_test.go
105 lines (81 loc) · 2.16 KB
/
success_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package respond
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestOk(t *testing.T) {
t.Parallel()
req := newRequest(t, "GET")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
NewResponse(w).
Ok(nil)
})
handler.ServeHTTP(rr, req)
if err := validateStatusCode(rr.Code, http.StatusOK); err != nil {
t.Fatal(err.Error())
}
if err := validateResponseBody(rr.Body.String(), ""); err != nil {
t.Fatal(err.Error())
}
}
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
func TestCreated(t *testing.T) {
t.Parallel()
req := newRequest(t, "POST")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
users := []User{
{1, "Billy", "billy@example.com"},
{2, "Joan", "joan@example.com"},
}
NewResponse(w).
Created(users)
})
handler.ServeHTTP(rr, req)
if err := validateStatusCode(rr.Code, http.StatusCreated); err != nil {
t.Fatal(err.Error())
}
expected := `[{"id":1,"name":"Billy","email":"billy@example.com"},` +
`{"id":2,"name":"Joan","email":"joan@example.com"}]`
if err := validateResponseBody(rr.Body.String(), expected); err != nil {
t.Fatal(err.Error())
}
}
func TestAccepted(t *testing.T) {
t.Parallel()
req := newRequest(t, "POST")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := NewResponse(w)
res.Accepted(nil)
})
handler.ServeHTTP(rr, req)
if err := validateStatusCode(rr.Code, http.StatusAccepted); err != nil {
t.Fatal(err.Error())
}
if err := validateResponseBody(rr.Body.String(), ""); err != nil {
t.Fatal(err.Error())
}
}
func TestNoContent(t *testing.T) {
t.Parallel()
req := newRequest(t, "POST")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := NewResponse(w)
res.NoContent()
})
handler.ServeHTTP(rr, req)
if err := validateStatusCode(rr.Code, http.StatusNoContent); err != nil {
t.Fatal(err.Error())
}
if err := validateResponseBody(rr.Body.String(), ""); err != nil {
t.Fatal(err.Error())
}
}