Skip to content

Commit

Permalink
fix(adapter-node-http): Use requestBodyBuffers to parse body (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-leapyear authored Jan 30, 2020
1 parent b9306c3 commit 113fec5
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions packages/@pollyjs/adapter-node-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import nock from 'nock';
import {
normalizeClientRequestArgs,
isUtf8Representable,
isContentEncoded,
isJSONContent
isContentEncoded
} from 'nock/lib/common';
import Adapter from '@pollyjs/adapter';
import { HTTP_METHODS } from '@pollyjs/utils';
Expand Down Expand Up @@ -63,28 +62,17 @@ export default class HttpAdapter extends Adapter {

HTTP_METHODS.forEach(m => {
// Add an intercept for each supported HTTP method that will match all paths
interceptor.intercept(/.*/, m).reply(function(_, body, respond) {
interceptor.intercept(/.*/, m).reply(function(_, _body, respond) {
const { req, method } = this;
const { headers } = req;
const parsedArguments = normalizeClientRequestArgs(
...REQUEST_ARGUMENTS.get(req)
);
const url = getUrlFromOptions(parsedArguments.options);

if (body) {
if (
typeof body === 'string' &&
!isUtf8Representable(Buffer.from(body, 'hex'))
) {
// Nock internally converts a binary buffer into its hexadecimal
// representation so convert it back to a buffer.
body = Buffer.from(body, 'hex');
} else if (isJSONContent(headers)) {
// Nock will parse json content into an object. We have our own way
// of dealing with json content so convert it back to a string.
body = JSON.stringify(body);
}
}
const requestBodyBuffer = Buffer.concat(req.requestBodyBuffers);
const body = isUtf8Representable(requestBodyBuffer)
? requestBodyBuffer.toString('utf8')
: requestBodyBuffer;

adapter.handleRequest({
url,
Expand Down

0 comments on commit 113fec5

Please sign in to comment.