Commit b917b14
fix(binding): empty value error (#2169)
* fix empty value error
Here is the code that can report an error
```go
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"time"
)
type header struct {
Duration time.Duration `header:"duration"`
CreateTime time.Time `header:"createTime" time_format:"unix"`
}
func needFix1() {
g := gin.Default()
g.GET("/", func(c *gin.Context) {
h := header{}
err := c.ShouldBindHeader(&h)
if err != nil {
c.JSON(500, fmt.Sprintf("fail:%s\n", err))
return
}
c.JSON(200, h)
})
g.Run(":8081")
}
func needFix2() {
g := gin.Default()
g.GET("/", func(c *gin.Context) {
h := header{}
err := c.ShouldBindHeader(&h)
if err != nil {
c.JSON(500, fmt.Sprintf("fail:%s\n", err))
return
}
c.JSON(200, h)
})
g.Run(":8082")
}
func sendNeedFix1() {
// send to needFix1
sendBadData("http://127.0.0.1:8081", "duration")
}
func sendNeedFix2() {
// send to needFix2
sendBadData("http://127.0.0.1:8082", "createTime")
}
func sendBadData(url, key string) {
req, err := http.NewRequest("GET", "http://127.0.0.1:8081", nil)
if err != nil {
fmt.Printf("err:%s\n", err)
return
}
// Only the key and no value can cause an error
req.Header.Add(key, "")
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
io.Copy(os.Stdout, rsp.Body)
rsp.Body.Close()
}
func main() {
go needFix1()
go needFix2()
time.Sleep(time.Second / 1000 * 200) // 200ms
sendNeedFix1()
sendNeedFix2()
}
```
* modify code
* add comment
* test(binding): use 'any' alias and require.NoError in form mapping tests
- Replace 'interface{}' with 'any' alias in bindTestData struct
- Change assert.NoError to require.NoError in TestMappingTimeUnixNano and TestMappingTimeDuration to fail fast on mapping errors
---------
Co-authored-by: Bo-Yi Wu <[email protected]>1 parent fad706f commit b917b14
2 files changed
+53
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
303 | 308 | | |
304 | 309 | | |
305 | 310 | | |
| |||
404 | 409 | | |
405 | 410 | | |
406 | 411 | | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
407 | 417 | | |
408 | 418 | | |
409 | 419 | | |
| |||
427 | 437 | | |
428 | 438 | | |
429 | 439 | | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | 440 | | |
436 | 441 | | |
437 | 442 | | |
| |||
475 | 480 | | |
476 | 481 | | |
477 | 482 | | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
478 | 487 | | |
479 | 488 | | |
480 | 489 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
226 | 226 | | |
227 | 227 | | |
228 | 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 | + | |
229 | 253 | | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
230 | 258 | | |
231 | 259 | | |
232 | 260 | | |
| |||
236 | 264 | | |
237 | 265 | | |
238 | 266 | | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
239 | 278 | | |
240 | 279 | | |
241 | 280 | | |
| |||
0 commit comments