Skip to content

Commit

Permalink
Merge branch 'fix-swagger-options' of https://github.com/joshunrau/sw…
Browse files Browse the repository at this point in the history
…agger into joshunrau-fix-swagger-options
  • Loading branch information
kamilmysliwiec committed Jun 19, 2023
2 parents cf245f9 + c83edb8 commit fb22900
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
65 changes: 65 additions & 0 deletions e2e/express.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,69 @@ describe('Express Swagger', () => {
expect(response.text.length).toBeGreaterThan(0);
});
});

describe('custom swagger options', () => {
const CUSTOM_CSS = 'body { background-color: hotpink !important }';
const CUSTOM_JS = '/foo.js';
const CUSTOM_JS_STR = 'console.log("foo")';
const CUSTOM_FAVICON = '/foo.ico';
const CUSTOM_SITE_TITLE = 'Foo';
const CUSTOM_CSS_URL = '/foo.css';

beforeEach(async () => {
const swaggerDocument = SwaggerModule.createDocument(
app,
builder.build()
);

SwaggerModule.setup('/', app, swaggerDocument, {
customCss: CUSTOM_CSS,
customJs: CUSTOM_JS,
customJsStr: CUSTOM_JS_STR,
customfavIcon: CUSTOM_FAVICON,
customSiteTitle: CUSTOM_SITE_TITLE,
customCssUrl: CUSTOM_CSS_URL
});

await app.init();
});

it('should contain the custom css string', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(CUSTOM_CSS);
});

it('should source the custom js url', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(`script src='${CUSTOM_JS}'></script>`);
});

it('should contain the custom js string', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(CUSTOM_JS_STR);
});

it('should contain the custom favicon', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(
`<link rel='icon' href='${CUSTOM_FAVICON}' />`
);
});

it('should contain the custom site title', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(`<title>${CUSTOM_SITE_TITLE}</title>`);
});

it('should include the custom stylesheet', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(
`<link href='${CUSTOM_CSS_URL}' rel='stylesheet'>`
);
});

afterEach(async () => {
await app.close();
});
});
});
78 changes: 78 additions & 0 deletions e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,82 @@ describe('Fastify Swagger', () => {
expect(response.text.length).toBeGreaterThan(0);
});
});

describe('custom swagger options', () => {
const CUSTOM_CSS = 'body { background-color: hotpink !important }';
const CUSTOM_JS = '/foo.js';
const CUSTOM_JS_STR = 'console.log("foo")';
const CUSTOM_FAVICON = '/foo.ico';
const CUSTOM_SITE_TITLE = 'Foo';
const CUSTOM_CSS_URL = '/foo.css';

beforeEach(async () => {
const swaggerDocument = SwaggerModule.createDocument(
app,
builder.build()
);

SwaggerModule.setup('/custom', app, swaggerDocument, {
customCss: CUSTOM_CSS,
customJs: CUSTOM_JS,
customJsStr: CUSTOM_JS_STR,
customfavIcon: CUSTOM_FAVICON,
customSiteTitle: CUSTOM_SITE_TITLE,
customCssUrl: CUSTOM_CSS_URL
});

await app.init();
await app.getHttpAdapter().getInstance().ready();
});

it('should contain the custom css string', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(CUSTOM_CSS);
});

it('should source the custom js url', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(`script src='${CUSTOM_JS}'></script>`);
});

it('should contain the custom js string', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(CUSTOM_JS_STR);
});

it('should contain the custom favicon', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(
`<link rel='icon' href='${CUSTOM_FAVICON}' />`
);
});

it('should contain the custom site title', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(`<title>${CUSTOM_SITE_TITLE}</title>`);
});

it('should include the custom stylesheet', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(
`<link href='${CUSTOM_CSS_URL}' rel='stylesheet'>`
);
});

afterEach(async () => {
await app.close();
});
});
});
2 changes: 1 addition & 1 deletion lib/swagger-ui/swagger-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function buildSwaggerHTML(
customSiteTitle = 'Swagger UI',
customCssUrl = '',
explorer = false
} = customOptions;
} = { ...customOptions, ...customOptions.swaggerOptions };

const favIconString = customfavIcon
? `<link rel='icon' href='${customfavIcon}' />`
Expand Down

0 comments on commit fb22900

Please sign in to comment.