Issue : creating response using .WithBody(Func<RequestMessage, string>...) and .WithStatusCode #141

Closed
opened 2025-12-29 08:22:46 +01:00 by adam · 7 comments
Owner

Originally created by @rickm-pra on GitHub (Sep 4, 2018).

Originally assigned to: @StefH on GitHub.

If I set up a response using both .WithBody and .WithStatusCode, it always returns a status code of 200. For example:

            _mock.Given(
                Request.Create()
                    .UsingPut()
                    .WithPath("/myendpoint")
            ).RespondWith(
                Response.Create()
                    .WithStatusCode(500)
                    .WithBody(requestMessage =>
                    {
                        var returnStr = JsonConvert.SerializeObject(new
                        {
                            Message = "Test error"
                        });
                        return returnStr;
                    })
                    );

will return the error message but with a 200 status.

It looks like the root cause is that the response callback is creating a new response instead of using the one that was configured.

Originally created by @rickm-pra on GitHub (Sep 4, 2018). Originally assigned to: @StefH on GitHub. If I set up a response using both .WithBody and .WithStatusCode, it always returns a status code of 200. For example: ``` _mock.Given( Request.Create() .UsingPut() .WithPath("/myendpoint") ).RespondWith( Response.Create() .WithStatusCode(500) .WithBody(requestMessage => { var returnStr = JsonConvert.SerializeObject(new { Message = "Test error" }); return returnStr; }) ); ``` will return the error message but with a 200 status. It looks like the root cause is that the response callback is creating a new response instead of using the one that was configured.
adam added the bug label 2025-12-29 08:22:46 +01:00
adam closed this issue 2025-12-29 08:22:46 +01:00
Author
Owner

@StefH commented on GitHub (Sep 4, 2018):

Do you have multiple mappings defined, or just this one?

In case you have multiple, it could be that another mapping does match better than this one.

@StefH commented on GitHub (Sep 4, 2018): Do you have multiple mappings defined, or just this one? In case you have multiple, it could be that another mapping does match better than this one.
Author
Owner

@rickm-pra commented on GitHub (Sep 4, 2018):

I tried it both ways. The number of mappings has no effect.

@rickm-pra commented on GitHub (Sep 4, 2018): I tried it both ways. The number of mappings has no effect.
Author
Owner

@rickm-pra commented on GitHub (Sep 4, 2018):

My crude fix was to change ProvideResponseAsync in Response.cs:

             if (Callback != null)
            {
                var message = Callback(requestMessage);
                message.StatusCode = ResponseMessage.StatusCode;
                return message;
            }

I doubt very much this is the right fix, but it does set the code properly.

@rickm-pra commented on GitHub (Sep 4, 2018): My crude fix was to change ProvideResponseAsync in Response.cs: ``` if (Callback != null) { var message = Callback(requestMessage); message.StatusCode = ResponseMessage.StatusCode; return message; } ``` I doubt very much this is the right fix, but it does set the code properly.
Author
Owner

@StefH commented on GitHub (Sep 4, 2018):

This is indeed a bug. Thanks for reporting. I'll investigate and fix this.

@StefH commented on GitHub (Sep 4, 2018): This is indeed a bug. Thanks for reporting. I'll investigate and fix this.
Author
Owner

@rickm-pra commented on GitHub (Sep 4, 2018):

Thanks!

@rickm-pra commented on GitHub (Sep 4, 2018): Thanks!
Author
Owner

@StefH commented on GitHub (Sep 4, 2018):

Issue is solved, I created a PR , take a look here:
https://github.com/WireMock-Net/WireMock.Net/pull/199

You can review but probably not approve this PR?

@StefH commented on GitHub (Sep 4, 2018): Issue is solved, I created a PR , take a look here: https://github.com/WireMock-Net/WireMock.Net/pull/199 You can review but probably not approve this PR?
Author
Owner

@rickm-pra commented on GitHub (Sep 4, 2018):

I can review, but it's not a binding approve. Looks good though!

@rickm-pra commented on GitHub (Sep 4, 2018): I can review, but it's not a binding approve. Looks good though!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#141