11import * as t from "tap" ;
2+ import { Hostname } from "../../vulnerabilities/ssrf/Hostname" ;
23import { getHostnameAndPortFromArgs as get } from "./getHostnameAndPortFromArgs" ;
34import { parse as parseUrl } from "url" ;
45
56t . test ( "it works with url string" , async ( t ) => {
67 t . same ( get ( [ "http://localhost:4000" ] ) , {
7- hostname : "localhost" ,
8+ hostname : Hostname . fromString ( "localhost" ) ,
89 port : 4000 ,
910 } ) ;
1011 t . same ( get ( [ "http://localhost?test=1" ] ) , {
11- hostname : "localhost" ,
12+ hostname : Hostname . fromString ( "localhost" ) ,
1213 port : 80 ,
1314 } ) ;
1415 t . same ( get ( [ "https://localhost" ] ) , {
15- hostname : "localhost" ,
16+ hostname : Hostname . fromString ( "localhost" ) ,
1617 port : 443 ,
1718 } ) ;
1819} ) ;
1920
2021t . test ( "it works with url object" , async ( t ) => {
2122 t . same ( get ( [ new URL ( "http://localhost:4000" ) ] ) , {
22- hostname : "localhost" ,
23+ hostname : Hostname . fromString ( "localhost" ) ,
2324 port : 4000 ,
2425 } ) ;
2526 t . same ( get ( [ new URL ( "http://localhost?test=1" ) ] ) , {
26- hostname : "localhost" ,
27+ hostname : Hostname . fromString ( "localhost" ) ,
2728 port : 80 ,
2829 } ) ;
2930 t . same ( get ( [ new URL ( "https://localhost" ) ] ) , {
30- hostname : "localhost" ,
31+ hostname : Hostname . fromString ( "localhost" ) ,
3132 port : 443 ,
3233 } ) ;
3334} ) ;
3435
3536t . test ( "it works with an array of strings" , async ( t ) => {
3637 t . same ( get ( [ [ "http://localhost:4000" ] ] ) , {
37- hostname : "localhost" ,
38+ hostname : Hostname . fromString ( "localhost" ) ,
3839 port : 4000 ,
3940 } ) ;
4041 t . same ( get ( [ [ "http://localhost?test=1" ] ] ) , {
41- hostname : "localhost" ,
42+ hostname : Hostname . fromString ( "localhost" ) ,
4243 port : 80 ,
4344 } ) ;
4445 t . same ( get ( [ [ "https://localhost" ] ] ) , {
45- hostname : "localhost" ,
46+ hostname : Hostname . fromString ( "localhost" ) ,
4647 port : 443 ,
4748 } ) ;
4849} ) ;
4950
5051t . test ( "it works with an legacy url object" , async ( t ) => {
5152 t . same ( get ( [ parseUrl ( "http://localhost:4000" ) ] ) , {
52- hostname : "localhost" ,
53+ hostname : Hostname . fromString ( "localhost" ) ,
5354 port : 4000 ,
5455 } ) ;
5556 t . same ( get ( [ parseUrl ( "http://localhost?test=1" ) ] ) , {
56- hostname : "localhost" ,
57+ hostname : Hostname . fromString ( "localhost" ) ,
5758 port : 80 ,
5859 } ) ;
5960 t . same ( get ( [ parseUrl ( "https://localhost" ) ] ) , {
60- hostname : "localhost" ,
61+ hostname : Hostname . fromString ( "localhost" ) ,
6162 port : 443 ,
6263 } ) ;
6364} ) ;
6465
6566t . test ( "it works with an options object containing origin" , async ( t ) => {
6667 t . same ( get ( [ { origin : "http://localhost:4000" } ] ) , {
67- hostname : "localhost" ,
68+ hostname : Hostname . fromString ( "localhost" ) ,
6869 port : 4000 ,
6970 } ) ;
7071 t . same ( get ( [ { origin : "http://localhost?test=1" } ] ) , {
71- hostname : "localhost" ,
72+ hostname : Hostname . fromString ( "localhost" ) ,
7273 port : 80 ,
7374 } ) ;
7475 t . same ( get ( [ { origin : "https://localhost" } ] ) , {
75- hostname : "localhost" ,
76+ hostname : Hostname . fromString ( "localhost" ) ,
7677 port : 443 ,
7778 } ) ;
7879} ) ;
@@ -81,15 +82,15 @@ t.test(
8182 "it works with an options object containing protocol, hostname and port" ,
8283 async ( t ) => {
8384 t . same ( get ( [ { protocol : "http:" , hostname : "localhost" , port : 4000 } ] ) , {
84- hostname : "localhost" ,
85+ hostname : Hostname . fromString ( "localhost" ) ,
8586 port : 4000 ,
8687 } ) ;
8788 t . same ( get ( [ { hostname : "localhost" , port : 4000 } ] ) , {
88- hostname : "localhost" ,
89+ hostname : Hostname . fromString ( "localhost" ) ,
8990 port : 4000 ,
9091 } ) ;
9192 t . same ( get ( [ { protocol : "https:" , hostname : "localhost" } ] ) , {
92- hostname : "localhost" ,
93+ hostname : Hostname . fromString ( "localhost" ) ,
9394 port : 443 ,
9495 } ) ;
9596 }
@@ -104,3 +105,11 @@ t.test("without hostname", async (t) => {
104105 t . same ( get ( [ { } ] ) , undefined ) ;
105106 t . same ( get ( [ { protocol : "https:" , port : 4000 } ] ) , undefined ) ;
106107} ) ;
108+
109+ t . test ( "invalid hostname" , async ( t ) => {
110+ t . same ( get ( [ { protocol : "https:" , hostname : " " } ] ) , undefined ) ;
111+ } ) ;
112+
113+ t . test ( "empty args" , async ( t ) => {
114+ t . same ( get ( [ ] ) , undefined ) ;
115+ } ) ;
0 commit comments