Skip to content

Commit

Permalink
core 404 and 500 routes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kommander committed Jun 21, 2017
1 parent 6ea873f commit 012fbec
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/aden.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function setupApp(pages) {
.then(() => this.applyHook('post:setup', { pages, app: this.app }))
.then(() => {
this.app.use((req, res, next) => this.notFoundRoute(req, res, next));
this.app.use(pages[0].basePath, (err, req, res, next) =>
this.app.use((err, req, res, next) =>
this.errorRoute(err, req, res, next));
});
}
Expand All @@ -79,9 +79,8 @@ function notFoundRoute(req, res, next) {

function errorRoute(err, req, res, next) {
this.log.error('Unhandled:', err);

if (res.headersSent) {
next(err);
return;
}

Expand All @@ -92,7 +91,7 @@ function errorRoute(err, req, res, next) {
if (!res.headersSent) {
res.status(500);
if (this.isDEV) {
res.send(`<pre>${err.stack || err.message || err.code || err}</pre>`);
res.send(`<pre>${err.stack || err}</pre>`);
} else {
res.send('Internal Server Error');
}
Expand Down
45 changes: 45 additions & 0 deletions test/integration/routing.dev.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,51 @@ describe('Routing Dev', () => {
.catch(done);
});

she('has a core status page (404)', (done) => {
aden({
dev: true,
attitudes: ['!statuspages'],
})
.init(path.resolve(__dirname, '../tmpdata/emptypath'))
.then((an) => an.run('dev'))
.then((an) => {
request(an.app)
.get('/')
.end((err, res) => {
if (err) done(err);
expect(res.status).toBe(404);
expect(res.text).toMatch(/Could not find what you were looking for\./ig);
an.shutdown(done);
});
})
.catch(done);
});

she('core error route returns when headers already sent (500)', (done) => {
aden({
dev: true,
attitudes: ['!statuspages'],
})
.hook('post:setup', ({ app }) => {
app.use((err, req, res, next) => {
res.send('errrrr');
next(err);
});
})
.init(path.resolve(__dirname, '../tmpdata/custom/provoke'))
.then((an) => an.run('dev'))
.then((an) => {
request(an.app)
.get('/')
.end((err, res) => {
if (err) done(err);
expect(res.text).toMatch(/errrrr/ig);
an.shutdown(done);
});
})
.catch(done);
});

she('// Things Aden already does but are untested...');
she('allows params in { route: \'/:id\'} > .server');
she('mounts absolute routes absolute');
Expand Down
45 changes: 45 additions & 0 deletions test/integration/routing.prod.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,51 @@ describe('Routing Prod', () => {
.catch(done);
});

she('has a core status page (404)', (done) => {
aden({
attitudes: ['!statuspages'],
})
.init(path.resolve(__dirname, '../tmpdata/emptypath'))
.then((an) => an.run('build'))
.then((an) => an.run('production'))
.then((an) => {
request(an.app)
.get('/')
.end((err, res) => {
if (err) done(err);
expect(res.status).toBe(404);
expect(res.text).toMatch(/Could not find what you were looking for\./ig);
an.shutdown(done);
});
})
.catch(done);
});

she('core error route returns when headers already sent (500)', (done) => {
aden({
attitudes: ['!statuspages'],
})
.hook('post:setup', ({ app }) => {
app.use((err, req, res, next) => {
res.send('errrrr');
next(err);
});
})
.init(path.resolve(__dirname, '../tmpdata/custom/provoke'))
.then((an) => an.run('build'))
.then((an) => an.run('production'))
.then((an) => {
request(an.app)
.get('/')
.end((err, res) => {
if (err) done(err);
expect(res.text).toMatch(/errrrr/ig);
an.shutdown(done);
});
})
.catch(done);
});

she('// Things Aden already does but are untested...');
she('allows params in { route: \'/:id\'} > .server');
she('mounts absolute routes absolute');
Expand Down

0 comments on commit 012fbec

Please sign in to comment.