Skip to content

Commit 8efee48

Browse files
committed
test(serve): add test for redoc server
1 parent dbf2820 commit 8efee48

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

packages/serve/src/lib/document-server/redocDocumentServer/redocDocumentServer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { DocumentServer } from '@vulcan-sql/serve/models';
1414
import * as fs from 'fs';
1515
import * as path from 'path';
1616
import { inject } from 'inversify';
17-
import { v4 } from 'uuid';
1817

1918
@VulcanInternalExtension('redoc')
2019
@VulcanExtensionId(DocumentServerType.redoc)
@@ -63,6 +62,7 @@ export class RedocDocumentServer extends DocumentServer {
6362
this.router.get(bundleFileUrl, async (ctx, next) => {
6463
await next();
6564
ctx.response.body = fs.createReadStream(redocPath);
65+
ctx.set('Content-Type', 'application/javascript');
6666
});
6767

6868
// Load template and render it

packages/serve/src/lib/middleware/docServerMiddleware.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class DocServerMiddleware extends BuiltInMiddleware {
3030
}
3131
}
3232

33-
public override async activate(): Promise<void> {
33+
public override async onActivate(): Promise<void> {
3434
for (const serve of this.servers) await serve.activate();
3535
}
3636

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { DocumentOptions, ProjectOptions } from '@vulcan-sql/core';
2+
import { RedocDocumentServer } from '../../src/lib/document-server';
3+
import * as Koa from 'koa';
4+
import * as supertest from 'supertest';
5+
import faker from '@faker-js/faker';
6+
import { Server } from 'http';
7+
8+
let http: Server;
9+
10+
afterEach(() => {
11+
http?.close();
12+
});
13+
14+
it('Should serve redoc server with all the required contents', async () => {
15+
// Arrange
16+
const server = new RedocDocumentServer(
17+
{},
18+
'',
19+
new DocumentOptions({
20+
folderPath: __dirname,
21+
}),
22+
new ProjectOptions()
23+
);
24+
await server.activate();
25+
const app = new Koa();
26+
http = app.listen(faker.datatype.number({ min: 20000, max: 30000 }));
27+
app.use(server.handle.bind(server));
28+
const agent = supertest.agent(http);
29+
30+
// Act
31+
const doc = await agent.get('/doc');
32+
const redocBundle = await agent.get('/doc/redoc');
33+
const spec = await agent.get('/doc/spec');
34+
35+
// Assert
36+
expect(doc.text.startsWith(`<!DOCTYPE html>`)).toBeTruthy();
37+
expect(
38+
redocBundle.text.startsWith(
39+
`/*! For license information please see redoc.standalone.js.LICENSE.txt */`
40+
)
41+
).toBeTruthy();
42+
expect(spec.body.toString()).toEqual(`name: "123"`);
43+
});
44+
45+
it('Should follow the url path we set', async () => {
46+
// Arrange
47+
const server = new RedocDocumentServer(
48+
{
49+
url: 'some-path-other-than-doc',
50+
},
51+
'',
52+
new DocumentOptions(),
53+
new ProjectOptions()
54+
);
55+
await server.activate();
56+
const app = new Koa();
57+
http = app.listen(faker.datatype.number({ min: 20000, max: 30000 }));
58+
app.use(server.handle.bind(server));
59+
const agent = supertest.agent(http);
60+
61+
// Act
62+
const doc = await agent.get('/some-path-other-than-doc');
63+
const redocBundle = await agent.get('/some-path-other-than-doc/redoc');
64+
65+
// Assert
66+
expect(doc.text.startsWith(`<!DOCTYPE html>`)).toBeTruthy();
67+
expect(
68+
redocBundle.text.startsWith(
69+
`/*! For license information please see redoc.standalone.js.LICENSE.txt */`
70+
)
71+
).toBeTruthy();
72+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name: "123"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { DocumentOptions } from '@vulcan-sql/core';
2+
import { DocServerMiddleware, DocumentServer } from '@vulcan-sql/serve';
3+
import * as compose from 'koa-compose';
4+
import * as sinon from 'ts-sinon';
5+
6+
describe('Test doc server middlewares', () => {
7+
let mockDocServer: Record<string, sinon.StubbedInstance<DocumentServer<any>>>;
8+
let mockOption: DocumentOptions;
9+
const mockDocServerFactory = (name: string) => mockDocServer[name];
10+
11+
beforeEach(() => {
12+
mockDocServer = {
13+
m1: sinon.stubInterface<DocumentServer>(),
14+
m2: sinon.stubInterface<DocumentServer>(),
15+
m3: sinon.stubInterface<DocumentServer>(),
16+
};
17+
mockOption = new DocumentOptions({
18+
server: ['m1', 'm2'],
19+
});
20+
});
21+
22+
it('Should active corresponding document servers', async () => {
23+
// Arrange
24+
const middleware = new DocServerMiddleware(
25+
{},
26+
'',
27+
mockDocServerFactory,
28+
mockOption
29+
);
30+
// Act
31+
await middleware.activate();
32+
// Assert
33+
expect(mockDocServer['m1'].activate?.called).toBeTruthy();
34+
expect(mockDocServer['m2'].activate?.called).toBeTruthy();
35+
expect(mockDocServer['m3'].activate?.called).toBeFalsy();
36+
});
37+
38+
it('Should execute middleware with correct order', async () => {
39+
// mid1 -> mid2(m1 -> m2) -> mid3 -> | next | -> mid3 -> mid2(m2 -> m1) -> mid1
40+
// 1 -> 2 -> 3 -> 4 -> -> 5 -> 6 -> 7 -> 8
41+
// Arrange
42+
const executeOrder: number[] = [];
43+
const mid1 = async (context: any, next: any) => {
44+
executeOrder.push(1);
45+
await next();
46+
executeOrder.push(8);
47+
};
48+
mockDocServer['m1'].handle.callsFake(async (context, next) => {
49+
executeOrder.push(2);
50+
await next();
51+
executeOrder.push(7);
52+
});
53+
mockDocServer['m2'].handle.callsFake(async (context, next) => {
54+
executeOrder.push(3);
55+
await next();
56+
executeOrder.push(6);
57+
});
58+
const mid2 = new DocServerMiddleware(
59+
{},
60+
'',
61+
mockDocServerFactory,
62+
mockOption
63+
);
64+
const mid3 = async (context: any, next: any) => {
65+
executeOrder.push(4);
66+
await next();
67+
executeOrder.push(5);
68+
};
69+
// Act
70+
const execute = compose<any>([mid1, mid2.handle.bind(mid2), mid3]);
71+
await execute({});
72+
// Assert
73+
expect(executeOrder).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
74+
});
75+
});

0 commit comments

Comments
 (0)