hibri / HttpMock

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

Request to put an example of how to mock POST request which contains BODY #74

Closed vinnakotars closed 6 years ago

vinnakotars commented 7 years ago

Hi,

Request you to put an example on the main page of how to mock POST request which contains BODY. If there are any restrictions on placing it there, please mail me ravi.sankar.vinnakota@outlook.com

Thanks, Ravi.

hibri commented 7 years ago

Hi @vinnakotars There is an example here https://github.com/hibri/HttpMock/blob/master/src/HttpMock.Integration.Tests/HttpEndPointTests.cs#L44

vinnakotars commented 7 years ago

Thank you Hibri. It is helpful. But my requirement is slightly different.

If I understand correctly, the solution you mentioned returns the body of the received POST request and then I validate the same in my rest of code. What I would like to have is: If my HTTPMock server receives a Post request with a specific body, then, it should return a canned response. e.g., I have bolded a desirable WithBody in the code below. Is something like that possible ? _stubHttp.Stub(x => x.Post("/endpoint")) .WithBody(expectedBody) .Return(expected) .OK();

hibri commented 7 years ago

This is not possible, and I don't think this is correct to implement. The POST body can be binary or text, it won't be feasible to match on this. What is the scenario you are testing? if the code path you are testing makes only one post request then just one mock should suffice.

vinnakotars commented 7 years ago

Hi Hibri, I will try to do with the inputs you gave me. I will get back to you again after trying. The case I was referring was the post request is same, but body is different !