1
1
import type {
2
2
FastifyInstance ,
3
- LightMyRequestResponse ,
4
3
InjectOptions ,
4
+ LightMyRequestResponse ,
5
5
} from 'fastify' ;
6
- import app from '../../src/app' ;
6
+ import type { Config } from '../../src/config' ;
7
+ import { config } from '../../src/config' ;
8
+ import pacRoute from '../../src/routes/pac-route' ;
9
+ import { getProxyUri } from '../../src/utils/get-proxy-uri' ;
7
10
import { StatusCodes } from '../../src/utils/http' ;
8
- import type { AddressInfo } from 'node:net' ;
9
- import { config , type Config } from '../../src/config' ;
10
- import pkg from '../../package.json' ;
11
+ import { buildFastifyInstance } from '../__helpers__/fastify' ;
11
12
12
13
jest . mock ( '../../src/config' ) ;
14
+ jest . mock ( '../../src/utils/get-proxy-uri' ) ;
13
15
14
- const addressInfo : AddressInfo = {
15
- address : '127.0.0.1' ,
16
- port : 3000 ,
17
- family : 'IPv4' ,
18
- } ;
19
-
16
+ const host : string = '127.0.0.1:3000' ;
17
+ /**
18
+ * Default request arguments for the PAC route
19
+ */
20
20
const defRequestArgs : InjectOptions = {
21
21
method : 'GET' ,
22
22
url : '/proxy.pac' ,
23
23
headers : {
24
- host : ` ${ addressInfo . address } : ${ String ( addressInfo . port ) } ` ,
24
+ host,
25
25
} ,
26
26
} ;
27
27
28
- /**
29
- * Builds a Fastify instance for testing.
30
- *
31
- * @returns The Fastify instance
32
- */
33
- function buildFastifyInstance ( ) : FastifyInstance {
34
- const fastify : FastifyInstance = app ( ) ;
35
-
36
- fastify . server . address = ( ) : AddressInfo => addressInfo ;
37
-
38
- beforeAll ( ( ) => fastify . ready ( ) ) ;
39
- afterAll ( ( ) => fastify . close ( ) ) ;
40
-
41
- return fastify ;
42
- }
43
-
44
28
describe ( 'PAC Route' , ( ) => {
45
- const fastify : FastifyInstance = buildFastifyInstance ( ) ;
46
-
47
- it ( 'returns ProxyMate header' , async ( ) => {
48
- const res : LightMyRequestResponse = await fastify . inject ( {
49
- ...defRequestArgs ,
50
- } ) ;
51
-
52
- expect ( res . headers [ 'x-proxymate' ] ) . toEqual ( pkg . version ) ;
53
- } ) ;
29
+ const fastify : FastifyInstance = buildFastifyInstance ( [ pacRoute ] ) ;
54
30
55
31
it ( 'returns valid PAC file' , async ( ) => {
56
32
const mockedConfig : jest . MockedObjectDeep < Config > = jest . mocked ( config ) ;
33
+ const mockedGetProxyUri : jest . MockedFunction < typeof getProxyUri > =
34
+ jest . mocked ( getProxyUri ) ;
35
+
36
+ mockedGetProxyUri . mockReturnValueOnce ( host ) ;
57
37
58
38
mockedConfig . tld = '.tld' ;
59
39
mockedConfig . routes = {
@@ -62,9 +42,7 @@ describe('PAC Route', () => {
62
42
'docs.test-app' : 'http://localhost:3003' ,
63
43
} ;
64
44
65
- const res : LightMyRequestResponse = await fastify . inject ( {
66
- ...defRequestArgs ,
67
- } ) ;
45
+ const res : LightMyRequestResponse = await fastify . inject ( defRequestArgs ) ;
68
46
69
47
expect ( res . statusCode ) . toEqual ( StatusCodes . Ok ) ;
70
48
expect ( res . headers [ 'content-type' ] ) . toEqual (
@@ -76,17 +54,17 @@ describe('PAC Route', () => {
76
54
// Check each route is properly included in the PAC file
77
55
for ( const route in mockedConfig . routes ) {
78
56
expect ( res . body ) . toContain (
79
- `if (dnsDomainIs(host, "${ route } ${ mockedConfig . tld } ")) return "PROXY ${ addressInfo . address } : ${ String ( addressInfo . port ) } "` ,
57
+ `if (dnsDomainIs(host, "${ route } ${ mockedConfig . tld } ")) return "PROXY ${ host } "` ,
80
58
) ;
81
59
}
82
60
} ) ;
83
61
84
- it ( 'returns 404 status code when host does not match proxy URI' , async ( ) => {
62
+ it ( 'returns 404 status code when host does not match the proxy URI' , async ( ) => {
85
63
const res : LightMyRequestResponse = await fastify . inject ( {
86
64
...defRequestArgs ,
87
65
headers : {
88
66
...defRequestArgs . headers ,
89
- host : '127.0.0.1:9999' ,
67
+ host : '127.0.0.1:9999' , // Invalid host
90
68
} ,
91
69
} ) ;
92
70
0 commit comments