@@ -11,14 +11,15 @@ import (
11
11
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
12
12
"github.com/labstack/echo/v4"
13
13
"io"
14
+ "net"
14
15
"net/http"
15
16
"reflect"
16
17
"strings"
17
18
"testing"
18
19
"time"
19
20
)
20
21
21
- func newFunctionURLRequest () events.LambdaFunctionURLRequest {
22
+ func newFunctionURLRequest (sourceIp string ) events.LambdaFunctionURLRequest {
22
23
return events.LambdaFunctionURLRequest {
23
24
Version : "2.0" ,
24
25
RawPath : "/example" ,
@@ -39,7 +40,7 @@ func newFunctionURLRequest() events.LambdaFunctionURLRequest {
39
40
Method : "POST" ,
40
41
Path : "/example" ,
41
42
Protocol : "HTTP/1.1" ,
42
- SourceIP : "127.0.0.1" ,
43
+ SourceIP : sourceIp ,
43
44
UserAgent : "Go-http-client/1.1" ,
44
45
},
45
46
},
@@ -150,7 +151,7 @@ func newFiberAdapter() handler.AdapterFunc {
150
151
result := make (map [string ]string )
151
152
result ["Method" ] = ctx .Method ()
152
153
result ["URL" ] = ctx .Request ().URI ().String ()
153
- result ["RemoteAddr" ] = ctx .IP () + ": http" // fiber uses net.ResolveTCPAddr which resolves :http to :80
154
+ result ["RemoteAddr" ] = net . JoinHostPort ( ctx .IP (), " http") // fiber uses net.ResolveTCPAddr which resolves :http to :80
154
155
result ["Body" ] = string (ctx .Body ())
155
156
156
157
return ctx .JSON (result )
@@ -262,38 +263,47 @@ func TestFunctionURLPOST(t *testing.T) {
262
263
}
263
264
264
265
func runTestFunctionURLPOST [T any ](t * testing.T , h func (context.Context , events.LambdaFunctionURLRequest ) (T , error ), ex extractor [T ]) {
265
- req := newFunctionURLRequest ()
266
- res , err := h (context .Background (), req )
267
- if err != nil {
268
- t .Error (err )
266
+ sourceIps := map [string ][2 ]string {
267
+ "ipv4" : {"127.0.0.1" , "127.0.0.1:http" },
268
+ "ipv6" : {"::1" , "[::1]:http" },
269
269
}
270
270
271
- if ex .StatusCode (res ) != http .StatusOK {
272
- t .Error ("expected status to be 200" )
273
- }
274
-
275
- if ex .Headers (res )["Content-Type" ] != "application/json" {
276
- t .Error ("expected Content-Type to be application/json" )
277
- }
278
-
279
- if ex .IsBase64Encoded (res ) {
280
- t .Error ("expected body not to be base64 encoded" )
281
- }
282
-
283
- body := make (map [string ]string )
284
- _ = json .Unmarshal ([]byte (ex .Body (res )), & body )
285
-
286
- expectedBody := map [string ]string {
287
- "Method" : "POST" ,
288
- "URL" : "https://0dhg9709da0dhg9709da0dhg9709da.lambda-url.eu-central-1.on.aws/example?key=value" ,
289
- "RemoteAddr" : "127.0.0.1:http" ,
290
- "Body" : "hello world" ,
291
- }
292
-
293
- if ! reflect .DeepEqual (body , expectedBody ) {
294
- t .Logf ("expected: %v" , expectedBody )
295
- t .Logf ("actual: %v" , body )
296
- t .Error ("request/response didnt match" )
271
+ for name , inputPair := range sourceIps {
272
+ t .Run (name , func (t * testing.T ) {
273
+ req := newFunctionURLRequest (inputPair [0 ])
274
+ res , err := h (context .Background (), req )
275
+ if err != nil {
276
+ t .Error (err )
277
+ }
278
+
279
+ if ex .StatusCode (res ) != http .StatusOK {
280
+ t .Error ("expected status to be 200" )
281
+ }
282
+
283
+ if ex .Headers (res )["Content-Type" ] != "application/json" {
284
+ t .Error ("expected Content-Type to be application/json" )
285
+ }
286
+
287
+ if ex .IsBase64Encoded (res ) {
288
+ t .Error ("expected body not to be base64 encoded" )
289
+ }
290
+
291
+ body := make (map [string ]string )
292
+ _ = json .Unmarshal ([]byte (ex .Body (res )), & body )
293
+
294
+ expectedBody := map [string ]string {
295
+ "Method" : "POST" ,
296
+ "URL" : "https://0dhg9709da0dhg9709da0dhg9709da.lambda-url.eu-central-1.on.aws/example?key=value" ,
297
+ "RemoteAddr" : inputPair [1 ],
298
+ "Body" : "hello world" ,
299
+ }
300
+
301
+ if ! reflect .DeepEqual (body , expectedBody ) {
302
+ t .Logf ("expected: %v" , expectedBody )
303
+ t .Logf ("actual: %v" , body )
304
+ t .Error ("request/response didnt match" )
305
+ }
306
+ })
297
307
}
298
308
}
299
309
@@ -328,7 +338,7 @@ func TestFunctionURLWithPanicAndRecover(t *testing.T) {
328
338
}
329
339
330
340
func runTestFunctionURLPanicAndRecover [T any ](t * testing.T , h func (context.Context , events.LambdaFunctionURLRequest ) (T , error )) {
331
- req := newFunctionURLRequest ()
341
+ req := newFunctionURLRequest ("127.0.0.1" )
332
342
_ , err := h (context .Background (), req )
333
343
if err == nil {
334
344
t .Error ("expected to receive an error" )
@@ -362,7 +372,7 @@ func TestFunctionURLDelayed(t *testing.T) {
362
372
}
363
373
364
374
func runTestFunctionURLDelayed [T any ](t * testing.T , h func (context.Context , events.LambdaFunctionURLRequest ) (T , error ), ex extractor [T ]) {
365
- req := newFunctionURLRequest ()
375
+ req := newFunctionURLRequest ("127.0.0.1" )
366
376
res , err := h (context .Background (), req )
367
377
if err != nil {
368
378
t .Error (err )
0 commit comments