Skip to content

Commit

Permalink
feat: 100 times cache time for wrong path
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Sep 20, 2019
1 parent 5743134 commit f7d9688
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/middleware/parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ const he = require('he');
const mercury_parser = require('@postlight/mercury-parser');
const cheerio = require('cheerio');
const got = require('@/utils/got');
const config = require('@/config').value;

module.exports = async (ctx, next) => {
await next();

if (!ctx.state.data && !ctx._matchedRoute) {
ctx.set({
'Cache-Control': `public, max-age=${config.cache.routeExpire * 100}`,
});
throw Error('wrong path');
}

Expand Down
2 changes: 2 additions & 0 deletions test/middleware/parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const server = require('../../lib/index');
const request = supertest(server);
const Parser = require('rss-parser');
const parser = new Parser();
const config = require('../../lib/config').value;

afterAll(() => {
server.close();
Expand Down Expand Up @@ -132,6 +133,7 @@ describe('wrong_path', () => {
it(`wrong_path`, async () => {
const response = await request.get('/wrong');
expect(response.status).toBe(404);
expect(response.headers['cache-control']).toBe(`public, max-age=${config.cache.routeExpire * 100}`);
expect(response.text).toMatch(/Error: wrong path/);
});
});
Expand Down

0 comments on commit f7d9688

Please sign in to comment.