MCPcopy
hub / github.com/gofiber/fiber / Test_Bind_RepeatParserWithSameStruct

Function Test_Bind_RepeatParserWithSameStruct

bind_test.go:2573–2628  ·  view source on GitHub ↗

go test -run Test_Bind_RepeatParserWithSameStruct -v

(t *testing.T)

Source from the content-addressed store, hash-verified

2571
2572// go test -run Test_Bind_RepeatParserWithSameStruct -v
2573func Test_Bind_RepeatParserWithSameStruct(t *testing.T) {
2574 t.Parallel()
2575 app := New(Config{
2576 CBOREncoder: cbor.Marshal,
2577 CBORDecoder: cbor.Unmarshal,
2578 })
2579 c := app.AcquireCtx(&fasthttp.RequestCtx{})
2580 defer app.ReleaseCtx(c)
2581
2582 type Request struct {
2583 QueryParam string `query:"query_param"`
2584 HeaderParam string `header:"header_param"`
2585 BodyParam string `json:"body_param" xml:"body_param" form:"body_param"`
2586 }
2587
2588 r := new(Request)
2589
2590 c.Request().URI().SetQueryString("query_param=query_param")
2591 require.NoError(t, c.Bind().Query(r))
2592 require.Equal(t, "query_param", r.QueryParam)
2593
2594 c.Request().Header.Add("header_param", "header_param")
2595 require.NoError(t, c.Bind().Header(r))
2596 require.Equal(t, "header_param", r.HeaderParam)
2597
2598 var gzipJSON bytes.Buffer
2599 w := gzip.NewWriter(&gzipJSON)
2600 _, err := w.Write([]byte(`{"body_param":"body_param"}`))
2601 require.NoError(t, err)
2602 err = w.Close()
2603 require.NoError(t, err)
2604 c.Request().Header.SetContentType(MIMEApplicationJSON)
2605 c.Request().Header.Set(HeaderContentEncoding, "gzip")
2606 c.Request().SetBody(gzipJSON.Bytes())
2607 c.Request().Header.SetContentLength(len(gzipJSON.Bytes()))
2608 require.NoError(t, c.Bind().Body(r))
2609 require.Equal(t, "body_param", r.BodyParam)
2610 c.Request().Header.Del(HeaderContentEncoding)
2611
2612 testDecodeParser := func(contentType, body string) {
2613 c.Request().Header.SetContentType(contentType)
2614 c.Request().SetBody([]byte(body))
2615 c.Request().Header.SetContentLength(len(body))
2616 require.NoError(t, c.Bind().Body(r))
2617 require.Equal(t, "body_param", r.BodyParam)
2618 }
2619
2620 cb, err := cbor.Marshal(&Request{BodyParam: "body_param"})
2621 require.NoError(t, err, "Failed to marshal CBOR data")
2622
2623 testDecodeParser(MIMEApplicationJSON, `{"body_param":"body_param"}`)
2624 testDecodeParser(MIMEApplicationXML, `<Demo><body_param>body_param</body_param></Demo>`)
2625 testDecodeParser(MIMEApplicationCBOR, string(cb))
2626 testDecodeParser(MIMEApplicationForm, "body_param=body_param")
2627 testDecodeParser(MIMEMultipartForm+`;boundary="b"`, "--b\r\nContent-Disposition: form-data; name=\"body_param\"\r\n\r\nbody_param\r\n--b--")
2628}
2629
2630type RequestConfig struct {

Callers

nothing calls this directly

Calls 15

BytesMethod · 0.95
AcquireCtxMethod · 0.80
ReleaseCtxMethod · 0.80
URIMethod · 0.80
NewFunction · 0.70
RequestMethod · 0.65
QueryMethod · 0.65
BindMethod · 0.65
AddMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected