WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.39k stars 207 forks source link

Server returns unexpected 400 unless I use Fiddler which fixes the issue #937

Closed laura-rodriguez closed 1 year ago

laura-rodriguez commented 1 year ago

Describe the bug

I have a unit test to test rate limits that requires two stub responses. The first one is to get an OAuth token (200 - access token) to authorize a request, and the second one which returns a 429. When I run the test from Visual Studio or CCI, the test fails because the response status is 400 instead of 429. However, when I use Fiddler (which I used for debugging) the test works just fine, and I'm not able to reproduce the issue.

Expected behavior:

I expect the test to return 200 with access token for the first call (this works fine), and then a 429 instead of 400.

Test to reproduce

The exact test code to reproduce it is public and can be found here.

Other related info

Provide additional information if any. Test failing: image Test passing when Fiddler is capturing image

StefH commented 1 year ago

It is a strange bug. It seems that the request is invalid and is not handled by WireMock?

I will take a look.

StefH commented 1 year ago

@laura-rodriguez

I think I found your issue.

This mapping is setup like: image

However WireMock.Net will return the access_token as defined, so with \r\n and with spaces.

This response is added to headers using this code: localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + token);

And apparently RestSharp breaks on the \r\n. Because when I do this: token.Replace("\r", "").Replace("\n", "") it works.

(Probably Fiddler tries to repair the data which is probably the explanation that it works with fiddler)

The next issue is that the errorLink is not valid json: image

laura-rodriguez commented 1 year ago

Thanks so much for looking into it @StefH ⭐ ! I'll fix my code with your suggestions and try again.

StefH commented 1 year ago

@laura-rodriguez Please let me know if this indeed solves your issue. Then this issue can be closed.

laura-rodriguez commented 1 year ago

@StefH thank you! Your suggestion fixes my issue.