Skip to content

Commit

Permalink
Fallback to mime type of rewrittenPath when mime type of relativePath…
Browse files Browse the repository at this point in the history
… is null (#13)
quocnguyen authored and leo committed May 30, 2018
1 parent 48d11fd commit 7bba16e
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -170,7 +170,7 @@ const appendHeaders = (target, source) => {
}
};

const getHeaders = async (customHeaders = [], relativePath, stats) => {
const getHeaders = async (customHeaders = [], relativePath, rewrittenPath, stats) => {
const related = {};

if (customHeaders.length > 0) {
@@ -187,7 +187,7 @@ const getHeaders = async (customHeaders = [], relativePath, stats) => {
}

const defaultHeaders = {
'Content-Type': mime.getType(relativePath),
'Content-Type': mime.getType(relativePath) || mime.getType(rewrittenPath),
'Last-Modified': stats.mtime.toUTCString(),
'Content-Length': stats.size
};
@@ -520,7 +520,7 @@ module.exports = async (request, response, config = {}, methods = {}) => {
relativePath = errorPage;
}

const headers = await getHeaders(config.headers, relativePath, stats);
const headers = await getHeaders(config.headers, relativePath, rewrittenPath, stats);
const stream = await handlers.createReadStream(absolutePath);

response.writeHead(response.statusCode || 200, headers);
13 changes: 13 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -723,3 +723,16 @@ test('set `createReadStream` handler to async function', async t => {
t.deepEqual(content, text);
});

test('return mime type of the `rewrittenPath` if mime type of `relativePath` is null', async t => {
const url = await getUrl({
rewrites: [{
source: '**',
destination: 'clean-file.html'
}]
});

const response = await fetch(`${url}/whatever`);
const type = response.headers.get('content-type');

t.is(type, 'text/html');
});

0 comments on commit 7bba16e

Please sign in to comment.