Skip to content

Commit 55f889c

Browse files
Add a test for .net 7
1 parent 78cf49b commit 55f889c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/RestClient.Net.UnitTests/MainUnitTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,37 @@ public async Task TestDotNet7ErrorHandlingNoRequestHeader()
663663

664664
Assert.AreEqual("The media type header is missing. The request is missing the Content-Type header", missingHeaderException.Message);
665665
}
666+
667+
[TestMethod]
668+
public async Task TestDotNet7ErrorHandlingNoResponseHeader()
669+
{
670+
using MockHttpMessageHandler mockHttpMessageHandler = new();
671+
672+
_ = mockHttpMessageHandler.
673+
When(HttpMethod.Post, JsonPlaceholderBaseUriString + JsonPlaceholderPostsSlug).
674+
With(request => request?.Content?.Headers?.ContentType?.MediaType == HeadersExtensions.JsonMediaType).
675+
Respond(
676+
HttpStatusCode.Created,
677+
JsonPlaceholderPostHeaders,
678+
null,
679+
//This is the JSON that gets returned when the content type is empty
680+
"{\r\n" +
681+
" \"id\": 101\r\n" +
682+
"}"
683+
);
684+
685+
using var client = new Client(
686+
new NewtonsoftSerializationAdapter(),
687+
baseUrl: JsonPlaceholderBaseUri,
688+
createHttpClient: (n) => mockHttpMessageHandler.ToHttpClient(),
689+
defaultRequestHeaders: HeadersExtensions.FromJsonContentType(),
690+
logger: consoleLoggerFactory.CreateLogger<Client>());
691+
692+
var missingHeaderException = await Assert.ThrowsExceptionAsync<MissingHeaderException>(async () =>
693+
await client.PostAsync<PostUserResponse, UserPost>(_userRequestBody, JsonPlaceholderPostsSlug));
694+
695+
Assert.AreEqual("The media type header is missing. The request has the Content-Type header so perhaps the response doesn't include it", missingHeaderException.Message);
696+
}
666697
#endif
667698

668699
[TestMethod]

0 commit comments

Comments
 (0)