Stubbed response with only callback returns unexpected status code.
See original GitHub issueDescribe the bug
When setting up a stubbed response with only a callback I would expect that the status code which I set on the returned ResponseMessage
would be the status code which the request will receive. But you always get HttpStatusCode.OK
anyway.
Expected behavior:
If I only specify .WithCallback()
and not .WithStatusCode()
I would expect that the status code of the returned ResponseMessage
object is returned.
Test to reproduce
This test is currently failing with WireMock.Net 1.3.5
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using NUnit.Framework;
using WireMock;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
namespace WireMockIssue
{
[TestFixture]
public class StatusCodeStubIssue
{
private WireMockServer _mockServer;
[SetUp]
public void Setup()
{
_mockServer = WireMockServer.Start(8088);
_mockServer
.Given(Request.Create().WithPath("/foo"))
.RespondWith(Response.Create()
.WithCallback(request => new ResponseMessage {StatusCode = HttpStatusCode.Conflict}));
}
[TearDown]
public void TearDown()
{
_mockServer.Dispose();
}
[Test]
public async Task TestToGetHttpConflictStatus()
{
using var httpClient = new HttpClient();
var response = await httpClient.PostAsync("http://localhost:8088/foo", new StringContent("dummy"));
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Conflict));
}
}
}
It fails with:
WireMockIssue.StatusCodeStubIssue.TestToGetHttpConflictStatus
Expected: Conflict
But was: OK
Other related info
I checked in the debugger that the callback was triggered. It was triggered.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
Stubbed method returns nil inside a before_create callback
The before_filter method is being called, but inside that callback, the stubbing of the account method does not seems to work. I've tested...
Read more >Stubs - Sinon.JS
Stubbing individual methods tests intent more precisely and is less susceptible to unexpected behavior as the object's code evolves.
Read more >When/how to use Mockito Answer
We use when-return clause to stub a method. ... public Void answer(InvocationOnMock invocation) { Callback callback = (Callback) invocation.
Read more >Response Stubbing - Standalone API Simulator
response: from: stub status: code: 200 reason: OK. API Simulator will actually default the HTTP status to 200 (OK) if the status isn't...
Read more >Class Mockito
The Mockito library enables mock creation, verification and stubbing. This javadoc content is also available on the https://site.mockito.org/ web page.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
See https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Hello @rudi-brunner, thank you for finding this bug. I’ll take a look.