Skip to content

Commit

Permalink
fix: remove or flatten header arrays in response
Browse files Browse the repository at this point in the history
  • Loading branch information
DigTheDoug committed Feb 4, 2022
1 parent f2f50b9 commit a0a9a2b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/commands/index.js
Expand Up @@ -133,10 +133,23 @@ const playback = {
if (response) {
try {
// We need to delete the 'content-encoding' header, as the recorded
// response will not have the body in that format.
// response will not have the body in that format due to compression.
// Notes on 'content-encoding':
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
delete response.headers['content-encoding'];
// Any headers that contain an array need to be either flattened
// if they only have one entry or deleted as Cypress does not
// handle array headers in response:
// 'An invalid StaticResponse was supplied to req.reply(). headers must be a map of strings to strings.'
for (const [key, value] of Object.entries(response.headers)){
if(Array.isArray(value) && value.length === 1) {
console.log(`Flattening response header "${key}"`);
response.headers[key] = value[0];
} else if(Array.isArray(value) && value.length >= 1) {
delete response.headers[key];
console.warn(`The response header "${key}" is an Array with multiple entries and was deleted.`);
}
}
req.reply(response.statusCode, response.body, response.headers);
} catch (e) {
console.error(response);
Expand Down

0 comments on commit a0a9a2b

Please sign in to comment.