-
Notifications
You must be signed in to change notification settings - Fork 0
/
tea_test.go
71 lines (56 loc) · 1.44 KB
/
tea_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
package tea
import (
"net/http"
"net/http/httptest"
"reflect"
"sync/atomic"
"testing"
)
func TestHandlerWritesStatusCode(t *testing.T) {
expected := http.StatusBadRequest
h := Handler(func(http.ResponseWriter, *http.Request) (int, interface{}) {
return expected, nil
})
w := httptest.NewRecorder()
h.ServeHTTP(w, nil)
w.Flush()
if expected != w.Code {
t.Fatalf("expected %d, got %d", expected, w.Code)
}
}
func TestHandlerWritesWithResponder(t *testing.T) {
const yes = 1
var called int32
body := struct{ Name string }{"Brett"}
Responder = func(w http.ResponseWriter, r *http.Request, v interface{}) {
atomic.StoreInt32(&called, yes)
if !reflect.DeepEqual(v, body) {
t.Fatalf("expected body to be %+v, but was %+v", body, v)
}
}
h := Handler(func(http.ResponseWriter, *http.Request) (int, interface{}) {
return 200, body
})
w := httptest.NewRecorder()
h.ServeHTTP(w, nil)
w.Flush()
if atomic.LoadInt32(&called) != yes {
t.Fatalf("expected Responder to be called")
}
}
func TestHandlerDoesNotWriteIfBodyIsNil(t *testing.T) {
const yes = 1
var called int32
Responder = func(w http.ResponseWriter, r *http.Request, v interface{}) {
atomic.StoreInt32(&called, yes)
}
h := Handler(func(http.ResponseWriter, *http.Request) (int, interface{}) {
return 200, nil
})
w := httptest.NewRecorder()
h.ServeHTTP(w, nil)
w.Flush()
if atomic.LoadInt32(&called) == yes {
t.Fatalf("expected Responder to NOT be called")
}
}