Stubbed response with only callback returns unexpected status code. #311

Closed
opened 2025-12-29 08:25:55 +01:00 by adam · 6 comments
Owner

Originally created by @rudi-brunner on GitHub (Nov 6, 2020).

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

I checked in the debugger that the callback was triggered. It was triggered.

Originally created by @rudi-brunner on GitHub (Nov 6, 2020). ### 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 ``` c# 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.
adam added the bug label 2025-12-29 08:25:55 +01:00
adam closed this issue 2025-12-29 08:25:55 +01:00
Author
Owner

@StefH commented on GitHub (Nov 6, 2020):

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

@StefH commented on GitHub (Nov 6, 2020): Hello @rudi-brunner, thank you for finding this bug. I'll take a look.
Author
Owner

@StefH commented on GitHub (Nov 6, 2020):

The issue is that you use an Enum, if you use new ResponseMessage {StatusCode = (int) HttpStatusCode.Conflict})); it should work.

However, I'll make a fix in the code.

@StefH commented on GitHub (Nov 6, 2020): The issue is that you use an Enum, if you use `new ResponseMessage {StatusCode = (int) HttpStatusCode.Conflict}));` it should work. However, I'll make a fix in the code.
Author
Owner

@StefH commented on GitHub (Nov 6, 2020):

@rudi-brunner can you try preview version WireMock.Net.1.3.5-ci-13952.nupkg (from MyGet)?

@StefH commented on GitHub (Nov 6, 2020): @rudi-brunner can you try preview version `WireMock.Net.1.3.5-ci-13952.nupkg` (from MyGet)?
Author
Owner

@rudi-brunner commented on GitHub (Nov 6, 2020):

Thanks for the quick response :)
I'm not familiar with MyGet. Is it myget.org? Created an account now. But how do I access your feed? Any URL I can use?

@rudi-brunner commented on GitHub (Nov 6, 2020): Thanks for the quick response :) I'm not familiar with MyGet. Is it myget.org? Created an account now. But how do I access your feed? Any URL I can use?
Author
Owner

@StefH commented on GitHub (Nov 6, 2020):

See https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@StefH commented on GitHub (Nov 6, 2020): See https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Author
Owner

@rudi-brunner commented on GitHub (Nov 6, 2020):

Works like a charm 🥇
Verified that it works both in the minimum test case which I posted here and in my full-blown test setup.
Good to go 👌

@rudi-brunner commented on GitHub (Nov 6, 2020): Works like a charm 🥇 Verified that it works both in the minimum test case which I posted here and in my full-blown test setup. Good to go 👌
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#311