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

Fix for DateTime Header causing null value in ResponseBuilder #123

Closed
rangerranger opened this issue Apr 10, 2018 · 19 comments
Closed

Fix for DateTime Header causing null value in ResponseBuilder #123

rangerranger opened this issue Apr 10, 2018 · 19 comments

Comments

@rangerranger
Copy link

Having the below header in a static mapping file as part of the response results in a NULL value being returned to ResponseBuilder in InitResponseBuilder, since it either expects a string or json - but this header value is being read in as a DateTime object.

"Rate-Limit-Reset": "2018-04-09T20:39:46.956Z",

I added a quickfix like so, please let me know if that looks ok?
In src/WireMock.Net/Server/FluentMockServer.Admin.cs:
RangerRangerOrg@747524c#diff-8633a9484e8c211d1cc3fc5d0c12f4de

@StefH
Copy link
Collaborator

StefH commented Apr 11, 2018

This quickfix contains two things? JsonExactMatcher code, do you still need that?
And the datetime fix.

Can you make a PR with just the datetime header fix and also add some unittests?

@rangerranger
Copy link
Author

I will add the unit tests for the DateTime fix and create a separate PR. As to the ExactJsonMatcher - please can you tell me how to correctly use the JsonPathMatcher if we want to just record request/responses via proxy WireMock and then use them to mock? In that case the JsonPathMatcher is not matching the subsequent requests even when they are exact same Json, Does the recording need to be changed to some JsonPath syntax after recording? Thank you Stef.

@StefH
Copy link
Collaborator

StefH commented Apr 11, 2018

If using WireMock to proxy request, please take a look at this code here:

https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.Proxy.Net452/Program.cs

Change this code

SaveMapping = true,
SaveMappingToFile = true

This will save each request and response in a file.

I hope this helps?

@rangerranger
Copy link
Author

rangerranger commented Apr 11, 2018 via email

@StefH
Copy link
Collaborator

StefH commented Apr 11, 2018

Ok I see.

When using the saved request, sometimes it's needed to manually adjust the mapping a bit because the matching is too strict because not only the body is matched but also headers, query and others.

Make sure to enable partial mapping and then do a http get request to /__admin/requests to see the matching details from the request you did send. Look for matchings where the match is 0.

@rangerranger
Copy link
Author

rangerranger commented Apr 11, 2018 via email

@StefH
Copy link
Collaborator

StefH commented Apr 15, 2018

Please create a sample project, so I can analyze.

@StefH
Copy link
Collaborator

StefH commented May 18, 2018

From now on it's also possible to use a simple Wildcard (string-matcher) for JSON messages, will this help you in this case?

@rangerranger
Copy link
Author

rangerranger commented May 21, 2018 via email

@StefH
Copy link
Collaborator

StefH commented May 21, 2018

Hello @rangerranger,

1]
When running in proxy mode with these settings:

SaveMapping = true,
SaveMappingToFile = true,

The request is recorded (and saved in the __admin\mappings folder). And the original body (when doing a request which has a body) is also recorded like:

"Body": {
      "Matcher": {
        "Name": "ExactMatcher",
        "Pattern": "{\n\t\"test\": 42\n}"
      }
}

However, the ExactMatcher is always used, you have to manually adjust it to the mapper you would like to use.

2]
A CustomBodyMatcher, CustomBodyAsJsonMatcher and CustomBodyAsBytes can be added to settings, but do you still need these when you read point 1 above?

@rangerranger
Copy link
Author

rangerranger commented May 21, 2018 via email

@StefH
Copy link
Collaborator

StefH commented May 21, 2018

Hi Aroon,

1]
The code block you mention is not changed in latest releases. However I think the proxy code works fine.

When you post a string as body, or a json string, the body is added to the request and the exact string is added as pattern and exact matcher is used.

So when I send this using postman:
post

I get exactly what I posted in option 1 above.

Now it's up to the user from WireMock to analyze the whole request, and check if some thing need changing. In order to craft the correct mapping.

2]
The bodyasbytes is not supported as is, but I believe that when you send a binary using post, the body is recorded as base64.

3]
When you want custom matchers, you can just use one of the Func methods:

/// <summary>
/// WithBody: func (string)
/// </summary>
/// <param name="func">The function.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody([NotNull] Func<string, bool> func);

/// <summary>
/// WithBody: func (byte[])
/// </summary>
/// <param name="func">The function.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody([NotNull] Func<byte[], bool> func);

/// <summary>
/// WithBody: func (object)
/// </summary>
/// <param name="func">The function.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody([NotNull] Func<object, bool> func);

See

IRequestBuilder WithBody([NotNull] Func<string, bool> func);

@StefH
Copy link
Collaborator

StefH commented May 28, 2018

Hello @rangerranger : Is my post above clear ?

@rangerranger
Copy link
Author

rangerranger commented May 28, 2018 via email

@StefH
Copy link
Collaborator

StefH commented May 28, 2018

a] The recording of any body should work, you don't need to use that content-type.

b] Correct. You cannot use that directly. There is no possible way yet to convert a recorded mapping (json) into the C# code which defines that mapping. I can investigate if this is easy to build...

@rangerranger
Copy link
Author

rangerranger commented May 28, 2018 via email

@StefH
Copy link
Collaborator

StefH commented May 28, 2018

A quick question: you are using a POST or PUT request ?

@rangerranger
Copy link
Author

rangerranger commented May 28, 2018 via email

@StefH
Copy link
Collaborator

StefH commented Jul 2, 2018

Latest version support a new matcher : JsonMatcher which can be used to match a json body 1:1.

See also #154

I'm closing this issue now, please open a new one with details if you still have issues.

Or start a chat at https://gitter.im/wiremock_dotnet/Lobby

@StefH StefH closed this as completed Jul 2, 2018
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

No branches or pull requests

2 participants