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: Correctly handle array header values #179

Merged
merged 3 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import '@pollyjs-tests/helpers/global-fetch';

import setupPersister from '@pollyjs-tests/helpers/setup-persister';
import setupFetchRecord from '@pollyjs-tests/helpers/setup-fetch-record';
import persisterTests from '@pollyjs-tests/integration/persister-tests';
import { setupMocha as setupPolly } from '@pollyjs/core';

import pollyConfig from '../utils/polly-config';

describe.only('Integration | FS Persister | node-fetch', function() {
setupPolly.beforeEach(pollyConfig);

setupFetchRecord({ host: 'http://localhost:4000' });
setupPersister();
setupPolly.afterEach();

persisterTests();
});
19 changes: 6 additions & 13 deletions packages/@pollyjs/adapter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ACTIONS, MODES, Serializers, assert } from '@pollyjs/utils';
import Interceptor from './-private/interceptor';
import isExpired from './utils/is-expired';
import stringifyRequest from './utils/stringify-request';
import normalizeRecordedResponse from './utils/normalize-recorded-response';

const REQUEST_HANDLER = Symbol();

Expand Down Expand Up @@ -185,19 +186,11 @@ export default class Adapter {
await this.timeout(pollyRequest, recordingEntry);
pollyRequest.action = ACTIONS.REPLAY;

const { status, statusText, headers, content } = recordingEntry.response;
const normalizedResponse = {
statusText,
statusCode: status,
headers: (headers || []).reduce((accum, { name, value }) => {
accum[name] = value;

return accum;
}, {}),
body: content && content.text
};

return this.onReplay(pollyRequest, normalizedResponse, recordingEntry);
return this.onReplay(
pollyRequest,
normalizeRecordedResponse(recordingEntry.response),
recordingEntry
);
}

if (config.recordIfMissing) {
Expand Down
30 changes: 30 additions & 0 deletions packages/@pollyjs/adapter/src/utils/normalize-recorded-response.js
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 }) => {
const existingValue = accum[name];

if (existingValue) {
if (!isArray(existingValue)) {
accum[name] = [existingValue];
}

accum[name].push(value);
} else {
accum[name] = value;
}

return accum;
}, {});
}
9 changes: 6 additions & 3 deletions packages/@pollyjs/persister/src/har/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import getByteLength from 'utf8-byte-length';
import setCookies from 'set-cookie-parser';

import toNVPairs from './utils/to-nv-pairs';
import getLastHeader from './utils/get-last-header';

function headersSize(request) {
const keys = [];
Expand Down Expand Up @@ -34,7 +35,7 @@ export default class Request {

if (request.body) {
this.postData = {
mimeType: request.getHeader('Content-Type') || 'text/plain',
mimeType: getLastHeader(this, 'Content-Type') || 'text/plain',
params: []
};

Expand All @@ -43,8 +44,10 @@ export default class Request {
}
}

if (request.hasHeader('Content-Length')) {
this.bodySize = parseInt(request.getHeader('Content-Length'), 10);
const contentLength = getLastHeader(this, 'Content-Length');

if (contentLength) {
this.bodySize = parseInt(contentLength, 10);
} else {
this.bodySize =
this.postData && this.postData.text
Expand Down
13 changes: 8 additions & 5 deletions packages/@pollyjs/persister/src/har/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import getByteLength from 'utf8-byte-length';
import setCookies from 'set-cookie-parser';

import toNVPairs from './utils/to-nv-pairs';
import getLastHeader from './utils/get-last-header';

function headersSize(response) {
const keys = [];
Expand All @@ -28,24 +29,26 @@ export default class Response {
this.headers = toNVPairs(response.headers);
this.headersSize = headersSize(this);
this.cookies = setCookies.parse(response.getHeader('Set-Cookie'));
this.redirectURL = '';
this.redirectURL = getLastHeader(this, 'Location') || '';

this.content = {
mimeType: response.getHeader('Content-Type') || 'text/plain'
mimeType: getLastHeader(this, 'Content-Type') || 'text/plain'
};

if (response.body && typeof response.body === 'string') {
this.content.text = response.body;
}

if (response.hasHeader('Content-Length')) {
this.content.size = parseInt(response.getHeader('Content-Length'), 10);
const contentLength = getLastHeader(this, 'Content-Length');

if (contentLength) {
this.content.size = parseInt(contentLength, 10);
} else {
this.content.size = this.content.text
? getByteLength(this.content.text)
: 0;
}

this.bodySize = this.content ? this.content.size : 0;
this.bodySize = this.content.size;
}
}
9 changes: 9 additions & 0 deletions packages/@pollyjs/persister/src/har/utils/get-last-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function getLastHeader({ headers }, name) {
name = name.toLowerCase();

for (let i = headers.length - 1; i >= 0; i--) {
if (headers[i].name === name) {
return headers[i].value;
}
}
}
13 changes: 12 additions & 1 deletion packages/@pollyjs/persister/src/har/utils/to-nv-pairs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
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)) {
pairs.push(...value.map(v => ({ name, value: v })));
offirgolan marked this conversation as resolved.
Show resolved Hide resolved
} else {
pairs.push({ name, value });
}

return pairs;
}, []);
}