@@ -11,14 +11,15 @@ import (
1111	"github.com/its-felix/aws-lambda-go-http-adapter/handler" 
1212	"github.com/labstack/echo/v4" 
1313	"io" 
14+ 	"net" 
1415	"net/http" 
1516	"reflect" 
1617	"strings" 
1718	"testing" 
1819	"time" 
1920)
2021
21- func  newFunctionURLRequest () events.LambdaFunctionURLRequest  {
22+ func  newFunctionURLRequest (sourceIp string ) events.LambdaFunctionURLRequest  {
2223	return  events.LambdaFunctionURLRequest {
2324		Version : "2.0" ,
2425		RawPath : "/example" ,
@@ -39,7 +40,7 @@ func newFunctionURLRequest() events.LambdaFunctionURLRequest {
3940				Method : "POST" ,
4041				Path : "/example" ,
4142				Protocol : "HTTP/1.1" ,
42- 				SourceIP : "127.0.0.1" ,
43+ 				SourceIP : sourceIp ,
4344				UserAgent : "Go-http-client/1.1" ,
4445			},
4546		},
@@ -150,7 +151,7 @@ func newFiberAdapter() handler.AdapterFunc {
150151		result  :=  make (map [string ]string )
151152		result ["Method" ] =  ctx .Method ()
152153		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 
154155		result ["Body" ] =  string (ctx .Body ())
155156
156157		return  ctx .JSON (result )
@@ -262,38 +263,47 @@ func TestFunctionURLPOST(t *testing.T) {
262263}
263264
264265func  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" },
269269	}
270270
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+ 		})
297307	}
298308}
299309
@@ -328,7 +338,7 @@ func TestFunctionURLWithPanicAndRecover(t *testing.T) {
328338}
329339
330340func  runTestFunctionURLPanicAndRecover [T  any ](t  * testing.T , h  func (context.Context , events.LambdaFunctionURLRequest ) (T , error )) {
331- 	req  :=  newFunctionURLRequest ()
341+ 	req  :=  newFunctionURLRequest ("127.0.0.1" )
332342	_ , err  :=  h (context .Background (), req )
333343	if  err  ==  nil  {
334344		t .Error ("expected to receive an error" )
@@ -362,7 +372,7 @@ func TestFunctionURLDelayed(t *testing.T) {
362372}
363373
364374func  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" )
366376	res , err  :=  h (context .Background (), req )
367377	if  err  !=  nil  {
368378		t .Error (err )
0 commit comments