@@ -13,7 +13,9 @@ import (
13
13
"io"
14
14
"net/http"
15
15
"reflect"
16
+ "strings"
16
17
"testing"
18
+ "time"
17
19
)
18
20
19
21
func newFunctionURLRequest () events.LambdaFunctionURLRequest {
@@ -77,6 +79,21 @@ func newVanillaPanicAdapter() handler.AdapterFunc {
77
79
return adapter .NewVanillaAdapter (mux )
78
80
}
79
81
82
+ func newVanillaDelayedAdapter () handler.AdapterFunc {
83
+ mux := http .NewServeMux ()
84
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
85
+ w .Header ().Set ("Content-Type" , "text/plain" )
86
+ w .WriteHeader (http .StatusOK )
87
+
88
+ for i := 0 ; i < 10 ; i ++ {
89
+ _ , _ = w .Write ([]byte ("pong" ))
90
+ time .Sleep (50 * time .Millisecond )
91
+ }
92
+ })
93
+
94
+ return adapter .NewVanillaAdapter (mux )
95
+ }
96
+
80
97
func newEchoAdapter () handler.AdapterFunc {
81
98
app := echo .New ()
82
99
app .Any ("*" , func (c echo.Context ) error {
@@ -108,6 +125,25 @@ func newEchoPanicAdapter() handler.AdapterFunc {
108
125
return adapter .NewEchoAdapter (app )
109
126
}
110
127
128
+ func newEchoDelayedAdapter () handler.AdapterFunc {
129
+ app := echo .New ()
130
+ app .Any ("*" , func (c echo.Context ) error {
131
+ w := c .Response ()
132
+
133
+ w .Header ().Set ("Content-Type" , "text/plain" )
134
+ w .WriteHeader (http .StatusOK )
135
+
136
+ for i := 0 ; i < 10 ; i ++ {
137
+ _ , _ = w .Write ([]byte ("pong" ))
138
+ time .Sleep (50 * time .Millisecond )
139
+ }
140
+
141
+ return nil
142
+ })
143
+
144
+ return adapter .NewEchoAdapter (app )
145
+ }
146
+
111
147
func newFiberAdapter () handler.AdapterFunc {
112
148
app := fiber .New ()
113
149
app .All ("*" , func (ctx * fiber.Ctx ) error {
@@ -132,6 +168,27 @@ func newFiberPanicAdapter() handler.AdapterFunc {
132
168
return adapter .NewFiberAdapter (app )
133
169
}
134
170
171
+ func newFiberDelayedAdapter () handler.AdapterFunc {
172
+ app := fiber .New ()
173
+ app .All ("*" , func (ctx * fiber.Ctx ) error {
174
+ w := ctx .Response ()
175
+
176
+ w .Header .Set ("Content-Type" , "text/plain" )
177
+ w .Header .SetStatusCode (http .StatusOK )
178
+
179
+ bw := w .BodyWriter ()
180
+
181
+ for i := 0 ; i < 10 ; i ++ {
182
+ _ , _ = bw .Write ([]byte ("pong" ))
183
+ time .Sleep (50 * time .Millisecond )
184
+ }
185
+
186
+ return nil
187
+ })
188
+
189
+ return adapter .NewFiberAdapter (app )
190
+ }
191
+
135
192
type extractor [T any ] interface {
136
193
StatusCode (T ) int
137
194
Headers (T ) map [string ]string
@@ -281,3 +338,54 @@ func runTestFunctionURLPanicAndRecover[T any](t *testing.T, h func(context.Conte
281
338
t .Error ("expected to receive error 'panic from test'" )
282
339
}
283
340
}
341
+
342
+ func TestFunctionURLDelayed (t * testing.T ) {
343
+ adapters := map [string ]handler.AdapterFunc {
344
+ "vanilla" : newVanillaDelayedAdapter (),
345
+ "echo" : newEchoDelayedAdapter (),
346
+ "fiber" : newFiberDelayedAdapter (),
347
+ }
348
+
349
+ for name , a := range adapters {
350
+ t .Run (name , func (t * testing.T ) {
351
+ t .Run ("normal" , func (t * testing.T ) {
352
+ h := handler .NewFunctionURLHandler (a )
353
+ runTestFunctionURLDelayed [events.LambdaFunctionURLResponse ](t , h , extractorNormal {})
354
+ })
355
+
356
+ t .Run ("streaming" , func (t * testing.T ) {
357
+ h := handler .NewFunctionURLStreamingHandler (a )
358
+ runTestFunctionURLDelayed [* events.LambdaFunctionURLStreamingResponse ](t , h , extractorStreaming {})
359
+ })
360
+ })
361
+ }
362
+ }
363
+
364
+ func runTestFunctionURLDelayed [T any ](t * testing.T , h func (context.Context , events.LambdaFunctionURLRequest ) (T , error ), ex extractor [T ]) {
365
+ req := newFunctionURLRequest ()
366
+ res , err := h (context .Background (), req )
367
+ if err != nil {
368
+ t .Error (err )
369
+ }
370
+
371
+ if ex .StatusCode (res ) != http .StatusOK {
372
+ t .Error ("expected status to be 200" )
373
+ }
374
+
375
+ if ex .Headers (res )["Content-Type" ] != "text/plain" {
376
+ t .Error ("expected Content-Type to be text/plain" )
377
+ }
378
+
379
+ if ex .IsBase64Encoded (res ) {
380
+ t .Error ("expected body not to be base64 encoded" )
381
+ }
382
+
383
+ body := ex .Body (res )
384
+ expectedBody := strings .Repeat ("pong" , 10 )
385
+
386
+ if body != expectedBody {
387
+ t .Logf ("expected: %v" , expectedBody )
388
+ t .Logf ("actual: %v" , body )
389
+ t .Error ("request/response didnt match" )
390
+ }
391
+ }
0 commit comments