Home / Function/ Test_Ctx_AutoFormat_Struct() — fiber Function Reference

Test_Ctx_AutoFormat_Struct() — fiber Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

ctx_test.go lines 1951–2011

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

	type Message struct {
		Sender     string `xml:"sender,attr"`
		Recipients []string
		Urgency    int `xml:"urgency,attr"`
	}
	data := Message{
		Recipients: []string{"Alice", "Bob"},
		Sender:     "Carol",
		Urgency:    3,
	}

	c.Request().Header.Set(HeaderAccept, MIMEApplicationJSON)
	err := c.AutoFormat(data)
	require.NoError(t, err)
	require.Equal(t, MIMEApplicationJSONCharsetUTF8, c.GetRespHeader(HeaderContentType)) //nolint:testifylint // this is comparing content-type headers, not JSON content
	require.JSONEq(t,
		`{"Sender":"Carol","Recipients":["Alice","Bob"],"Urgency":3}`,
		string(c.Response().Body()),
	)

	c.Request().Header.Set(HeaderAccept, MIMEApplicationMsgPack)
	err = c.AutoFormat(data)
	require.NoError(t, err)

	require.Equal(t, MIMEApplicationMsgPack, c.GetRespHeader(HeaderContentType))
	require.Equal(t, []byte{
		// {"Sender":"Carol","Recipients":["Alice","Bob"],"Urgency":3}
		0x83, 0xa6, 0x53, 0x65, 0x6e, 0x64, 0x65, 0x72, 0xa5, 0x43, 0x61,
		0x72, 0x6f, 0x6c, 0xaa, 0x52, 0x65, 0x63, 0x69, 0x70, 0x69, 0x65,
		0x6e, 0x74, 0x73, 0x92, 0xa5, 0x41, 0x6c, 0x69, 0x63, 0x65, 0xa3,
		0x42, 0x6f, 0x62, 0xa7, 0x55, 0x72, 0x67, 0x65, 0x6e, 0x63, 0x79,
		0x3,
	},
		c.Response().Body())

	c.Request().Header.Set(HeaderAccept, MIMEApplicationCBOR)
	err = c.AutoFormat(data)
	require.NoError(t, err)
	require.Equal(t, MIMEApplicationCBOR, c.GetRespHeader(HeaderContentType))
	require.Equal(t, "a36653656e646572654361726f6c6a526563697069656e74738265416c69636563426f6267557267656e637903",
		hex.EncodeToString(c.Response().Body()))

	c.Request().Header.Set(HeaderAccept, MIMEApplicationXML)
	err = c.AutoFormat(data)
	require.NoError(t, err)
	require.Equal(t, MIMEApplicationXMLCharsetUTF8, c.GetRespHeader(HeaderContentType))
	require.Equal(t,
		`<Message sender="Carol" urgency="3"><Recipients>Alice</Recipients><Recipients>Bob</Recipients></Message>`,
		string(c.Response().Body()),
	)
}

Domain

Subdomains

Defined In

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free