Skip to content

Commit

Permalink
feat: check if header has already been set (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
9662 committed May 18, 2021
1 parent 14f5592 commit 6048415
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 10 additions & 7 deletions lib/middlewares/route.js
Expand Up @@ -63,16 +63,19 @@ module.exports = function(app) {
return;
}

if (feed && feed.path === url) {
if (feed.type === 'atom') {
res.setHeader('Content-Type', 'application/atom+xml');
} else if (feed.type === 'rss') {
res.setHeader('Content-Type', 'application/rss+xml');
// Do not overwrite header if already set by another middleware
if (!res.hasHeader('Content-Type')) {
if (feed && feed.path === url) {
if (feed.type === 'atom') {
res.setHeader('Content-Type', 'application/atom+xml');
} else if (feed.type === 'rss') {
res.setHeader('Content-Type', 'application/rss+xml');
} else {
res.setHeader('Content-Type', extname ? mime.getType(extname) : 'application/octet-stream');
}
} else {
res.setHeader('Content-Type', extname ? mime.getType(extname) : 'application/octet-stream');
}
} else {
res.setHeader('Content-Type', extname ? mime.getType(extname) : 'application/octet-stream');
}

if (method === 'GET') {
Expand Down
13 changes: 12 additions & 1 deletion test/index.js
Expand Up @@ -31,10 +31,17 @@ describe('server', () => {
{path: 'index.html', data: 'index'},
{path: 'foo/index.html', data: 'foo'},
{path: 'bar/baz.html', data: 'baz'},
{path: 'bar.jpg', data: 'bar'}
{path: 'bar.jpg', data: 'bar'},
{path: 'baz.zzz', data: ''}
]);

// Register middlewares
hexo.extend.filter.register('server_middleware', app => {
app.use('/baz.zzz', (req, res, next) => {
res.setHeader('Content-Type', 'application/x-custom');
next();
});
});
hexo.extend.filter.register('server_middleware', require('../lib/middlewares/header'));
hexo.extend.filter.register('server_middleware', require('../lib/middlewares/gzip'));
hexo.extend.filter.register('server_middleware', require('../lib/middlewares/logger'));
Expand Down Expand Up @@ -102,6 +109,10 @@ describe('server', () => {
.expect('Content-Type', 'image/jpeg')
.expect(200)));

it('Do not try to overwrite Content-Type header', () => Promise.using(prepareServer(), app => request(app).get('/baz.zzz')
.expect('Content-Type', 'application/x-custom')
.expect(200)));

it('Enable compression if options.compress is true', () => {
hexo.config.server.compress = true;

Expand Down

0 comments on commit 6048415

Please sign in to comment.