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

Function Test_App_DisablePreParseMultipartForm

app_test.go:2825–2871  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2823}
2824
2825func Test_App_DisablePreParseMultipartForm(t *testing.T) {
2826 t.Parallel()
2827 // Must be used with both otherwise there is no point.
2828 testString := "this is a test"
2829
2830 app := New(Config{DisablePreParseMultipartForm: true, StreamRequestBody: true})
2831 app.Post("/", func(c Ctx) error {
2832 req := c.Request()
2833 mpf, err := req.MultipartForm()
2834 if err != nil {
2835 return err
2836 }
2837 if !req.IsBodyStream() {
2838 return errors.New("not a body stream")
2839 }
2840 file, err := mpf.File["test"][0].Open()
2841 if err != nil {
2842 return fmt.Errorf("failed to open: %w", err)
2843 }
2844 buffer := make([]byte, len(testString))
2845 n, err := file.Read(buffer)
2846 if err != nil {
2847 return fmt.Errorf("failed to read: %w", err)
2848 }
2849 if n != len(testString) {
2850 return errors.New("bad read length")
2851 }
2852 return c.Send(buffer)
2853 })
2854 b := &bytes.Buffer{}
2855 w := multipart.NewWriter(b)
2856 writer, err := w.CreateFormFile("test", "test")
2857 require.NoError(t, err, "w.CreateFormFile")
2858 n, err := writer.Write([]byte(testString))
2859 require.NoError(t, err, "writer.Write")
2860 require.Len(t, testString, n, "writer n")
2861 require.NoError(t, w.Close(), "w.Close()")
2862
2863 req := httptest.NewRequest(MethodPost, "/", b)
2864 req.Header.Set("Content-Type", w.FormDataContentType())
2865 resp, err := app.Test(req)
2866 require.NoError(t, err, "app.Test(req)")
2867 body, err := io.ReadAll(resp.Body)
2868 require.NoError(t, err, "io.ReadAll(resp.Body)")
2869
2870 require.Equal(t, testString, string(body))
2871}
2872
2873func Test_App_Test_no_timeout_infinitely(t *testing.T) {
2874 t.Parallel()

Callers

nothing calls this directly

Calls 13

TestMethod · 0.80
NewFunction · 0.70
PostMethod · 0.65
RequestMethod · 0.65
MultipartFormMethod · 0.65
NewMethod · 0.65
ErrorfMethod · 0.65
SendMethod · 0.65
WriteMethod · 0.65
LenMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected