Home / Function/ TestContextShouldBindBodyWithPlain() — gin Function Reference

TestContextShouldBindBodyWithPlain() — gin Function Reference

Architecture documentation for the TestContextShouldBindBodyWithPlain() function in context_test.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  08a3ac47_f462_ed2c_da90_a31423a4be44["TestContextShouldBindBodyWithPlain()"]
  ebe0ae48_a62b_a38f_5bac_5bbbd96fc508["context_test.go"]
  08a3ac47_f462_ed2c_da90_a31423a4be44 -->|defined in| ebe0ae48_a62b_a38f_5bac_5bbbd96fc508
  style 08a3ac47_f462_ed2c_da90_a31423a4be44 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

context_test.go lines 2816–2889

func TestContextShouldBindBodyWithPlain(t *testing.T) {
	for _, tt := range []struct {
		name        string
		bindingBody binding.BindingBody
		body        string
	}{
		{
			name:        " JSON & JSON-BODY ",
			bindingBody: binding.JSON,
			body:        `{"foo":"FOO"}`,
		},
		{
			name:        " JSON & XML-BODY ",
			bindingBody: binding.XML,
			body: `<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo>FOO</foo>
</root>`,
		},
		{
			name:        " JSON & YAML-BODY ",
			bindingBody: binding.YAML,
			body:        `foo: FOO`,
		},
		{
			name:        " JSON & TOM-BODY ",
			bindingBody: binding.TOML,
			body:        `foo=FOO`,
		},
		{
			name:        " JSON & Plain-BODY ",
			bindingBody: binding.Plain,
			body:        `foo=FOO`,
		},
	} {
		t.Logf("testing: %s", tt.name)

		w := httptest.NewRecorder()
		c, _ := CreateTestContext(w)

		c.Request, _ = http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(tt.body))

		type typeJSON struct {
			Foo string `json:"foo" binding:"required"`
		}
		objJSON := typeJSON{}

		if tt.bindingBody == binding.Plain {
			body := ""
			require.NoError(t, c.ShouldBindBodyWithPlain(&body))
			assert.Equal(t, "foo=FOO", body)
		}

		if tt.bindingBody == binding.JSON {
			require.NoError(t, c.ShouldBindBodyWithJSON(&objJSON))
			assert.Equal(t, typeJSON{"FOO"}, objJSON)
		}

		if tt.bindingBody == binding.XML {
			require.Error(t, c.ShouldBindBodyWithJSON(&objJSON))
			assert.Equal(t, typeJSON{}, objJSON)
		}

		if tt.bindingBody == binding.YAML {
			require.Error(t, c.ShouldBindBodyWithJSON(&objJSON))
			assert.Equal(t, typeJSON{}, objJSON)
		}

		if tt.bindingBody == binding.TOML {
			require.Error(t, c.ShouldBindBodyWithJSON(&objJSON))
			assert.Equal(t, typeJSON{}, objJSON)
		}
	}
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does TestContextShouldBindBodyWithPlain() do?
TestContextShouldBindBodyWithPlain() is a function in the gin codebase, defined in context_test.go.
Where is TestContextShouldBindBodyWithPlain() defined?
TestContextShouldBindBodyWithPlain() is defined in context_test.go at line 2816.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free