go test -run Test_Ctx_BodyStreamWriter
(t *testing.T)
| 8804 | |
| 8805 | // go test -run Test_Ctx_BodyStreamWriter |
| 8806 | func Test_Ctx_BodyStreamWriter(t *testing.T) { |
| 8807 | t.Parallel() |
| 8808 | ctx := &fasthttp.RequestCtx{} |
| 8809 | |
| 8810 | ctx.SetBodyStreamWriter(func(w *bufio.Writer) { |
| 8811 | fmt.Fprintf(w, "body writer line 1\n") |
| 8812 | if err := w.Flush(); err != nil { |
| 8813 | t.Errorf("unexpected error: %s", err) |
| 8814 | } |
| 8815 | fmt.Fprintf(w, "body writer line 2\n") |
| 8816 | }) |
| 8817 | |
| 8818 | require.True(t, ctx.IsBodyStream()) |
| 8819 | |
| 8820 | s := ctx.Response.String() |
| 8821 | br := bufio.NewReader(bytes.NewBufferString(s)) |
| 8822 | var resp fasthttp.Response |
| 8823 | require.NoError(t, resp.Read(br)) |
| 8824 | |
| 8825 | body := string(resp.Body()) |
| 8826 | expectedBody := "body writer line 1\nbody writer line 2\n" |
| 8827 | require.Equal(t, expectedBody, body) |
| 8828 | } |
| 8829 | |
| 8830 | // go test -v -run=^$ -bench=Benchmark_Ctx_BodyStreamWriter -benchmem -count=4 |
| 8831 | func Benchmark_Ctx_BodyStreamWriter(b *testing.B) { |