-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_internal_test.go
84 lines (78 loc) · 1.63 KB
/
context_internal_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
package tcp
import (
"io"
"strconv"
"testing"
"github.com/matryer/is"
)
const (
eol = "\n"
msg = "hello world"
msgWithEol = msg + eol
)
func TestContext_Write(t *testing.T) {
var (
dt = []struct {
w *ResponseRecorder
in []byte
out string
size int
err error
}{
{err: io.EOF},
{w: NewRecorder()},
{w: NewRecorder(), in: []byte(msg), size: len(msg), out: msg},
{w: NewRecorder(), in: []byte(msgWithEol), size: len(msgWithEol), out: msgWithEol},
}
are = is.New(t)
)
for i, tt := range dt {
tt := tt
t.Run("#"+strconv.Itoa(i), func(t *testing.T) {
c := newContextWithWriter(tt.w)
size, err := c.Write(tt.in)
are.Equal(err, tt.err) // mismatch error
are.Equal(size, tt.size) // mismatch size
if tt.w != nil {
are.Equal(tt.w.Body.String(), tt.out) // mismatch result
}
})
}
}
func TestContext_String(t *testing.T) {
var (
dt = []struct {
w *ResponseRecorder
in,
out,
err string
}{
{err: io.EOF.Error()},
{w: NewRecorder(), out: eol},
{w: NewRecorder(), in: msg, out: msgWithEol},
{w: NewRecorder(), in: msgWithEol, out: msgWithEol},
}
are = is.New(t)
)
for i, tt := range dt {
tt := tt
t.Run("#"+strconv.Itoa(i), func(t *testing.T) {
c := newContextWithWriter(tt.w)
c.String(tt.in)
are.Equal(c.Err().Error(), tt.err) // mismatch error
if tt.w != nil {
are.Equal(tt.w.Body.String(), tt.out) // mismatch result
}
})
}
}
func newContextWithWriter(w io.WriteCloser) *Context {
c := &Context{
Request: NewRequest(ACK, nil),
writer: responseWriter{ResponseWriter: w},
}
if w != nil {
c.reset()
}
return c
}