-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: when setting any content header after setting the HttpContent wi…
…th JsonBody (or any other custom IResponseBehavior) it would reset the HttpContent to an empty content. (#99)
- Loading branch information
Showing
3 changed files
with
69 additions
and
10 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/MockHttp/Language/Flow/Response/EnsureHttpContentBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using MockHttp.Responses; | ||
|
||
namespace MockHttp.Language.Flow.Response; | ||
|
||
internal sealed class EnsureHttpContentBehavior | ||
: IResponseBehavior | ||
{ | ||
public Task HandleAsync | ||
( | ||
MockHttpRequestContext requestContext, | ||
HttpResponseMessage responseMessage, | ||
ResponseHandlerDelegate next, | ||
CancellationToken cancellationToken) | ||
{ | ||
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract | ||
responseMessage.Content ??= new EmptyContent(); | ||
return next(requestContext, responseMessage, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Net; | ||
using System.Text; | ||
using MockHttp.FluentAssertions; | ||
using Newtonsoft.Json; | ||
|
||
namespace MockHttp.Json.Issues; | ||
|
||
public class Issue98 | ||
{ | ||
[Fact] | ||
public async Task When_setting_content_header_after_setting_jsonBody_it_should_return_expected_response() | ||
{ | ||
using var mockHttp = new MockHttpHandler(); | ||
mockHttp | ||
.When(m => m | ||
.Method(HttpMethod.Post) | ||
.RequestUri("api/login") | ||
.ContentType("application/json; charset=utf-8") | ||
.JsonBody(new { username = @"corp\user", password = "Super.Mari0.Bro$$" }) | ||
) | ||
.Respond(r => r | ||
.StatusCode(HttpStatusCode.OK) | ||
.JsonBody(new { username = "user@corp" }, Encoding.UTF8) | ||
.Header("Set-Cookie", | ||
"session=abcdefghi==; Expires=Tue, 01 Feb 2024 01:01:01 GMT; Secure; HttpOnly; Path=/") | ||
) | ||
.Verifiable(); | ||
|
||
var client = new HttpClient(mockHttp) | ||
{ | ||
BaseAddress = new Uri("http://localhost") | ||
}; | ||
// Act | ||
HttpResponseMessage? response = await client.PostAsJsonAsync("api/login", new { username = @"corp\user", password = @"Super.Mari0.Bro$$" }); | ||
|
||
// Assert | ||
response.Should().HaveStatusCode(HttpStatusCode.OK); | ||
response.Headers.Should() | ||
.ContainKey("Set-Cookie", | ||
"session=abcdefghi==; Expires=Tue, 01 Feb 2024 01:01:01 GMT; Secure; HttpOnly; Path=/"); | ||
response.Should().HaveContentType("application/json; charset=utf-8"); | ||
await response.Should().HaveContentAsync(JsonConvert.SerializeObject(new { username = "user@corp" }), Encoding.UTF8); | ||
mockHttp.Verify(); | ||
} | ||
} |