forked from naoina/kocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_test.go
93 lines (86 loc) · 2.19 KB
/
result_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
package kocha
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func Test_resultContentProc(t *testing.T) {
buf := bytes.NewBufferString("foobar")
result := &resultContent{Body: buf}
w := httptest.NewRecorder()
res := newResponse(w)
result.Proc(res)
var actual interface{} = w.Body.String()
var expected interface{} = "foobar"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
closer := &testCloser{bytes.NewBufferString("brown fox"), false}
result = &resultContent{Body: closer}
w = httptest.NewRecorder()
res = newResponse(w)
result.Proc(res)
actual = w.Body.String()
expected = "brown fox"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
actual = closer.Closed
expected = true
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
}
func Test_resultRedirectProc(t *testing.T) {
req, err := http.NewRequest("GET", "/path/to/request", nil)
if err != nil {
panic(err)
}
result := &resultRedirect{
Request: newRequest(req),
URL: "/path/to/redirect",
Permanently: false,
}
w := httptest.NewRecorder()
res := newResponse(w)
result.Proc(res)
var actual interface{} = w.Code
var expected interface{} = http.StatusFound
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
actual = w.Header().Get("Location")
expected = "/path/to/redirect"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
result = &resultRedirect{
Request: newRequest(req),
URL: "/path/to/redirect/permanently",
Permanently: true,
}
w = httptest.NewRecorder()
res = newResponse(w)
result.Proc(res)
actual = w.Code
expected = http.StatusMovedPermanently
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
actual = w.Header().Get("Location")
expected = "/path/to/redirect/permanently"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
}
type testCloser struct {
io.Reader
Closed bool
}
func (c *testCloser) Close() error {
c.Closed = true
return nil
}