-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_embedding_test.go
More file actions
259 lines (219 loc) · 6.9 KB
/
Copy pathrequest_embedding_test.go
File metadata and controls
259 lines (219 loc) · 6.9 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package httpserver
import (
"io"
"mime/multipart"
"net/http"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// ============ Embedded struct field tests ============
//
// These tests verify that embedded structs with tags work correctly, including:
// - Empty tags (header:"", query:"", etc.) on embedded fields that store field
// indices for direct binding.
// - Non-empty tags on embedded fields, which go through common.BindStructWithTag
// (mapstructure with Squash:true).
// - Nested anonymous structs (multiple levels of embedding).
// - Unexported fields are skipped.
func tagPanicGuard(t *testing.T) {
t.Helper()
if r := recover(); r != nil {
t.Fatalf("unexpected panic — bug is present: %v", r)
}
}
// ------------ default tag on embedded field ------------
func TestEmbed_DefaultTag(t *testing.T) {
defer tagPanicGuard(t)
type Base struct {
Name string `default:"alice"`
}
type Base2 struct {
Base
Name string `default:"alice"`
}
type Req struct {
Name string `default:"alice"`
Base2
Base
}
captured, rec := doRequest[Req](t, captureHandler[Req], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "alice", captured.request.Name)
}
// ------------ empty header tag on embedded field ------------
func TestEmbed_EmptyHeaderTag(t *testing.T) {
defer tagPanicGuard(t)
type Base struct {
Headers http.Header `header:""`
}
type Req struct {
Base
}
captured, rec := doRequest[Req](t, captureHandler[Req],
http.MethodGet, "/", withHeader("X-Custom", "value"))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.Header{"X-Custom": {"value"}}, captured.request.Headers)
}
// ------------ empty cookie tag on embedded field ------------
func TestEmbed_EmptyCookieTag(t *testing.T) {
defer tagPanicGuard(t)
type Base struct {
Cookies KeyValues `cookie:""`
}
type Req struct {
Base
}
captured, rec := doRequest[Req](t, captureHandler[Req],
http.MethodGet, "/", withCookie("name", "value"))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, KeyValues{"name": {"value"}}, captured.request.Cookies)
}
// ------------ empty query tag on embedded field ------------
func TestEmbed_EmptyQueryTag(t *testing.T) {
defer tagPanicGuard(t)
type Base struct {
Params KeyValues `query:""`
}
type Req struct {
Base
}
captured, rec := doRequest[Req](t, captureHandler[Req],
http.MethodGet, "/", withQuery("key=value"))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, KeyValues{"key": {"value"}}, captured.request.Params)
}
// ------------ empty url tag on embedded field ------------
func TestEmbed_EmptyUrlTag(t *testing.T) {
defer tagPanicGuard(t)
type Base struct {
Params KeyValue `url:""`
}
type Req struct {
Base
}
captured, rec := doServeMuxRequest[Req](t, http.MethodGet, "/{id}", "/123",
captureHandler[Req])
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, KeyValue{"id": "123"}, captured.request.Params)
}
// ------------ empty form tag on embedded field ------------
//
// bindForm is called through bindFullTextBody which runs the binder in a
// goroutine with a recover. We call createTags + bindForm directly here to
// test in the main goroutine for simplicity.
func TestEmbed_EmptyFormTag(t *testing.T) {
defer tagPanicGuard(t)
type Base struct {
Values KeyValues `form:""`
}
type Req struct {
Base
}
reqType := reflect.TypeFor[Req]()
tags := createTags(reqType)
var req Req
parsed := reflect.ValueOf(&req).Elem()
_, err := tags.bindForm(strings.NewReader("key=value"), parsed)
assert.NoError(t, err)
assert.Equal(t, KeyValues{"key": {"value"}}, req.Base.Values)
}
// ------------ empty json tag on embedded field ------------
func TestEmbed_EmptyJsonTag(t *testing.T) {
type Base struct {
Data map[string]any `json:""`
}
type Req struct {
Base
}
captured, rec := doRequest[Req](t, captureHandler[Req],
http.MethodPost, "/", withRawBody("application/json", []byte(`{"foo":"bar"}`)))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, map[string]any{"foo": "bar"}, captured.request.Data)
}
// ------------ multipart tag on embedded field ------------
func TestEmbed_MultipartTag(t *testing.T) {
defer tagPanicGuard(t)
type Base struct {
Reader *multipart.Reader `multipart:""`
}
type Req struct {
Base
}
captured, rec := doRequest[Req](t, captureHandler[Req],
http.MethodPost, "/", withMultipartBody(t, func(w *multipart.Writer) {
_ = w.WriteField("key", "value")
}))
assert.Equal(t, http.StatusOK, rec.Code)
assert.NotNil(t, captured.request.Reader)
}
// ------------ body tag on embedded field ------------
func TestEmbed_BodyTag(t *testing.T) {
defer tagPanicGuard(t)
type Base struct {
Body io.ReadCloser `body:""`
}
type Req struct {
Base
}
captured, rec := doRequest[Req](t, captureHandler[Req],
http.MethodPost, "/", withRawBody("text/plain", []byte("hello")))
assert.Equal(t, http.StatusOK, rec.Code)
assert.NotNil(t, captured.request.Body)
}
// ------------ unexported fields are skipped ------------
func TestEmbed_UnexportedField_Skipped(t *testing.T) {
type Req struct {
Name string `query:"name"`
age int // unexported — should be skipped
}
captured, rec := doRequest[Req](t, captureHandler[Req],
http.MethodGet, "/", withQuery("name=alice"))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "alice", captured.request.Name, "Name")
assert.Equal(t, 0, captured.request.age, "age (unexported, not set)")
}
// ------------ contrast: non-empty tags on embedded field work ------------
//
// Non-empty tags (header:"X", query:"q", etc.) don't store field indices.
// They set the tags.flags bit and binding goes through common.BindStructWithTag
// (mapstructure with Squash:true), which handles embedding correctly.
func TestEmbed_NonEmptyTag_Works(t *testing.T) {
type Inner struct {
Name string `query:"name"`
}
type Req struct {
Inner
Top string `query:"top"`
}
captured, rec := doRequest[Req](t, captureHandler[Req], http.MethodGet, "/",
withQuery("name=alice&top=hello"))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "alice", captured.request.Name)
assert.Equal(t, "hello", captured.request.Top)
}
// ------------ nested anonymous structs (3 levels) ------------
func TestEmbed_NestedAnonymous(t *testing.T) {
type Inner struct {
Val string `query:"val"`
}
type Middle struct {
Inner
Mid string `query:"mid"`
}
type Req struct {
Middle
Top string `query:"top"`
}
captured, rec := doRequest[Req](t, captureHandler[Req], http.MethodGet, "/", withQuery("val=a&mid=b&top=c"))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "a", captured.request.Val, "Val")
assert.Equal(t, "b", captured.request.Mid, "Mid")
assert.Equal(t, "c", captured.request.Top, "Top")
}