Problem with Request Match WithBody #164

Closed
opened 2025-12-29 08:23:13 +01:00 by adam · 4 comments
Owner

Originally created by @jafin on GitHub (Feb 12, 2019).

Good afternoon,

I am having trouble understanding what I have configured incorrectly to get a Request match using WithBody. Test below gives 404 when using WithBody and WildcardMatcher or Func matcher.
If I remove the WithBody on the Request then a http OK response occurs.

      [Fact]
        public async Task BodyContentTest()
        {
            var server = FluentMockServer.Start();
            server.Given(Request.Create()
                    .WithPath("/Test")
                    .UsingPost()
                    .WithBody(new WildcardMatcher("access_token")))
                .RespondWith(Response.Create()
                    .WithStatusCode(200)
                    .WithHeader("Content-Type", "text/plain")
                    .WithBody("Result"));

            var client = new HttpClient {BaseAddress = new Uri($"http://localhost:{server.Ports[0]}/Test")};
            var content = new StringContent("access_token=foo", Encoding.UTF8, "application/json");
            var testResponse = await client.PostAsync("/Test", content);
            Assert.Equal(HttpStatusCode.OK, testResponse.StatusCode);
        }
Originally created by @jafin on GitHub (Feb 12, 2019). Good afternoon, I am having trouble understanding what I have configured incorrectly to get a Request match using WithBody. Test below gives 404 when using WithBody and WildcardMatcher or Func matcher. If I remove the WithBody on the Request then a http OK response occurs. ```c# [Fact] public async Task BodyContentTest() { var server = FluentMockServer.Start(); server.Given(Request.Create() .WithPath("/Test") .UsingPost() .WithBody(new WildcardMatcher("access_token"))) .RespondWith(Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "text/plain") .WithBody("Result")); var client = new HttpClient {BaseAddress = new Uri($"http://localhost:{server.Ports[0]}/Test")}; var content = new StringContent("access_token=foo", Encoding.UTF8, "application/json"); var testResponse = await client.PostAsync("/Test", content); Assert.Equal(HttpStatusCode.OK, testResponse.StatusCode); } ```
adam added the question label 2025-12-29 08:23:13 +01:00
adam closed this issue 2025-12-29 08:23:14 +01:00
Author
Owner

@jafin commented on GitHub (Feb 12, 2019):

I was able to get a matcher going for body content if I used:

 private static Func<string, bool> StringMatch()
        {
            return b => b.Contains("access_token");
        }

but still unsure why WildcardMatcher or as inline func
WithBody(b=>b.ToString().Contains("access_token")))
did not work.
If anyone could enlighten me that would be great!

@jafin commented on GitHub (Feb 12, 2019): I was able to get a matcher going for body content if I used: ```c# private static Func<string, bool> StringMatch() { return b => b.Contains("access_token"); } ``` but still unsure why WildcardMatcher or as inline func WithBody(b=>b.ToString().Contains("access_token"))) did not work. If anyone could enlighten me that would be great!
Author
Owner

@StefH commented on GitHub (Feb 12, 2019):

When using WildcardMatcher, you should use * as a wildcard character.

Like:

.WithBody(new WildcardMatcher("*access_token*")))
@StefH commented on GitHub (Feb 12, 2019): When using WildcardMatcher, you should use `*` as a wildcard character. Like: ``` c# .WithBody(new WildcardMatcher("*access_token*"))) ```
Author
Owner

@StefH commented on GitHub (Feb 15, 2019):

@jafin : does this solve your issue?

@StefH commented on GitHub (Feb 15, 2019): @jafin : does this solve your issue?
Author
Owner

@jafin commented on GitHub (Feb 19, 2019):

yes thanks for the reply.

@jafin commented on GitHub (Feb 19, 2019): yes thanks for the reply.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#164