Home / Function/ Test_Bind_Body() — fiber Function Reference

Test_Bind_Body() — fiber Function Reference

Architecture documentation for the Test_Bind_Body() function in bind_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  67d821b2_6c97_10bb_19c7_b75d1f37b7cd["Test_Bind_Body()"]
  55065b01_f5dc_4e53_5a74_5ecc7aca8f52["bind_test.go"]
  67d821b2_6c97_10bb_19c7_b75d1f37b7cd -->|defined in| 55065b01_f5dc_4e53_5a74_5ecc7aca8f52
  style 67d821b2_6c97_10bb_19c7_b75d1f37b7cd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

bind_test.go lines 893–1097

func Test_Bind_Body(t *testing.T) {
	t.Parallel()
	app := New(Config{
		MsgPackEncoder: msgpack.Marshal,
		MsgPackDecoder: msgpack.Unmarshal,
		CBOREncoder:    cbor.Marshal,
		CBORDecoder:    cbor.Unmarshal,
	})
	reqBody := []byte(`{"name":"john"}`)

	type Demo struct {
		Name  string   `json:"name" xml:"name" form:"name" query:"name" msgpack:"name"`
		Names []string `json:"names" xml:"names" form:"names" query:"names" msgpack:"names"`
	}

	// Helper function to test compressed bodies
	testCompressedBody := func(t *testing.T, compressedBody []byte, encoding string) {
		t.Helper()
		c := app.AcquireCtx(&fasthttp.RequestCtx{})
		c.Request().Header.SetContentType(MIMEApplicationJSON)
		c.Request().Header.Set(fasthttp.HeaderContentEncoding, encoding)
		c.Request().SetBody(compressedBody)
		c.Request().Header.SetContentLength(len(compressedBody))
		d := new(Demo)
		require.NoError(t, c.Bind().Body(d))
		require.Equal(t, "john", d.Name)
		c.Request().Header.Del(fasthttp.HeaderContentEncoding)
	}

	t.Run("Gzip", func(t *testing.T) {
		t.Parallel()
		compressedBody := fasthttp.AppendGzipBytes(nil, reqBody)
		require.NotEqual(t, reqBody, compressedBody)
		testCompressedBody(t, compressedBody, "gzip")
	})

	t.Run("Deflate", func(t *testing.T) {
		t.Parallel()
		compressedBody := fasthttp.AppendDeflateBytes(nil, reqBody)
		require.NotEqual(t, reqBody, compressedBody)
		testCompressedBody(t, compressedBody, "deflate")
	})

	t.Run("Brotli", func(t *testing.T) {
		t.Parallel()
		compressedBody := fasthttp.AppendBrotliBytes(nil, reqBody)
		require.NotEqual(t, reqBody, compressedBody)
		testCompressedBody(t, compressedBody, "br")
	})

	t.Run("Zstd", func(t *testing.T) {
		t.Parallel()
		compressedBody := fasthttp.AppendZstdBytes(nil, reqBody)
		require.NotEqual(t, reqBody, compressedBody)
		testCompressedBody(t, compressedBody, "zstd")
	})

	testDecodeParser := func(t *testing.T, contentType string, body []byte) {
		t.Helper()
		c := app.AcquireCtx(&fasthttp.RequestCtx{})
		c.Request().Header.SetContentType(contentType)
		c.Request().SetBody(body)
		c.Request().Header.SetContentLength(len(body))
		d := new(Demo)
		require.NoError(t, c.Bind().Body(d))
		require.Equal(t, "john", d.Name)
	}

	testErrorParser := func(t *testing.T, contentType string, body []byte) {
		t.Helper()
		c := app.AcquireCtx(&fasthttp.RequestCtx{})
		c.Request().Header.SetContentType(contentType)
		c.Request().SetBody(body)
		c.Request().Header.SetContentLength(len(body))
		d := new(Demo)
		err := c.Bind().Body(d)
		require.Error(t, err)
	}

	t.Run("JSON", func(t *testing.T) {
		testDecodeParser(t, MIMEApplicationJSON, []byte(`{"name":"john"}`))

Domain

Subdomains

Defined In

Frequently Asked Questions

What does Test_Bind_Body() do?
Test_Bind_Body() is a function in the fiber codebase, defined in bind_test.go.
Where is Test_Bind_Body() defined?
Test_Bind_Body() is defined in bind_test.go at line 893.

Analyze Your Own Codebase

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

Try Supermodel Free