@@ -10,6 +10,7 @@ import {
1010 eventHandler ,
1111 getMethod ,
1212 getQuery ,
13+ getRequestURL ,
1314} from "../src" ;
1415
1516describe ( "" , ( ) => {
@@ -93,6 +94,46 @@ describe("", () => {
9394 } ) ;
9495 } ) ;
9596
97+ describe ( "getRequestURL" , ( ) => {
98+ const tests = [
99+ { path : "/foo" , url : "http://127.0.0.1/foo" } ,
100+ { path : "/test" , host : "example.com" , url : "http://example.com/test" } ,
101+ {
102+ path : "/test" ,
103+ headers : [ [ "x-forwarded-proto" , "https" ] ] ,
104+ url : "https://127.0.0.1:80/test" ,
105+ } ,
106+ {
107+ path : "/test" ,
108+ headers : [ [ "x-forwarded-host" , "example.com" ] ] ,
109+ url : "http://example.com/test" ,
110+ } ,
111+ ] ;
112+ for ( const test of tests ) {
113+ it ( "getRequestURL: " + JSON . stringify ( test ) , async ( ) => {
114+ app . use (
115+ "/" ,
116+ eventHandler ( ( event ) => {
117+ const url = getRequestURL ( event ) ;
118+ // @ts -ignore
119+ url . port = 80 ;
120+ return url ;
121+ } )
122+ ) ;
123+ const req = request . get ( test . path ) ;
124+ if ( test . host ) {
125+ req . set ( "Host" , test . host ) ;
126+ }
127+ if ( test . headers ) {
128+ for ( const header of test . headers ) {
129+ req . set ( header [ 0 ] , header [ 1 ] ) ;
130+ }
131+ }
132+ expect ( ( await req ) . text ) . toBe ( JSON . stringify ( test . url ) ) ;
133+ } ) ;
134+ }
135+ } ) ;
136+
96137 describe ( "assertMethod" , ( ) => {
97138 it ( "only allow head and post" , async ( ) => {
98139 app . use (
0 commit comments