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

relaxing headers check to allow partial matching #220

Closed
wants to merge 2 commits into from
Closed

relaxing headers check to allow partial matching #220

wants to merge 2 commits into from

Conversation

skyboyer
Copy link

Allowing mock match if request contains extra headers not specified in mock explicitly.
#219
#214

function containsHeaders(reference, target) {
console.log(reference, target);
if (!reference || !target) return deepEqual(reference, target);
if (typeof reference !== 'object' || typeof target !== 'object') return deepEqual(reference, target);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For rear edge case if for any reason handler[2] is not object but say a String instance('false') - so it would not match with headers object { 0: 'f' } even if it's passed in real request.

@@ -66,6 +66,30 @@ axios.get('/users', { params: { searchText: 'John' } } )
});
```

Mocking a `POST` request with specifying headers that must be present among request headers(but all extra request headers are ignored):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why there was nothing on filtering by headers before

'Content-Type': 'application/x-www-form-urlencoded',
'Header-test': 'test-header'
};

mock.onPost('/withHeaders', undefined, headers).reply(200);
mock.onPost('/withHeaders', undefined, { 'Header-test': 'test-header' }).reply(200);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we are passing more headers than specified in mock and that should still match(so it validate both filtering by headers and that fact check is partial not strict)

@mkalam-alami
Copy link

mkalam-alami commented Nov 5, 2019

This change would be very helpful. While it breaks retro-compatibility a bit it feels like a better default than the current behavior.

Alternately if we really want to keep retro-compatibility, we could let the headers argument be a function that takes the whole header object as input and accepts it or not.

src/utils.js Outdated Show resolved Hide resolved
@ctimmerm
Copy link
Owner

At this point I can't do breaking changes, however support for asymmetric matchers has been implemented in #181, which will allow you to achieve the same thing.

@ctimmerm ctimmerm closed this Feb 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants