Home / Function/ Test_Ctx_CBOR() — fiber Function Reference

Test_Ctx_CBOR() — fiber Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

ctx_test.go lines 6023–6092

func Test_Ctx_CBOR(t *testing.T) {
	t.Parallel()
	app := New(Config{
		CBOREncoder: cbor.Marshal,
		CBORDecoder: cbor.Unmarshal,
	})
	c := app.AcquireCtx(&fasthttp.RequestCtx{})

	require.Error(t, c.CBOR(complex(1, 1)))

	type dummyStruct struct {
		Name string
		Age  int
	}

	// Test without ctype
	err := c.CBOR(dummyStruct{ // map has no order
		Name: "Grame",
		Age:  20,
	})
	require.NoError(t, err)
	require.Equal(t, `a2644e616d65654772616d656341676514`, hex.EncodeToString(c.Response().Body()))
	require.Equal(t, "application/cbor", string(c.Response().Header.Peek("content-type")))

	// Test with ctype
	err = c.CBOR(dummyStruct{ // map has no order
		Name: "Grame",
		Age:  20,
	}, "application/problem+cbor")
	require.NoError(t, err)
	require.Equal(t, `a2644e616d65654772616d656341676514`, hex.EncodeToString(c.Response().Body()))
	require.Equal(t, "application/problem+cbor", string(c.Response().Header.Peek("content-type")))

	testEmpty := func(v any, r string) {
		cbErr := c.CBOR(v)
		require.NoError(t, cbErr)
		require.Equal(t, r, hex.EncodeToString(c.Response().Body()))
	}

	testEmpty(nil, "f6")
	testEmpty("", `60`)
	testEmpty(0, "00")
	testEmpty([]int{}, "80")

	// Test invalid types
	err = c.CBOR(make(chan int))
	require.Error(t, err)

	err = c.CBOR(func() {})
	require.Error(t, err)

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

		app := New(Config{
			CBOREncoder: func(_ any) ([]byte, error) {
				return []byte(hex.EncodeToString([]byte("random"))), nil
			},
		})
		c := app.AcquireCtx(&fasthttp.RequestCtx{})

		err := c.CBOR(Map{ // map has no order
			"Name": "Grame",
			"Age":  20,
		})
		require.NoError(t, err)
		require.Equal(t, `72616e646f6d`, string(c.Response().Body()))
		require.Equal(t, "application/cbor", string(c.Response().Header.Peek("content-type")))
	})
}

Domain

Subdomains

Defined In

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free