| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package binding |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"net/http" |
| 21 |
"net/http/httptest" |
| 22 |
"reflect" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
|
| 26 |
chi "github.com/go-chi/chi/v5" |
| 27 |
"github.com/stretchr/testify/assert" |
| 28 |
"github.com/stretchr/testify/require" |
| 29 |
) |
| 30 |
|
| 31 |
var formTestCases = []formTestCase{ |
| 32 |
{ |
| 33 |
description: "Happy path", |
| 34 |
shouldSucceed: true, |
| 35 |
payload: `title=Glorious+Post+Title&content=Lorem+ipsum+dolor+sit+amet`, |
| 36 |
contentType: formContentType, |
| 37 |
expected: Post{Title: "Glorious Post Title", Content: "Lorem ipsum dolor sit amet"}, |
| 38 |
}, |
| 39 |
{ |
| 40 |
description: "Happy path with interface", |
| 41 |
shouldSucceed: true, |
| 42 |
withInterface: true, |
| 43 |
payload: `title=Glorious+Post+Title&content=Lorem+ipsum+dolor+sit+amet`, |
| 44 |
contentType: formContentType, |
| 45 |
expected: Post{Title: "Glorious Post Title", Content: "Lorem ipsum dolor sit amet"}, |
| 46 |
}, |
| 47 |
{ |
| 48 |
description: "Empty payload", |
| 49 |
shouldSucceed: false, |
| 50 |
payload: ``, |
| 51 |
contentType: formContentType, |
| 52 |
expected: Post{}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
description: "Empty content type", |
| 56 |
shouldSucceed: false, |
| 57 |
payload: `title=Glorious+Post+Title&content=Lorem+ipsum+dolor+sit+amet`, |
| 58 |
contentType: ``, |
| 59 |
expected: Post{}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
description: "Malformed form body", |
| 63 |
shouldSucceed: false, |
| 64 |
payload: `title=%2`, |
| 65 |
contentType: formContentType, |
| 66 |
expected: Post{}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
description: "With nested and embedded structs", |
| 70 |
shouldSucceed: true, |
| 71 |
payload: `title=Glorious+Post+Title&id=1&name=Matt+Holt`, |
| 72 |
contentType: formContentType, |
| 73 |
expected: BlogPost{Post: Post{Title: "Glorious Post Title"}, Id: 1, Author: Person{Name: "Matt Holt"}}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
description: "Required embedded struct field not specified", |
| 77 |
shouldSucceed: false, |
| 78 |
payload: `id=1&name=Matt+Holt`, |
| 79 |
contentType: formContentType, |
| 80 |
expected: BlogPost{Id: 1, Author: Person{Name: "Matt Holt"}}, |
| 81 |
}, |
| 82 |
{ |
| 83 |
description: "Required nested struct field not specified", |
| 84 |
shouldSucceed: false, |
| 85 |
payload: `title=Glorious+Post+Title&id=1`, |
| 86 |
contentType: formContentType, |
| 87 |
expected: BlogPost{Post: Post{Title: "Glorious Post Title"}, Id: 1}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
description: "Multiple values into slice", |
| 91 |
shouldSucceed: true, |
| 92 |
payload: `title=Glorious+Post+Title&id=1&name=Matt+Holt&rating=4&rating=3&rating=5`, |
| 93 |
contentType: formContentType, |
| 94 |
expected: BlogPost{Post: Post{Title: "Glorious Post Title"}, Id: 1, Author: Person{Name: "Matt Holt"}, Ratings: []int{4, 3, 5}}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
description: "Unexported field", |
| 98 |
shouldSucceed: true, |
| 99 |
payload: `title=Glorious+Post+Title&id=1&name=Matt+Holt&unexported=foo`, |
| 100 |
contentType: formContentType, |
| 101 |
expected: BlogPost{Post: Post{Title: "Glorious Post Title"}, Id: 1, Author: Person{Name: "Matt Holt"}}, |
| 102 |
}, |
| 103 |
{ |
| 104 |
description: "Query string POST", |
| 105 |
shouldSucceed: true, |
| 106 |
payload: `title=Glorious+Post+Title&content=Lorem+ipsum+dolor+sit+amet`, |
| 107 |
contentType: formContentType, |
| 108 |
expected: Post{Title: "Glorious Post Title", Content: "Lorem ipsum dolor sit amet"}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
description: "Query string with Content-Type (POST request)", |
| 112 |
shouldSucceed: true, |
| 113 |
queryString: "?title=Glorious+Post+Title&content=Lorem+ipsum+dolor+sit+amet", |
| 114 |
payload: ``, |
| 115 |
contentType: formContentType, |
| 116 |
expected: Post{Title: "Glorious Post Title", Content: "Lorem ipsum dolor sit amet"}, |
| 117 |
}, |
| 118 |
{ |
| 119 |
description: "Query string without Content-Type (GET request)", |
| 120 |
shouldSucceed: true, |
| 121 |
method: "GET", |
| 122 |
queryString: "?title=Glorious+Post+Title&content=Lorem+ipsum+dolor+sit+amet", |
| 123 |
payload: ``, |
| 124 |
expected: Post{Title: "Glorious Post Title", Content: "Lorem ipsum dolor sit amet"}, |
| 125 |
}, |
| 126 |
{ |
| 127 |
description: "Embed struct pointer", |
| 128 |
shouldSucceed: true, |
| 129 |
deepEqual: true, |
| 130 |
method: "GET", |
| 131 |
queryString: "?name=Glorious+Post+Title&email=Lorem+ipsum+dolor+sit+amet", |
| 132 |
payload: ``, |
| 133 |
expected: EmbedPerson{&Person{Name: "Glorious Post Title", Email: "Lorem ipsum dolor sit amet"}}, |
| 134 |
}, |
| 135 |
{ |
| 136 |
description: "Embed struct pointer remain nil if not binded", |
| 137 |
shouldSucceed: true, |
| 138 |
deepEqual: true, |
| 139 |
method: "GET", |
| 140 |
queryString: "?", |
| 141 |
payload: ``, |
| 142 |
expected: EmbedPerson{nil}, |
| 143 |
}, |
| 144 |
{ |
| 145 |
description: "Custom error handler", |
| 146 |
shouldSucceed: true, |
| 147 |
deepEqual: true, |
| 148 |
method: "GET", |
| 149 |
queryString: "?", |
| 150 |
payload: ``, |
| 151 |
expected: CustomErrorHandle{}, |
| 152 |
}, |
| 153 |
{ |
| 154 |
description: "pointer form", |
| 155 |
shouldSucceed: true, |
| 156 |
deepEqual: true, |
| 157 |
payload: fmt.Sprintf("Id=%d&IdPointer=%d&Url=%s&UrlPointer=%s&AlphaDash=%s&AlphaDashPointer=%s", idInt, idInt, urlStr, urlStr, alphaDashStr, alphaDashStr), |
| 158 |
contentType: formContentType, |
| 159 |
expected: PointerForm{ID: idInt, IDPointer: &idInt, URL: urlStr, URLPointer: &urlStr, AlphaDash: alphaDashStr, AlphaDashPointer: &alphaDashStr}, |
| 160 |
}, |
| 161 |
{ |
| 162 |
description: "Trim spacing", |
| 163 |
shouldSucceed: true, |
| 164 |
payload: `title=Glorious+Post+Title%20%20&description=A%20description%20with%20intentional%20spaces%20%20&slug=glorious-post%20`, |
| 165 |
contentType: formContentType, |
| 166 |
expected: TrimSpaceForm{Title: "Glorious Post Title", Description: "A description with intentional spaces ", Slug: "glorious-post"}, |
| 167 |
}, |
| 168 |
{ |
| 169 |
description: "Trim spacing and validating", |
| 170 |
shouldSucceed: false, |
| 171 |
payload: `title=Glorious+Post+Title%20%20&description=A%20description%20with%20intentional%20spaces%20%20&slug=%20`, |
| 172 |
contentType: formContentType, |
| 173 |
expected: TrimSpaceForm{Title: "Glorious Post Title", Description: "A description with intentional spaces "}, |
| 174 |
}, |
| 175 |
} |
| 176 |
|
| 177 |
func init() { |
| 178 |
AddRule(&Rule{ |
| 179 |
func(rule string) bool { |
| 180 |
return rule == "CustomRule" |
| 181 |
}, |
| 182 |
func(errs Errors, _ string, _ any) (bool, Errors) { |
| 183 |
return false, errs |
| 184 |
}, |
| 185 |
}) |
| 186 |
SetNameMapper(nameMapper) |
| 187 |
} |
| 188 |
|
| 189 |
func Test_Form(t *testing.T) { |
| 190 |
for _, testCase := range formTestCases { |
| 191 |
t.Run(testCase.description, func(t *testing.T) { |
| 192 |
performFormTest(t, Form, testCase) |
| 193 |
}) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func performFormTest(t *testing.T, binder handlerFunc, testCase formTestCase) { |
| 198 |
resp := httptest.NewRecorder() |
| 199 |
m := chi.NewRouter() |
| 200 |
|
| 201 |
formTestHandler := func(actual any, errs Errors) { |
| 202 |
if testCase.shouldSucceed { |
| 203 |
assert.Empty(t, errs) |
| 204 |
} else if !testCase.shouldSucceed { |
| 205 |
assert.NotEmpty(t, errs) |
| 206 |
} |
| 207 |
expString := fmt.Sprintf("%+v", testCase.expected) |
| 208 |
actString := fmt.Sprintf("%+v", actual) |
| 209 |
if actString != expString && (!testCase.deepEqual || !reflect.DeepEqual(testCase.expected, actual)) { |
| 210 |
assert.Equal(t, expString, actString) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
switch p := testCase.expected.(type) { |
| 215 |
case Post: |
| 216 |
if testCase.withInterface { |
| 217 |
m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 218 |
var actual Post |
| 219 |
errs := binder(req, &actual) |
| 220 |
assert.Equal(t, p.Title, actual.Title) |
| 221 |
formTestHandler(actual, errs) |
| 222 |
}) |
| 223 |
} else { |
| 224 |
m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 225 |
var actual Post |
| 226 |
errs := binder(req, &actual) |
| 227 |
formTestHandler(actual, errs) |
| 228 |
}) |
| 229 |
m.Get(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 230 |
var actual Post |
| 231 |
errs := binder(req, &actual) |
| 232 |
formTestHandler(actual, errs) |
| 233 |
}) |
| 234 |
} |
| 235 |
|
| 236 |
case BlogPost: |
| 237 |
if testCase.withInterface { |
| 238 |
m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 239 |
var actual BlogPost |
| 240 |
errs := binder(req, &actual) |
| 241 |
assert.Equal(t, p.Title, actual.Title) |
| 242 |
formTestHandler(actual, errs) |
| 243 |
}) |
| 244 |
} else { |
| 245 |
m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 246 |
var actual BlogPost |
| 247 |
errs := binder(req, &actual) |
| 248 |
formTestHandler(actual, errs) |
| 249 |
}) |
| 250 |
} |
| 251 |
|
| 252 |
case EmbedPerson: |
| 253 |
m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 254 |
var actual EmbedPerson |
| 255 |
errs := binder(req, &actual) |
| 256 |
formTestHandler(actual, errs) |
| 257 |
}) |
| 258 |
m.Get(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 259 |
var actual EmbedPerson |
| 260 |
errs := binder(req, &actual) |
| 261 |
formTestHandler(actual, errs) |
| 262 |
}) |
| 263 |
case CustomErrorHandle: |
| 264 |
m.Get(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 265 |
var actual CustomErrorHandle |
| 266 |
errs := binder(req, &actual) |
| 267 |
formTestHandler(actual, errs) |
| 268 |
}) |
| 269 |
case PointerForm: |
| 270 |
m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 271 |
var actual PointerForm |
| 272 |
errs := binder(req, &actual) |
| 273 |
formTestHandler(actual, errs) |
| 274 |
}) |
| 275 |
case TrimSpaceForm: |
| 276 |
m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 277 |
var actual TrimSpaceForm |
| 278 |
errs := binder(req, &actual) |
| 279 |
formTestHandler(actual, errs) |
| 280 |
}) |
| 281 |
} |
| 282 |
|
| 283 |
if len(testCase.method) == 0 { |
| 284 |
testCase.method = "POST" |
| 285 |
} |
| 286 |
|
| 287 |
req, err := http.NewRequest(testCase.method, testRoute+testCase.queryString, strings.NewReader(testCase.payload)) |
| 288 |
if err != nil { |
| 289 |
panic(err) |
| 290 |
} |
| 291 |
req.Header.Set("Content-Type", testCase.contentType) |
| 292 |
|
| 293 |
m.ServeHTTP(resp, req) |
| 294 |
|
| 295 |
switch resp.Code { |
| 296 |
case http.StatusNotFound: |
| 297 |
panic("Routing is messed up in test fixture (got 404): check methods and paths") |
| 298 |
case http.StatusInternalServerError: |
| 299 |
panic("Something bad happened on '" + testCase.description + "'") |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
type ( |
| 304 |
formTestCase struct { |
| 305 |
description string |
| 306 |
shouldSucceed bool |
| 307 |
deepEqual bool |
| 308 |
withInterface bool |
| 309 |
queryString string |
| 310 |
payload string |
| 311 |
contentType string |
| 312 |
expected any |
| 313 |
method string |
| 314 |
} |
| 315 |
) |
| 316 |
|
| 317 |
type defaultForm struct { |
| 318 |
Default string `binding:"Default(hello world)"` |
| 319 |
} |
| 320 |
|
| 321 |
func Test_Default(t *testing.T) { |
| 322 |
m := chi.NewRouter() |
| 323 |
m.Get("/", func(_ http.ResponseWriter, req *http.Request) { |
| 324 |
var f defaultForm |
| 325 |
Bind(req, &f) |
| 326 |
assert.Equal(t, "hello world", f.Default) |
| 327 |
}) |
| 328 |
resp := httptest.NewRecorder() |
| 329 |
req, err := http.NewRequest("GET", "/", nil) |
| 330 |
require.NoError(t, err) |
| 331 |
|
| 332 |
m.ServeHTTP(resp, req) |
| 333 |
} |