Skip to content

Commit

Permalink
Allow for setting headers in createReadStream (#37)
Browse files Browse the repository at this point in the history
* Allow for setting headers in `createReadStream`

* Added regression test
  • Loading branch information
leo authored Jul 21, 2018
1 parent e5e26da commit d23fd04
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,8 @@ module.exports = async (request, response, config = {}, methods = {}) => {
});
}

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

response.writeHead(response.statusCode || 200, headers);
stream.pipe(response);
Expand Down
33 changes: 33 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,36 @@ test('error responses get custom headers', async t => {
t.is(text, content);
});

test('modify config in `createReadStream` handler', async t => {
const name = '.dotfile';
const related = path.join(fixturesFull, name);
const content = await fs.readFile(related, 'utf8');

const config = {
headers: []
};

const header = {
key: 'X-Custom-Header',
value: 'test'
};

const url = await getUrl(config, {
createReadStream: async file => {
config.headers.unshift({
source: name,
headers: [header]
});

return fs.createReadStream(file);
}
});

const response = await fetch(`${url}/${name}`);
const text = await response.text();
const output = response.headers.get(header.key);

t.deepEqual(content, text);
t.deepEqual(output, header.value);
});

0 comments on commit d23fd04

Please sign in to comment.