-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Correctly handle array header values (#179)
* fix: Correctly handle array header values * fix: Add `_fromType` and add test cases * docs: Update docs
- Loading branch information
1 parent
b13c053
commit fb7dbb4
Showing
9 changed files
with
163 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/@pollyjs/adapter/src/utils/normalize-recorded-response.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { isArray } = Array; | ||
|
||
export default function normalizeRecordedResponse(response) { | ||
const { status, statusText, headers, content } = response; | ||
|
||
return { | ||
statusText, | ||
statusCode: status, | ||
headers: normalizeHeaders(headers), | ||
body: content && content.text | ||
}; | ||
} | ||
|
||
function normalizeHeaders(headers) { | ||
return (headers || []).reduce((accum, { name, value, _fromType }) => { | ||
const existingValue = accum[name]; | ||
|
||
if (existingValue) { | ||
if (!isArray(existingValue)) { | ||
accum[name] = [existingValue]; | ||
} | ||
|
||
accum[name].push(value); | ||
} else { | ||
accum[name] = _fromType === 'array' ? [value] : value; | ||
} | ||
|
||
return accum; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/@pollyjs/persister/src/har/utils/get-first-header.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { isArray } = Array; | ||
|
||
/** | ||
* Get the value of the given header name. If the value is an array, | ||
* get the first value. | ||
* | ||
* @export | ||
* @param {PollyRequest | PollyResponse} r | ||
* @param {string} name | ||
* @returns {string | undefined} | ||
*/ | ||
export default function getFirstHeader(r, name) { | ||
const value = r.getHeader(name); | ||
|
||
if (isArray(value)) { | ||
return value.length > 0 ? value[0] : ''; | ||
} | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
const { keys } = Object; | ||
const { isArray } = Array; | ||
|
||
export default function toNVPairs(o) { | ||
return keys(o || {}).map(name => ({ name, value: o[name] })); | ||
return keys(o || {}).reduce((pairs, name) => { | ||
const value = o[name]; | ||
|
||
if (isArray(value)) { | ||
// _fromType is used to convert the stored header back into the | ||
// type it originally was. Take the following example: | ||
// { 'content-type': ['text/htm'] } | ||
// => { name: 'content-type', value: 'text/html', _fromType: 'array } | ||
// => { 'content-type': ['text/htm'] } | ||
pairs.push(...value.map(v => ({ name, value: v, _fromType: 'array' }))); | ||
} else { | ||
pairs.push({ name, value }); | ||
} | ||
|
||
return pairs; | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters