question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stubbed response with only callback returns unexpected status code.

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
StefHcommented, Nov 6, 2020

Hello @rudi-brunner, thank you for finding this bug. I’ll take a look.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found