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

Using Matchers in request body #23

Open
shobhitkasliwal-onbe opened this issue Jan 4, 2022 · 3 comments
Open

Using Matchers in request body #23

shobhitkasliwal-onbe opened this issue Jan 4, 2022 · 3 comments

Comments

@shobhitkasliwal-onbe
Copy link

Hello
I am trying to implement the API Consumer using matchers in the POST request body but the mock server is always responding with 500 (body is always comes as empty in the expected response) . Can anyone please guide me how can I implement the api consumer and provider using the Matchers in the request and response body ?

Thanks

@bartschotten
Copy link
Owner

Hi,
Can you provide some examples of what you have so far?
Other than that I can only recommend that you look at the test projects as an example, if you haven't already.

@shobhitkasliwal-onbe
Copy link
Author

Hi @bartschotten - Thanks for the response. I did look at the test project but its not sending the request body as matchers.

Below is the sample code that is not working for me.

`[Fact]
public async Task Bff_PlaceOrder_Test2()
{

    var input = Guid.NewGuid().ToString();
    var requestJObject = Pact.JsonContent.With(Some.String.LikeGuid(input));

    var interaction = Pact.Interaction
    .UponReceiving("My Post Request")
    .With(Pact.Request
        .WithHeader("Content-Type", "application/json; charset=utf-8")
        .WithMethod(Method.POST)
        .WithBody(requestJObject)
        .WithPath("/mytest"))
    .WillRespondWith(Pact.Response
        .WithStatus(200)
        .WithHeader("Content-Type", "application/json")
        .WithBody(Pact.JsonContent.With(Some.Element.WithTheExactValue("true"))));
    builder.SetUp(interaction);

    var httpClient = new HttpClient { BaseAddress = new Uri(_mockProviderServiceBaseUri) };
    var requestContent = new StringContent(JsonConvert.SerializeObject(input), Encoding.UTF8, "application/json");

    // Act
    var result = await httpClient.PostAsync("mytest", requestContent);
    
    var body = await result.Content.ReadAsStringAsync();

    // Assert
    Assert.True(result != null);
   
}`

I am always getting 500 Error with the following message as response content.
{"message":"No matching response set up for this request.","actualRequests":{"method":"POST","path":"/mytest","headers":{"Host":"localhost:9224","Content-Type":"application/json; charset=utf-8","Content-Length":"38"},"query":{},"body":"175af1f4-b838-4504-b44e-6963566e89d7"},"expectedRequests":[{"method":"POST","path":"/mytest","headers":{"Content-Type":"application/json; charset=utf-8"},"query":{},"body":{}}]}

In the expected response the body is always coming as empty.

@bartschotten
Copy link
Owner

Oh now I see. Matching rules on requests are not supported. As I see it they are not an essential part of Pact.

https://github.com/bartschotten/com-pact#whats-not-supported

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