Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
fix(server): beta.13 - fixed handling of missing view in koa and fast…
Browse files Browse the repository at this point in the history
…ify adapters (#193)
  • Loading branch information
serhiisol authored Aug 26, 2023
1 parent 1dc26bd commit d983552
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 8 deletions.
21 changes: 19 additions & 2 deletions server/integration/http/routes/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, HttpStatus } from '@server';
import { Delete, Get, Head, Options, Patch, Post, Put } from '@server/http';
import { Delete, Get, Head, Options, Patch, Post, Put, Render } from '@server/http';

@Controller()
export class AppController {
Expand Down Expand Up @@ -28,7 +28,24 @@ export class AppController {
}

@Put('put', HttpStatus.ACCEPTED)
Put() {
put() {
return 'put';
}

@Get('*', 404)
status404() {
return 'not-found';
}

@Get('render')
@Render('view')
tryRender() {
return { message: 'Hello World' };
}

@Get('render-missing')
@Render('missing')
tryRenderMissing() {
return { message: 'Hello World' };
}
}
21 changes: 21 additions & 0 deletions server/integration/http/routes/src/view.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta
http-equiv="X-UA-Compatible"
content="IE=edge"
>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
<title>POST</title>
</head>

<body>
<%= message %>
</body>

</html>
30 changes: 30 additions & 0 deletions server/integration/http/routes/test/express.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Application, HttpStatus, Module } from '@server';
import { ExpressAdapter } from '@server/express';
import { HttpModule } from '@server/http';
import { join } from 'path';
import * as request from 'supertest';

import { AppModule } from '../src/app.module';
Expand All @@ -21,6 +22,9 @@ describe('Express :: Routes', () => {
app = await Application.create(TestModule);
module = await app.inject<HttpModule>(HttpModule);

module.set('view engine', 'ejs');
module.set('views', join(__dirname, '..', 'src'));

await module.listen();
});

Expand Down Expand Up @@ -67,4 +71,30 @@ describe('Express :: Routes', () => {
.put('/put')
.expect(HttpStatus.ACCEPTED, 'put');
});

it('registers `*` request', async () => {
return request(module.getHttpServer())
.get('/does-not-exist')
.expect(HttpStatus.NOT_FOUND, 'not-found');
});

describe('Render', () => {
it('renders view', async () => {
return request(module.getHttpServer())
.get('/render')
.expect((req) => {
expect(req.status).toBe(HttpStatus.OK);
expect(req.text).toContain('Hello World');
});
});

it('renders non-existing view', async () => {
return request(module.getHttpServer())
.get('/render-missing')
.expect((req) => {
expect(req.status).toBe(HttpStatus.INTERNAL_SERVER_ERROR);
expect(req.body.message).toBeDefined();
});
});
});
});
35 changes: 35 additions & 0 deletions server/integration/http/routes/test/fastify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as FastifyView from '@fastify/view';
import { Application, HttpStatus, Module } from '@server';
import { FastifyAdapter } from '@server/fastify';
import { HttpModule } from '@server/http';
import { join } from 'path';
import * as request from 'supertest';

import { AppModule } from '../src/app.module';
Expand All @@ -21,6 +23,13 @@ describe('Fastify :: Routes', () => {
app = await Application.create(TestModule);
module = await app.inject<HttpModule>(HttpModule);

module.use(FastifyView, {
engine: {
ejs: require('ejs'),
},
root: join(__dirname, '..', 'src'),
});

await module.listen();
});

Expand Down Expand Up @@ -67,4 +76,30 @@ describe('Fastify :: Routes', () => {
.put('/put')
.expect(HttpStatus.ACCEPTED, 'put');
});

it('registers `*` request', async () => {
return request(module.getHttpServer())
.get('/does-not-exist')
.expect(HttpStatus.NOT_FOUND, 'not-found');
});

describe('Render', () => {
it('renders view', async () => {
return request(module.getHttpServer())
.get('/render')
.expect((req) => {
expect(req.status).toBe(HttpStatus.OK);
expect(req.text).toContain('Hello World');
});
});

it('renders non-existing view', async () => {
return request(module.getHttpServer())
.get('/render-missing')
.expect((req) => {
expect(req.status).toBe(HttpStatus.INTERNAL_SERVER_ERROR);
expect(req.body.message).toBeDefined();
});
});
});
});
33 changes: 33 additions & 0 deletions server/integration/http/routes/test/koa.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Application, HttpStatus, Module } from '@server';
import { HttpModule } from '@server/http';
import { KoaAdapter } from '@server/koa';
import * as koaViews from 'koa-views';
import { join } from 'path';
import * as request from 'supertest';

import { AppModule } from '../src/app.module';
Expand All @@ -21,6 +23,11 @@ describe('Koa :: Routes', () => {
app = await Application.create(TestModule);
module = await app.inject<HttpModule>(HttpModule);

module.use(koaViews(join(__dirname, '..', 'src'), {
autoRender: false,
extension: 'ejs',
}));

await module.listen();
});

Expand Down Expand Up @@ -67,4 +74,30 @@ describe('Koa :: Routes', () => {
.put('/put')
.expect(HttpStatus.ACCEPTED, 'put');
});

it('registers `*` request', async () => {
return request(module.getHttpServer())
.get('/does-not-exist')
.expect(HttpStatus.NOT_FOUND, 'not-found');
});

describe('Render', () => {
it('renders view', async () => {
return request(module.getHttpServer())
.get('/render')
.expect((req) => {
expect(req.status).toBe(HttpStatus.OK);
expect(req.text).toContain('Hello World');
});
});

it('renders non-existing view', async () => {
return request(module.getHttpServer())
.get('/render-missing')
.expect((req) => {
expect(req.status).toBe(HttpStatus.INTERNAL_SERVER_ERROR);
expect(req.body.message).toBeDefined();
});
});
});
});
4 changes: 2 additions & 2 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@
]
}
},
"version": "1.0.0-beta.12"
"version": "1.0.0-beta.13"
}
4 changes: 3 additions & 1 deletion server/src/platforms/fastify/fastify-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export class FastifyAdapter implements HttpApplicationAdapter {

render(_response: Fastify.FastifyReply, template: string, message: object) {
return new Promise<string>((resolve, reject) => (this.app as any).view(template, message,
(err: Error, html: string) => err ? reject(err) : resolve(html),
(err: Error, html: string) => err || html?.['stack']
? reject(new Error((err || html).toString()))
: resolve(html),
));
}

Expand Down
8 changes: 6 additions & 2 deletions server/src/platforms/koa/koa-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ export class KoaAdapter implements HttpApplicationAdapter {
}

async render(response: Koa.Response, template: string, message: object) {
const html = await response.ctx.render(template, message);
try {
const html = await response.ctx.render(template, message);

return html as unknown as string;
return html as unknown as string;
} catch (err) {
throw new Error(err.message);
}
}

reply(response: Koa.Response, message: unknown, statusCode?: number) {
Expand Down

0 comments on commit d983552

Please sign in to comment.