Skip to content

Commit

Permalink
feat(core): Provide the request as an argument to matchRequestsBy met…
Browse files Browse the repository at this point in the history
…hods (#293)
  • Loading branch information
offirgolan authored Jan 13, 2020
1 parent 4448be5 commit 4e3163f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 29 deletions.
58 changes: 47 additions & 11 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ a GUID for the request.

polly.configure({
matchRequestsBy: {
method(method) {
method(method, req) {
return method.toLowerCase();
}
}
Expand All @@ -353,7 +353,7 @@ a GUID for the request.

polly.configure({
matchRequestsBy: {
headers(headers) {
headers(headers, req) {
delete headers['X-AUTH-TOKEN'];
return headers;
}
Expand Down Expand Up @@ -395,7 +395,7 @@ a GUID for the request.

polly.configure({
matchRequestsBy: {
body(body) {
body(body, req) {
const json = JSON.parse(body);

delete json.email;
Expand Down Expand Up @@ -442,6 +442,42 @@ a GUID for the request.
});
```

- ### url

_Type_: `Boolean | Function | Object`
_Default_: `{ protocol: true, username: true, ... }`

The request url.

**Example**

```js
polly.configure({
matchRequestsBy: {
url: false
}
});

polly.configure({
matchRequestsBy: {
url(url, req) {
return url.replace('test', '');
}
}
});

polly.configure({
matchRequestsBy: {
url: {
protocol(protocol) {
return 'https:';
},
query: false
}
}
});
```

- ### url.protocol

_Type_: `Boolean | Function`
Expand All @@ -463,7 +499,7 @@ a GUID for the request.
polly.configure({
matchRequestsBy: {
url: {
protocol(protocol) {
protocol(protocol, req) {
return 'https:';
}
}
Expand Down Expand Up @@ -491,7 +527,7 @@ a GUID for the request.
polly.configure({
matchRequestsBy: {
url: {
username(username) {
username(username, req) {
return 'username';
}
}
Expand All @@ -517,7 +553,7 @@ a GUID for the request.
}
matchRequestsBy: {
url: {
password(password) {
password(password, req) {
return 'password';
}
}
Expand Down Expand Up @@ -546,7 +582,7 @@ a GUID for the request.
polly.configure({
matchRequestsBy: {
url: {
hostname(hostname) {
hostname(hostname, req) {
return hostname.replace('.com', '.net');
}
}
Expand Down Expand Up @@ -575,7 +611,7 @@ a GUID for the request.
polly.configure({
matchRequestsBy: {
url: {
port(port) {
port(port, req) {
return 3000;
}
}
Expand All @@ -596,7 +632,7 @@ a GUID for the request.
polly.configure({
matchRequestsBy: {
url: {
pathname(pathname) {
pathname(pathname, req) {
return pathname.replace('/api/v1', '/api');
}
}
Expand Down Expand Up @@ -625,7 +661,7 @@ a GUID for the request.
polly.configure({
matchRequestsBy: {
url: {
query(query) {
query(query, req) {
return { ...query, token: '' };
}
}
Expand Down Expand Up @@ -654,7 +690,7 @@ a GUID for the request.
polly.configure({
matchRequestsBy: {
url: {
hash(hash) {
hash(hash, req) {
return hash.replace(/token=[0-9]+/, '');
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/@pollyjs/core/src/-private/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ export default class PollyRequest extends HTTPBase {
if (this[key] && matchRequestsBy[key]) {
this.identifiers[key] = NormalizeRequest[key](
this[key],
matchRequestsBy[key]
matchRequestsBy[key],
this
);
}
});
Expand Down
36 changes: 20 additions & 16 deletions packages/@pollyjs/core/src/utils/normalize-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ function isFunction(fn) {
return typeof fn === 'function';
}

export function method(method, config) {
return isFunction(config) ? config(method) : method.toUpperCase();
export function method(method, config, req) {
return isFunction(config) ? config(method, req) : method.toUpperCase();
}

export function url(url, config = {}) {
const parsedUrl = parseUrl(url, true);
export function url(url, config, req) {
let parsedUrl = parseUrl(url, true);

// Remove any url properties that have been disabled via the config
keys(config).forEach(key => {
if (isFunction(config[key])) {
parsedUrl.set(key, config[key](parsedUrl[key]));
} else if (!config[key]) {
parsedUrl.set(key, '');
}
});
if (isFunction(config)) {
parsedUrl = parseUrl(config(url, req), true);
} else {
// Remove any url properties that have been disabled via the config
keys(config || {}).forEach(key => {
if (isFunction(config[key])) {
parsedUrl.set(key, config[key](parsedUrl[key], req));
} else if (!config[key]) {
parsedUrl.set(key, '');
}
});
}

// Sort Query Params
if (isObjectLike(parsedUrl.query)) {
Expand All @@ -36,11 +40,11 @@ export function url(url, config = {}) {
return parsedUrl.href;
}

export function headers(headers, config) {
export function headers(headers, config, req) {
const normalizedHeaders = new HTTPHeaders(headers);

if (isFunction(config)) {
return config(normalizedHeaders);
return config(normalizedHeaders, req);
}

if (isObjectLike(config) && isArray(config.exclude)) {
Expand All @@ -50,8 +54,8 @@ export function headers(headers, config) {
return normalizedHeaders;
}

export function body(body, config) {
return isFunction(config) ? config(body) : body;
export function body(body, config, req) {
return isFunction(config) ? config(body, req) : body;
}

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ describe('Unit | Utils | Normalize Request', function() {
it('should support a custom fn', function() {
expect(method('GET', m => m.toLowerCase())).to.equal('get');
});

it('should pass the correct arguments to the custom fn', function() {
const req = {};

method(
'GET',
(method, request) => {
expect(method).to.equal('GET');
expect(request).to.equal(req);

return method;
},
req
);
});
});

describe('headers', function() {
Expand Down Expand Up @@ -71,6 +86,22 @@ describe('Unit | Utils | Normalize Request', function() {
).to.deep.equal({ accept: 'foo', 'content-type': 'Bar' });
});

it('should pass the correct arguments to the custom fn', function() {
const req = {};
const reqHeaders = { foo: 'foo' };

headers(
reqHeaders,
(headers, request) => {
expect(headers).to.deep.equal(reqHeaders);
expect(request).to.equal(req);

return headers;
},
req
);
});

it('should not mutate the original headers in the custom fn', function() {
const reqHeaders = { foo: 'bar' };

Expand All @@ -88,7 +119,7 @@ describe('Unit | Utils | Normalize Request', function() {

describe('url', function() {
it('should sort query params', function() {
expect(url('http://foo.com?b=1&c=1&a=1')).to.equal(
expect(url('http://foo.com?b=1&c=1&a=1', {})).to.equal(
'http://foo.com?a=1&b=1&c=1'
);
});
Expand Down Expand Up @@ -162,11 +193,64 @@ describe('Unit | Utils | Normalize Request', function() {
it('should respect relative urls', function() {
expect(url('/some/path')).to.equal('/some/path');
});

it('should support a custom fn', function() {
expect(url('https://foo.bar', url => url.replace('bar', 'foo'))).to.equal(
'https://foo.foo'
);
});

it('should pass the correct arguments to the custom fn', function() {
const req = {};

url(
'https://foo.bar',
(url, request) => {
expect(url).to.deep.equal('https://foo.bar');
expect(request).to.equal(req);

return url;
},
req
);
});

it("should pass the correct arguments to the individual `matchRequestsBy.url` option's custom fn", function() {
const req = {};

url(
'https://foo.bar',
{
protocol: (protocol, request) => {
expect(protocol).to.deep.equal('https:');
expect(request).to.equal(req);

return protocol;
}
},
req
);
});
});

describe('body', function() {
it('should support a custom fn', function() {
expect(body('foo', b => b.toUpperCase())).to.equal('FOO');
});

it('should pass the correct arguments to the custom fn', function() {
const req = {};

url(
'body',
(body, request) => {
expect(body).to.deep.equal('body');
expect(request).to.equal(req);

return body;
},
req
);
});
});
});
21 changes: 21 additions & 0 deletions tests/integration/adapter-identifier-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ function adapterIdentifierTests() {
});

describe('url', function() {
testConfiguration('url', false, {
expected: {
id: '3914e0be4d2f04139554f5ffada7191c',
identifiers: {
headers: { 'content-type': 'application/json;charset=utf-8' },
method: 'POST',
body: '{}'
}
},
overrides: {
'node-http': {
id: 'ecc7560697b752deb2529686affcaa71',
identifiers: {
headers: {
host: 'localhost:4000'
}
}
}
}
});

testConfiguration('url.protocol', false, {
expected: {
id: '79224baf23dc29f8115516cb8fe0546f',
Expand Down

0 comments on commit 4e3163f

Please sign in to comment.