Skip to content

Commit 0c8477b

Browse files
committed
test: add missing tests for plugins
1 parent 58827aa commit 0c8477b

File tree

4 files changed

+112
-43
lines changed

4 files changed

+112
-43
lines changed

tests/__helpers__/fastify.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type {
2+
FastifyInstance,
3+
FastifyPluginAsync,
4+
FastifyPluginCallback,
5+
} from 'fastify';
6+
import Fastify from 'fastify';
7+
8+
/**
9+
* Builds a Fastify instance for testing.
10+
*
11+
* @param plugins - The plugins to register on the Fastify instance
12+
* @returns The Fastify instance
13+
*/
14+
export function buildFastifyInstance(
15+
plugins: (FastifyPluginAsync | FastifyPluginCallback)[] = [],
16+
): FastifyInstance {
17+
const fastify: FastifyInstance = Fastify();
18+
19+
for (const plugin of plugins) {
20+
fastify.register(plugin);
21+
}
22+
23+
beforeAll(() => fastify.ready());
24+
afterAll(() => fastify.close());
25+
26+
return fastify;
27+
}

tests/plugins/add-app-headers.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { FastifyInstance, LightMyRequestResponse } from 'fastify';
2+
import pkg from '../../package.json';
3+
import { buildFastifyInstance } from '../__helpers__/fastify';
4+
import addAppHeaders from '../../src/plugins/add-app-headers';
5+
6+
describe('Add App Headers Plugin', () => {
7+
const fastify: FastifyInstance = buildFastifyInstance([addAppHeaders]);
8+
9+
it('returns ProxyMate header', async () => {
10+
const res: LightMyRequestResponse = await fastify.inject({
11+
method: 'GET',
12+
url: '/',
13+
});
14+
15+
expect(res.headers['x-proxymate']).toEqual(pkg.version);
16+
});
17+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { FastifyInstance } from 'fastify';
2+
import type { Config, Env } from '../../src/config';
3+
import { config, env } from '../../src/config';
4+
import printRegisteredRoutes from '../../src/plugins/print-registered-routes';
5+
import { buildFastifyInstance } from '../__helpers__/fastify';
6+
7+
jest.mock('../../src/config');
8+
9+
describe('Print Registered Routes Plugin', () => {
10+
const fastify: FastifyInstance = buildFastifyInstance([
11+
printRegisteredRoutes,
12+
]);
13+
14+
it('logs routes on server start', async () => {
15+
const mockedLogInfo: jest.SpyInstance<
16+
void,
17+
[msg: string, ...args: unknown[]],
18+
unknown
19+
> = jest.spyOn(fastify.log, 'info');
20+
const mockedConfig: jest.MockedObjectDeep<Config> = jest.mocked(config);
21+
const mockedEnv: jest.MockedObjectDeep<Env> = jest.mocked(env);
22+
23+
mockedConfig.tld = '.test';
24+
mockedConfig.routes = {
25+
'auth.app': 'http://localhost:3001',
26+
'api.app': 'http://localhost:3002',
27+
};
28+
mockedEnv.PORT = 3000;
29+
30+
await fastify.listen({
31+
port: mockedEnv.PORT,
32+
});
33+
34+
const routesHostnames: string[] = Object.keys(mockedConfig.routes);
35+
36+
// Verify the logs
37+
expect(mockedLogInfo).toHaveBeenCalledWith(
38+
`Proxy listening on port ${String(mockedEnv.PORT)} for domains:`,
39+
);
40+
expect(mockedLogInfo).toHaveBeenCalledWith(
41+
`- ${String(routesHostnames[0])}${String(mockedConfig.tld)}`,
42+
);
43+
expect(mockedLogInfo).toHaveBeenCalledWith(
44+
`- ${String(routesHostnames[1])}${String(mockedConfig.tld)}`,
45+
);
46+
});
47+
});

tests/routes/pac-route.test.ts

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,39 @@
11
import type {
22
FastifyInstance,
3-
LightMyRequestResponse,
43
InjectOptions,
4+
LightMyRequestResponse,
55
} 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';
710
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';
1112

1213
jest.mock('../../src/config');
14+
jest.mock('../../src/utils/get-proxy-uri');
1315

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+
*/
2020
const defRequestArgs: InjectOptions = {
2121
method: 'GET',
2222
url: '/proxy.pac',
2323
headers: {
24-
host: `${addressInfo.address}:${String(addressInfo.port)}`,
24+
host,
2525
},
2626
};
2727

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-
4428
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]);
5430

5531
it('returns valid PAC file', async () => {
5632
const mockedConfig: jest.MockedObjectDeep<Config> = jest.mocked(config);
33+
const mockedGetProxyUri: jest.MockedFunction<typeof getProxyUri> =
34+
jest.mocked(getProxyUri);
35+
36+
mockedGetProxyUri.mockReturnValueOnce(host);
5737

5838
mockedConfig.tld = '.tld';
5939
mockedConfig.routes = {
@@ -62,9 +42,7 @@ describe('PAC Route', () => {
6242
'docs.test-app': 'http://localhost:3003',
6343
};
6444

65-
const res: LightMyRequestResponse = await fastify.inject({
66-
...defRequestArgs,
67-
});
45+
const res: LightMyRequestResponse = await fastify.inject(defRequestArgs);
6846

6947
expect(res.statusCode).toEqual(StatusCodes.Ok);
7048
expect(res.headers['content-type']).toEqual(
@@ -76,17 +54,17 @@ describe('PAC Route', () => {
7654
// Check each route is properly included in the PAC file
7755
for (const route in mockedConfig.routes) {
7856
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}"`,
8058
);
8159
}
8260
});
8361

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 () => {
8563
const res: LightMyRequestResponse = await fastify.inject({
8664
...defRequestArgs,
8765
headers: {
8866
...defRequestArgs.headers,
89-
host: '127.0.0.1:9999',
67+
host: '127.0.0.1:9999', // Invalid host
9068
},
9169
});
9270

0 commit comments

Comments
 (0)