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

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

Closed
rickm-pra opened this issue Sep 4, 2018 · 7 comments
Assignees
Labels

Comments

@rickm-pra
Copy link

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.

@StefH
Copy link
Collaborator

StefH commented 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.

@rickm-pra
Copy link
Author

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

@rickm-pra
Copy link
Author

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.

@StefH
Copy link
Collaborator

StefH commented Sep 4, 2018

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

@StefH StefH self-assigned this Sep 4, 2018
@StefH StefH added the bug label Sep 4, 2018
@StefH StefH changed the title Issue creating response using .WithBody and .WithStatusCode Issue : creating response using .WithBody(Func<RequestMessage, string>...) and .WithStatusCode Sep 4, 2018
@rickm-pra
Copy link
Author

Thanks!

StefH added a commit that referenced this issue Sep 4, 2018
@StefH
Copy link
Collaborator

StefH commented Sep 4, 2018

Issue is solved, I created a PR , take a look here:
#199

You can review but probably not approve this PR?

@rickm-pra
Copy link
Author

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

StefH added a commit that referenced this issue Sep 4, 2018
@StefH StefH closed this as completed Sep 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants