-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexpress.openapi-servers.unit.tests.js
More file actions
85 lines (77 loc) · 3.57 KB
/
express.openapi-servers.unit.tests.js
File metadata and controls
85 lines (77 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { jest, describe, test, expect, beforeEach } from '@jest/globals';
/**
* Unit tests for computeOpenApiServerUrl — pure function, no Express/config side-effects.
* express.js has transitive deps (logger, config) so we mock all module-level deps
* before importing to isolate the pure computation.
*/
describe('lib/services/express — OpenAPI servers.url derivation:', () => {
beforeEach(() => jest.resetModules());
const getComputeOpenApiServerUrl = async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: {
domain: 'http://localhost:3000',
swagger: { enable: false },
app: { title: 'test', description: 'd' },
files: { swagger: [], guides: [], routes: [], configs: [], policies: [], preRoutes: [] },
bodyParser: {},
cors: { origin: [], credentials: false },
csrf: {},
posthog: {},
analytics: {},
trust: {},
log: { fileLogger: { directoryPath: '/tmp', fileName: 'test.log' } },
},
}));
jest.unstable_mockModule('../../../lib/services/logger.js', () => ({
default: {
warn: jest.fn(),
error: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
getLogFormat: jest.fn().mockReturnValue('dev'),
getMorganOptions: jest.fn().mockReturnValue({}),
},
}));
jest.unstable_mockModule('../../../lib/services/errorTracker.js', () => ({
default: { setupExpressErrorHandler: jest.fn(), init: jest.fn() },
}));
jest.unstable_mockModule('../../../lib/services/analytics.js', () => ({
default: { init: jest.fn().mockResolvedValue(undefined), isConfigured: jest.fn().mockReturnValue(false) },
}));
jest.unstable_mockModule('../../../lib/helpers/guides.js', () => ({
default: { loadGuides: jest.fn().mockReturnValue([]), mergeGuidesIntoSpec: jest.fn() },
}));
jest.unstable_mockModule('../../../lib/middlewares/requestId.js', () => ({ default: jest.fn() }));
jest.unstable_mockModule('../../../lib/middlewares/analytics.js', () => ({ default: jest.fn() }));
jest.unstable_mockModule('../../../lib/middlewares/policy.js', () => ({
default: { discoverPolicies: jest.fn().mockResolvedValue(undefined) },
}));
jest.unstable_mockModule('../../../lib/middlewares/posthog-context.middleware.js', () => ({
posthogContextMiddleware: jest.fn(),
}));
const mod = await import('../../../lib/services/express.js');
return mod.computeOpenApiServerUrl;
};
test('keeps full URL when domain already has http:// scheme', async () => {
const fn = await getComputeOpenApiServerUrl();
expect(fn('http://localhost:3000')).toBe('http://localhost:3000');
});
test('keeps full URL when domain already has https:// scheme with custom subdomain', async () => {
const fn = await getComputeOpenApiServerUrl();
expect(fn('https://app.example.com')).toBe('https://app.example.com');
});
test('prepends https://api. when domain is a bare host (no scheme)', async () => {
const fn = await getComputeOpenApiServerUrl();
expect(fn('trawl.me')).toBe('https://api.trawl.me');
});
test('prepends https://api. for any bare host', async () => {
const fn = await getComputeOpenApiServerUrl();
expect(fn('example.com')).toBe('https://api.example.com');
});
test('falls back to http://localhost:3000 when domain is falsy', async () => {
const fn = await getComputeOpenApiServerUrl();
expect(fn('')).toBe('http://localhost:3000');
expect(fn(null)).toBe('http://localhost:3000');
expect(fn(undefined)).toBe('http://localhost:3000');
});
});