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

Function Test_Logger_ByteSent_Streaming

middleware/logger/logger_test.go:1245–1284  ·  view source on GitHub ↗

go test -run Test_Logger_ByteSent_Streaming

(t *testing.T)

Source from the content-addressed store, hash-verified

1243
1244// go test -run Test_Logger_ByteSent_Streaming
1245func Test_Logger_ByteSent_Streaming(t *testing.T) {
1246 t.Parallel()
1247 app := fiber.New()
1248
1249 buf := bytebufferpool.Get()
1250 defer bytebufferpool.Put(buf)
1251
1252 app.Use(New(Config{
1253 Format: "${bytesReceived} ${bytesSent} ${status}",
1254 Stream: buf,
1255 }))
1256
1257 app.Get("/", func(c fiber.Ctx) error {
1258 c.Set("Connection", "keep-alive")
1259 c.Set("Transfer-Encoding", "chunked")
1260 c.RequestCtx().SetBodyStreamWriter(func(w *bufio.Writer) {
1261 var i int
1262 for {
1263 i++
1264 msg := fmt.Sprintf("%d - the time is %v", i, time.Now())
1265 fmt.Fprintf(w, "data: Message: %s\n\n", msg)
1266 err := w.Flush()
1267 if err != nil {
1268 break
1269 }
1270 if i == 10 {
1271 break
1272 }
1273 }
1274 })
1275 return nil
1276 })
1277
1278 resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
1279 require.NoError(t, err)
1280 require.Equal(t, fiber.StatusOK, resp.StatusCode)
1281
1282 // -2 means identity, -1 means chunked, 200 status
1283 require.Equal(t, "-2 -1 200", buf.String())
1284}
1285
1286type fakeOutput int
1287

Callers

nothing calls this directly

Calls 11

TestMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
GetMethod · 0.65
PutMethod · 0.65
UseMethod · 0.65
SetMethod · 0.65
RequestCtxMethod · 0.65
StringMethod · 0.65
NowMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected