-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathresponse_test.go
85 lines (64 loc) · 1.67 KB
/
response_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
package gig
import (
"testing"
"github.com/matryer/is"
)
func TestResponse(t *testing.T) {
conn := &FakeConn{}
res := &Response{Writer: conn}
is := is.New(t)
_, err := res.Write([]byte("test"))
is.NoErr(err)
is.Equal("20 text/gemini\r\ntest", conn.Written)
}
func TestResponse_Write_FallsBackToDefaultStatus(t *testing.T) {
conn := &FakeConn{}
res := &Response{Writer: conn}
is := is.New(t)
_, err := res.Write([]byte("test"))
is.NoErr(err)
is.Equal("20 text/gemini\r\ntest", conn.Written)
}
func TestResponse_Write_Fails(t *testing.T) {
conn := &FakeConn{FailAfter: 1}
res := &Response{Writer: conn}
is := is.New(t)
_, err := res.Write([]byte("test"))
is.True(err != nil)
is.Equal("2", conn.Written)
}
func TestResponse_Double_WriteHeader(t *testing.T) {
conn := &FakeConn{}
res := &Response{Writer: conn}
is := is.New(t)
is.NoErr(res.WriteHeader(StatusSuccess, "text/gemini"))
is.NoErr(res.WriteHeader(StatusGone, "oops"))
is.Equal("20 text/gemini\r\n", conn.Written)
}
func TestResponse_Write_UsesSetResponseCode(t *testing.T) {
conn := &FakeConn{}
res := &Response{Writer: conn}
is := is.New(t)
res.Status = StatusCGIError
_, err := res.Write([]byte("test"))
is.NoErr(err)
is.Equal("42 text/gemini\r\ntest", conn.Written)
}
func TestResponse_Write_FailIfFailedBefore(t *testing.T) {
conn := &FakeConn{
FailAfter: 4,
}
res := &Response{Writer: conn}
is := is.New(t)
n, err := res.Write([]byte("test"))
is.True(err != nil)
is.Equal(n, 0)
is.Equal(res.err, err)
is.Equal("20 t", conn.Written)
_, err2 := res.Write([]byte("test"))
is.True(err2 != nil)
is.Equal(n, 0)
is.Equal(res.err, err)
is.Equal(err, err2)
is.Equal("20 t", conn.Written)
}