Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly forward headers in asset cache #511

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 15 additions & 4 deletions test/unit/utils/response-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const logger = { debug: () => '' }
const defaultResponse = {
url: () => 'http://example.com/foo.txt',
status: () => 200,
headers: () => 'fake headers',
headers: () => ({ 'fake': 'foo=bar' }),
buffer: () => 'hello',
text() { return this.buffer() },
request() {
Expand All @@ -28,7 +28,7 @@ describe('Response cache util', () => {
expect(getResponseCache('http://example.com/foo.txt')).to.eql({
status: 200,
body: 'hello',
headers: 'fake headers',
headers: { fake: [ 'foo=bar' ] },
})
})

Expand All @@ -38,7 +38,7 @@ describe('Response cache util', () => {
expect(getResponseCache('http://example.com/foo.txt')).to.eql({
status: 201,
body: 'hello',
headers: 'fake headers',
headers: { fake: [ 'foo=bar' ] },
})
})

Expand All @@ -49,7 +49,7 @@ describe('Response cache util', () => {
expect(getResponseCache('http://example.com/foo.txt')).to.eql({
status: 200,
body: 'hello',
headers: 'fake headers',
headers: { fake: [ 'foo=bar' ] },
})
})

Expand All @@ -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' ],
})
})
})