Skip to content

Commit

Permalink
fix: Properly forward headers in asset cache
Browse files Browse the repository at this point in the history
CDP returns multiple headers joined by newlines, however `request.respond` (used
for cached responses) will hang if there are newlines in headers. This commit
normalizes header values as arrays split on newlines.
  • Loading branch information
Robdel12 committed May 18, 2020
1 parent 06dc067 commit b91f28e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/utils/response-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ export async function cacheResponse(response: Response, logger: any) {

responseCache[responseUrl] = {
status: response.status(),
headers: response.headers(),
// CDP returns multiple headers joined by newlines, however
// `request.respond` (used for cached responses) will hang if there are
// newlines in headers. The following reduction normalizes header values
// as arrays split on newlines
headers: Object.entries(response.headers())
.reduce((norm, [key, value]) => (
// tslint:disable-next-line
Object.assign(norm, { [key]: value.split('\n') })
), {}),
body: buffer,
}

Expand Down
11 changes: 11 additions & 0 deletions test/unit/utils/response-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,15 @@ describe('Response cache util', () => {

expect(getResponseCache('http://example.com/foo.txt')).to.eql(undefined)
})

it('newlines are removed from headers', async () => {
await cacheResponse({
...defaultResponse,
headers: () => ({ 'fake': 'foo=bar\nthing=baz' })
}, logger)

expect(getResponseCache('http://example.com/foo.txt').headers).to.eql({
fake: [ 'foo=bar', 'thing=baz' ],
})
})
})

0 comments on commit b91f28e

Please sign in to comment.