Home / Function/ Test_Ctx_MsgPack() — fiber Function Reference

Test_Ctx_MsgPack() — fiber Function Reference

Architecture documentation for the Test_Ctx_MsgPack() function in ctx_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  d0f06fa4_32d9_49c3_1ef6_c923325cf919["Test_Ctx_MsgPack()"]
  7b3d4933_5ae3_f84d_ff6d_0cb34e268026["ctx_test.go"]
  d0f06fa4_32d9_49c3_1ef6_c923325cf919 -->|defined in| 7b3d4933_5ae3_f84d_ff6d_0cb34e268026
  style d0f06fa4_32d9_49c3_1ef6_c923325cf919 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

ctx_test.go lines 5916–5993

func Test_Ctx_MsgPack(t *testing.T) {
	t.Parallel()

	app := New(Config{
		MsgPackEncoder: msgpack.Marshal,
		MsgPackDecoder: msgpack.Unmarshal,
	})

	c := app.AcquireCtx(&fasthttp.RequestCtx{})

	err := c.MsgPack(complex(1, 1))

	require.NoError(t, err)
	require.Equal(t, "\u0600?\xf0\x00\x00\x00\x00\x00\x00?\xf0\x00\x00\x00\x00\x00\x00", string(c.Response().Body()))

	// Test without ctype
	err = c.MsgPack(Map{ // map has no order
		"Name": "Grame",
	})
	require.NoError(t, err)
	require.Equal(t, "\x81\xa4Name\xa5Grame", string(c.Response().Body()))
	require.Equal(t, MIMEApplicationMsgPack, string(c.Response().Header.Peek("content-type")))

	// Test with ctype
	err = c.MsgPack(Map{ // map has no order
		"Name": "Grame",
	}, "application/problem+msgpack")
	require.NoError(t, err)
	require.Equal(t, "\x81\xa4Name\xa5Grame", string(c.Response().Body()))
	require.Equal(t, "application/problem+msgpack", string(c.Response().Header.Peek("content-type")))

	testEmpty := func(v any, r string) {
		err := c.MsgPack(v)
		require.NoError(t, err)
		require.Equal(t, r, string(c.Response().Body()))
	}

	testEmpty(nil, "\xc0")
	testEmpty("", "\xa0")
	testEmpty(0, "\x00")
	testEmpty([]int{}, "\x90")

	t.Run("custom msgpack encoder", func(t *testing.T) {
		t.Parallel()

		app := New(Config{
			MsgPackEncoder: func(_ any) ([]byte, error) {
				return []byte(`["custom","msgpack"]`), nil
			},
		})
		c := app.AcquireCtx(&fasthttp.RequestCtx{})

		err := c.MsgPack(Map{ // map has no order
			"Name": "Grame",
			"Age":  20,
		})
		require.NoError(t, err)
		require.Equal(t, `["custom","msgpack"]`, string(c.Response().Body()))
		require.Equal(t, MIMEApplicationMsgPack, string(c.Response().Header.Peek("content-type")))
	})

	t.Run("error msgpack", func(t *testing.T) {
		t.Parallel()

		app := New(Config{
			MsgPackEncoder: func(_ any) ([]byte, error) {
				return []byte("error"), errors.New("msgpack error")
			},
		})
		c := app.AcquireCtx(&fasthttp.RequestCtx{})

		err := c.MsgPack(Map{ // map has no order
			"Name": "Grame",
			"Age":  20,
		})
		require.Error(t, err)
	})
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does Test_Ctx_MsgPack() do?
Test_Ctx_MsgPack() is a function in the fiber codebase, defined in ctx_test.go.
Where is Test_Ctx_MsgPack() defined?
Test_Ctx_MsgPack() is defined in ctx_test.go at line 5916.

Analyze Your Own Codebase

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

Try Supermodel Free