Skip to content

Commit 2bd347d

Browse files
committed
test: add testcases for delayed streaming responses
1 parent d786d45 commit 2bd347d

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

functionurl_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
"io"
1414
"net/http"
1515
"reflect"
16+
"strings"
1617
"testing"
18+
"time"
1719
)
1820

1921
func newFunctionURLRequest() events.LambdaFunctionURLRequest {
@@ -77,6 +79,21 @@ func newVanillaPanicAdapter() handler.AdapterFunc {
7779
return adapter.NewVanillaAdapter(mux)
7880
}
7981

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+
8097
func newEchoAdapter() handler.AdapterFunc {
8198
app := echo.New()
8299
app.Any("*", func(c echo.Context) error {
@@ -108,6 +125,25 @@ func newEchoPanicAdapter() handler.AdapterFunc {
108125
return adapter.NewEchoAdapter(app)
109126
}
110127

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+
111147
func newFiberAdapter() handler.AdapterFunc {
112148
app := fiber.New()
113149
app.All("*", func(ctx *fiber.Ctx) error {
@@ -132,6 +168,27 @@ func newFiberPanicAdapter() handler.AdapterFunc {
132168
return adapter.NewFiberAdapter(app)
133169
}
134170

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+
135192
type extractor[T any] interface {
136193
StatusCode(T) int
137194
Headers(T) map[string]string
@@ -281,3 +338,54 @@ func runTestFunctionURLPanicAndRecover[T any](t *testing.T, h func(context.Conte
281338
t.Error("expected to receive error 'panic from test'")
282339
}
283340
}
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

Comments
 (0)