TestContextShouldBindBodyWith() — gin Function Reference
Architecture documentation for the TestContextShouldBindBodyWith() function in context_test.go from the gin codebase.
Entity Profile
Dependency Diagram
graph TD d85c554e_b5bd_61c4_ecf1_09c2d0797d42["TestContextShouldBindBodyWith()"] ebe0ae48_a62b_a38f_5bac_5bbbd96fc508["context_test.go"] d85c554e_b5bd_61c4_ecf1_09c2d0797d42 -->|defined in| ebe0ae48_a62b_a38f_5bac_5bbbd96fc508 style d85c554e_b5bd_61c4_ecf1_09c2d0797d42 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
context_test.go lines 2480–2557
func TestContextShouldBindBodyWith(t *testing.T) {
type typeA struct {
Foo string `json:"foo" xml:"foo" binding:"required"`
}
type typeB struct {
Bar string `json:"bar" xml:"bar" binding:"required"`
}
for _, tt := range []struct {
name string
bindingA, bindingB binding.BindingBody
bodyA, bodyB string
}{
{
name: "JSON & JSON",
bindingA: binding.JSON,
bindingB: binding.JSON,
bodyA: `{"foo":"FOO"}`,
bodyB: `{"bar":"BAR"}`,
},
{
name: "JSON & XML",
bindingA: binding.JSON,
bindingB: binding.XML,
bodyA: `{"foo":"FOO"}`,
bodyB: `<?xml version="1.0" encoding="UTF-8"?>
<root>
<bar>BAR</bar>
</root>`,
},
{
name: "XML & XML",
bindingA: binding.XML,
bindingB: binding.XML,
bodyA: `<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo>FOO</foo>
</root>`,
bodyB: `<?xml version="1.0" encoding="UTF-8"?>
<root>
<bar>BAR</bar>
</root>`,
},
} {
t.Logf("testing: %s", tt.name)
// bodyA to typeA and typeB
{
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.Request, _ = http.NewRequest(
http.MethodPost, "http://example.com", strings.NewReader(tt.bodyA),
)
// When it binds to typeA and typeB, it finds the body is
// not typeB but typeA.
objA := typeA{}
require.NoError(t, c.ShouldBindBodyWith(&objA, tt.bindingA))
assert.Equal(t, typeA{"FOO"}, objA)
objB := typeB{}
require.Error(t, c.ShouldBindBodyWith(&objB, tt.bindingB))
assert.NotEqual(t, typeB{"BAR"}, objB)
}
// bodyB to typeA and typeB
{
// When it binds to typeA and typeB, it finds the body is
// not typeA but typeB.
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.Request, _ = http.NewRequest(
http.MethodPost, "http://example.com", strings.NewReader(tt.bodyB),
)
objA := typeA{}
require.Error(t, c.ShouldBindBodyWith(&objA, tt.bindingA))
assert.NotEqual(t, typeA{"FOO"}, objA)
objB := typeB{}
require.NoError(t, c.ShouldBindBodyWith(&objB, tt.bindingB))
assert.Equal(t, typeB{"BAR"}, objB)
}
}
}
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does TestContextShouldBindBodyWith() do?
TestContextShouldBindBodyWith() is a function in the gin codebase, defined in context_test.go.
Where is TestContextShouldBindBodyWith() defined?
TestContextShouldBindBodyWith() is defined in context_test.go at line 2480.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free