diff --git a/src/utils/response-cache.ts b/src/utils/response-cache.ts index 67588055..e9fa6190 100644 --- a/src/utils/response-cache.ts +++ b/src/utils/response-cache.ts @@ -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, } diff --git a/test/unit/utils/response-cache.test.ts b/test/unit/utils/response-cache.test.ts index 15c9c2e8..baae94d1 100644 --- a/test/unit/utils/response-cache.test.ts +++ b/test/unit/utils/response-cache.test.ts @@ -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' ], + }) + }) })