MCPcopy
hub / github.com/gin-gonic/gin / TestContextShouldBindBodyWithJSON

Function TestContextShouldBindBodyWithJSON

context_test.go:2560–2622  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2558}
2559
2560func TestContextShouldBindBodyWithJSON(t *testing.T) {
2561 for _, tt := range []struct {
2562 name string
2563 bindingBody binding.BindingBody
2564 body string
2565 }{
2566 {
2567 name: " JSON & JSON-BODY ",
2568 bindingBody: binding.JSON,
2569 body: `{"foo":"FOO"}`,
2570 },
2571 {
2572 name: " JSON & XML-BODY ",
2573 bindingBody: binding.XML,
2574 body: `<?xml version="1.0" encoding="UTF-8"?>
2575<root>
2576<foo>FOO</foo>
2577</root>`,
2578 },
2579 {
2580 name: " JSON & YAML-BODY ",
2581 bindingBody: binding.YAML,
2582 body: `foo: FOO`,
2583 },
2584 {
2585 name: " JSON & TOM-BODY ",
2586 bindingBody: binding.TOML,
2587 body: `foo=FOO`,
2588 },
2589 } {
2590 t.Logf("testing: %s", tt.name)
2591
2592 w := httptest.NewRecorder()
2593 c, _ := CreateTestContext(w)
2594
2595 c.Request, _ = http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(tt.body))
2596
2597 type typeJSON struct {
2598 Foo string `json:"foo" binding:"required"`
2599 }
2600 objJSON := typeJSON{}
2601
2602 if tt.bindingBody == binding.JSON {
2603 require.NoError(t, c.ShouldBindBodyWithJSON(&objJSON))
2604 assert.Equal(t, typeJSON{"FOO"}, objJSON)
2605 }
2606
2607 if tt.bindingBody == binding.XML {
2608 require.Error(t, c.ShouldBindBodyWithJSON(&objJSON))
2609 assert.Equal(t, typeJSON{}, objJSON)
2610 }
2611
2612 if tt.bindingBody == binding.YAML {
2613 require.Error(t, c.ShouldBindBodyWithJSON(&objJSON))
2614 assert.Equal(t, typeJSON{}, objJSON)
2615 }
2616
2617 if tt.bindingBody == binding.TOML {

Callers

nothing calls this directly

Calls 3

CreateTestContextFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected