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

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

Closed
rudi-brunner opened this issue Nov 6, 2020 · 6 comments · Fixed by #535
Closed

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

rudi-brunner opened this issue Nov 6, 2020 · 6 comments · Fixed by #535
Labels

Comments

@rudi-brunner
Copy link

rudi-brunner commented 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:https://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.

@StefH
Copy link
Collaborator

StefH commented Nov 6, 2020

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

@StefH
Copy link
Collaborator

StefH commented 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
Copy link
Collaborator

StefH commented Nov 6, 2020

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

@rudi-brunner
Copy link
Author

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?

@StefH
Copy link
Collaborator

StefH commented Nov 6, 2020

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

@rudi-brunner
Copy link
Author

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 👌

StefH added a commit that referenced this issue Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants