-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
121 lines (107 loc) · 2.69 KB
/
http_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package testttp_test
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strings"
"testing"
"github.com/jsteenb2/testttp"
)
func TestHTTP(t *testing.T) {
svr := newMux()
t.Run("Get", func(t *testing.T) {
testttp.Get("/").
Do(svr).
ExpectStatus(t, http.StatusOK).
ExpectBody(assertBody(t, http.MethodGet))
})
t.Run("Post", func(t *testing.T) {
testttp.Post("/", nil).Do(svr).
ExpectStatus(t, http.StatusCreated).
ExpectBody(assertBody(t, http.MethodPost))
})
t.Run("Put", func(t *testing.T) {
testttp.Put("/", nil).
Do(svr).
ExpectStatus(t, http.StatusAccepted).
ExpectBody(assertBody(t, http.MethodPut))
})
t.Run("Patch", func(t *testing.T) {
testttp.Patch("/", nil).
Do(svr).
ExpectStatus(t, http.StatusPartialContent).
ExpectBody(assertBody(t, http.MethodPatch))
})
t.Run("Delete", func(t *testing.T) {
testttp.Delete("/").
Do(svr).
ExpectStatus(t, http.StatusNoContent)
})
t.Run("Headers", func(t *testing.T) {
testttp.Post("/", strings.NewReader(`a: foo`)).
Headers("Content-Type", "text/yml").
Do(svr).
Expect(func(resp *testttp.Resp) {
equals(t, "text/yml", resp.Req.Header.Get("Content-Type"))
})
})
}
type foo struct {
Name, Thing, Method string
}
func newMux() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
writeFn(w, req.Method, http.StatusOK)
case http.MethodPost:
writeFn(w, req.Method, http.StatusCreated)
case http.MethodPut:
writeFn(w, req.Method, http.StatusAccepted)
case http.MethodPatch:
writeFn(w, req.Method, http.StatusPartialContent)
case http.MethodDelete:
w.WriteHeader(http.StatusNoContent)
}
})
return mux
}
func assertBody(t *testing.T, method string) func(*bytes.Buffer) {
return func(buf *bytes.Buffer) {
var f foo
if err := json.NewDecoder(buf).Decode(&f); err != nil {
t.Fatal(err)
}
expected := foo{Name: "name", Thing: "thing", Method: method}
equals(t, expected, f)
}
}
func writeFn(w http.ResponseWriter, method string, statusCode int) {
f := foo{Name: "name", Thing: "thing", Method: method}
r, err := encodeBuf(f)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(statusCode)
if _, err := io.Copy(w, r); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func equals(t *testing.T, expected, actual interface{}) {
t.Helper()
if expected == actual {
return
}
t.Errorf("expected: %v\tactual: %v", expected, actual)
}
func encodeBuf(v interface{}) (io.Reader, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(v); err != nil {
return nil, err
}
return &buf, nil
}