-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext_test.go
227 lines (197 loc) · 5.16 KB
/
context_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package xray
import (
"bytes"
"errors"
"github.com/stretchr/testify/assert"
"github.com/xray-family/xray/codec"
"io"
"net/http"
"strings"
"testing"
)
func newContextMocker(options ...Option) *Context {
var router = New(options...)
var request = &Request{
Header: HttpHeader{Header: http.Header{}},
Body: io.NopCloser(bytes.NewBuffer(nil)),
}
var writer = newResponseWriterMocker()
var ctx = NewContext(router, request, writer)
return ctx
}
func newResponseWriterMocker() *responseWriterMocker {
return &responseWriterMocker{
protocol: ProtocolHTTP,
statusCode: 0,
header: HttpHeader{Header: http.Header{}},
buf: bytes.NewBuffer(nil),
}
}
type responseWriterMocker struct {
protocol string
statusCode int
header HttpHeader
buf *bytes.Buffer
}
func (c *responseWriterMocker) SetProtocol(p string) {
c.protocol = p
}
func (c *responseWriterMocker) Protocol() string {
return c.protocol
}
func (c *responseWriterMocker) Header() Header {
return c.header
}
func (c *responseWriterMocker) Write(p []byte) (int, error) {
if strings.TrimSpace(string(p)) == `"err"` {
return 0, errors.New("test error")
}
return c.buf.Write(p)
}
func (c *responseWriterMocker) Code(code int) {
c.statusCode = code
}
func (c *responseWriterMocker) Flush() error {
return nil
}
func (c *responseWriterMocker) Raw() any {
return nil
}
func TestContext_BindJSON(t *testing.T) {
var as = assert.New(t)
t.Run("", func(t *testing.T) {
var ctx = newContextMocker()
ctx.Request.Body = io.NopCloser(bytes.NewBufferString(`{"age":1}`))
var params = struct {
Age int `json:"age"`
}{}
as.NoError(ctx.BindJSON(¶ms))
as.Equal(1, params.Age)
})
t.Run("", func(t *testing.T) {
var ctx = newContextMocker()
ctx.Request.Body = io.NopCloser(strings.NewReader(`{"age":1}`))
var params = struct {
Age int `json:"age"`
}{}
as.NoError(ctx.BindJSON(¶ms))
as.Equal(1, params.Age)
})
t.Run("", func(t *testing.T) {
var ctx = newContextMocker()
ctx.Request.Body = io.NopCloser(bytes.NewBufferString(`{"age":"1}`))
var params = struct {
Age int `json:"age"`
}{}
as.Error(ctx.BindJSON(¶ms))
})
t.Run("", func(t *testing.T) {
var ctx = newContextMocker()
var params = struct {
Age int `json:"age"`
}{}
as.Error(ctx.BindJSON(¶ms))
})
}
func TestContext_Write(t *testing.T) {
var as = assert.New(t)
t.Run("write json 1", func(t *testing.T) {
var ctx = newContextMocker()
var params = Any{"name": "aha"}
if err := ctx.WriteJSON(http.StatusOK, params); err != nil {
as.NoError(err)
return
}
var writer = ctx.Writer.(*responseWriterMocker)
as.Equal(http.StatusOK, writer.statusCode)
as.Equal(MimeJson, writer.header.Get(ContentType))
var buf = bytes.NewBufferString("")
codec.StdJsonCodec.NewEncoder(buf).Encode(params)
as.Equal(buf.Len(), writer.buf.Len())
})
t.Run("write string", func(t *testing.T) {
var ctx = newContextMocker()
var params = "hello"
if err := ctx.WriteString(http.StatusOK, params); err != nil {
as.NoError(err)
return
}
var writer = ctx.Writer.(*responseWriterMocker)
as.Equal(http.StatusOK, writer.statusCode)
as.Equal("", writer.header.Get(ContentType))
as.Equal(params, writer.buf.String())
})
t.Run("write string", func(t *testing.T) {
var ctx = newContextMocker()
var params = []byte("hello")
if err := ctx.WriteBytes(http.StatusOK, params); err != nil {
as.NoError(err)
return
}
var writer = ctx.Writer.(*responseWriterMocker)
as.Equal(http.StatusOK, writer.statusCode)
as.Equal("", writer.header.Get(ContentType))
as.Equal(string(params), writer.buf.String())
})
t.Run("write bytes", func(t *testing.T) {
var ctx = newContextMocker()
as.Error(ctx.WriteBytes(http.StatusOK, []byte(`"err"`)))
})
t.Run("write json", func(t *testing.T) {
var ctx = newContextMocker()
var err = ctx.WriteJSON(200, "err")
as.Error(err)
})
}
func TestContext_Storage(t *testing.T) {
var as = assert.New(t)
var ctx = newContextMocker()
ctx.Set("name", "aha")
ctx.Set("age", 1)
{
v, _ := ctx.Get("name")
as.Equal("aha", v)
}
{
v, _ := ctx.Get("age")
as.Equal(1, v)
}
}
func TestContext_Param(t *testing.T) {
var as = assert.New(t)
var router = New()
t.Run("", func(t *testing.T) {
var ctx = NewContext(router, &Request{
Header: &HttpHeader{http.Header{}},
VPath: "/:id",
}, newResponseWriterMocker())
id := ctx.Param("id")
as.Equal("", id)
})
t.Run("", func(t *testing.T) {
var ctx = NewContext(router, &Request{
Header: &HttpHeader{http.Header{}},
VPath: "/api/v1",
}, newResponseWriterMocker())
id := ctx.Param("id")
as.Equal("", id)
})
t.Run("", func(t *testing.T) {
var ctx = NewContext(router, &Request{
VPath: "/api/v1",
RPath: "/api/v1",
}, newResponseWriterMocker())
id := ctx.Param("id")
as.Equal("", id)
})
}
type closer struct{ *bytes.Buffer }
func (c *closer) Close() error { return nil }
func TestRequest_Close(t *testing.T) {
var req = &Request{Header: &HttpHeader{http.Header{}}, Body: &closer{bytes.NewBufferString(`{"hello":"world"}`)}}
var ctx = newContextMocker()
ctx.Request = req
var res = make(map[string]string)
ctx.BindJSON(&res)
assert.Equal(t, res["hello"], "world")
}