hibri / HttpMock

A library for creating Http servers on the fly in tests and stubbing responses
MIT License
128 stars 44 forks source link

Ignore Headers when Matching #90

Closed jonfinerty closed 6 years ago

jonfinerty commented 6 years ago

Hi, I'm using this library to test my application and it works great, but it looks like in order to stub out an endpoint successfully I need to list all the headers that get sent in WithHeaders. Unfortunately our application sends out GUID correlation id's in the header so it's easy to predict their values.

Is it (or would it be) possible to stub out an endpoint ignoring any given headers?

jonfinerty commented 6 years ago

I was just looking at doing a PR for this, but theres a question over intended behaviour when there's multiple stubs for the same endpoint with different headers.

A possible solution is:

It should try and match with the stub with the most correct headers, falling back to stubs which match fewer and then finally fall back to a stub without any headers.

hibri commented 6 years ago

Hi @jonfinerty The header match logic should return at the first match, does not need all headers. https://github.com/hibri/HttpMock/blob/master/src/HttpMock/HeaderMatch.cs#L8

An approach would be to override the header matching logic to take a lambda, so you can inject your own custom logic. Something like https://github.com/hibri/HttpMock/blob/master/src/HttpMock.Integration.Tests/StubConstraintsTests.cs#L27

It could be

.WithHeaders ( headers => headers.Contains(xxx))
hibri commented 6 years ago

If you have time could you send a PR with a failing test with this scenario?

jonfinerty commented 6 years ago

sure, I'll try to find some time this week

jonfinerty commented 6 years ago

Turns out we were adding a header with a null value, so I'm not sure what particularly was choking on that but we've fixed that bug in our application. Please close this issue if you want and if I find out something in HttpMock breaks on null headers I'll open a new issue/example test. Thanks and sorry for the mis-issue :)

hibri commented 6 years ago

Thanks @jonfinerty . No worries :)