Skip to content

Commit 78cf49b

Browse files
All tests pass exception for .NET Framework
1 parent c4612a0 commit 78cf49b

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace RestClient.Net
4+
{
5+
public class MissingHeaderException : Exception
6+
{
7+
public bool IsRequest { get; }
8+
9+
public MissingHeaderException(
10+
string message,
11+
bool isRequest,
12+
Exception innerException) :
13+
base(message, innerException) => IsRequest = isRequest;
14+
}
15+
}

src/RestClient.Net.UnitTests/MainUnitTests.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ public MainUnitTests()
288288
#region Tests
289289

290290
#region External Api Tests
291+
292+
#if !NET7_0_OR_GREATER
293+
294+
//TODO: Enable this for .NET 7 and up when the bugs get fixed
295+
296+
291297
[TestMethod]
292298
public async Task TestHead()
293299
{
@@ -309,6 +315,7 @@ public async Task TestHead()
309315

310316
Assert.AreEqual(GoogleHeadHeaders[CacheControlHeaderName], response.Headers[CacheControlHeaderName].Single());
311317
}
318+
#endif
312319

313320
#if !NET45
314321

@@ -617,10 +624,13 @@ public async Task TestUpdate(HttpRequestMethod httpRequestMethod)
617624
#endif
618625
}
619626

627+
#if !NET7_0_OR_GREATER
628+
629+
//TODO: Enable this for .NET 7 and up when the bugs get fixed
630+
620631
[TestMethod]
621632
public async Task TestConsoleLogging()
622633
{
623-
//var logger = new ConsoleLogger();
624634
using var client = new Client(
625635
new NewtonsoftSerializationAdapter(),
626636
baseUrl: JsonPlaceholderBaseUri,
@@ -633,6 +643,27 @@ public async Task TestConsoleLogging()
633643
Assert.AreEqual(101, response.Body?.Id);
634644
Assert.AreEqual(201, response.StatusCode);
635645
}
646+
#else
647+
/// <summary>
648+
/// This relates to this issue: https://github.com/dotnet/runtime/issues/81506
649+
/// This should eventually get fixed but even if it doesn't this proves that RC Net at least
650+
/// throws a meaningful exception
651+
/// </summary>
652+
[TestMethod]
653+
public async Task TestDotNet7ErrorHandlingNoRequestHeader()
654+
{
655+
using var client = new Client(
656+
new NewtonsoftSerializationAdapter(),
657+
baseUrl: JsonPlaceholderBaseUri,
658+
createHttpClient: GetCreateHttpClient(),
659+
logger: consoleLoggerFactory.CreateLogger<Client>());
660+
661+
var missingHeaderException = await Assert.ThrowsExceptionAsync<MissingHeaderException>(async () =>
662+
await client.PostAsync<PostUserResponse, UserPost>(_userRequestBody, JsonPlaceholderPostsSlug));
663+
664+
Assert.AreEqual("The media type header is missing. The request is missing the Content-Type header", missingHeaderException.Message);
665+
}
666+
#endif
636667

637668
[TestMethod]
638669
public async Task TestGetWithXmlSerialization()

src/RestClient.Net/Client.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ public async Task<Response<TResponseBody>> SendAsync<TResponseBody, TRequestBody
262262
logger.LogError(tce, Messages.ErrorTaskCancelled, request);
263263
throw;
264264
}
265+
catch (MissingHeaderException mhe)
266+
{
267+
logger.LogError(mhe, mhe.Message, request);
268+
throw;
269+
}
265270
catch (Exception ex)
266271
{
267272
var exception = new SendException(Messages.ErrorSendException, request, ex);

src/RestClient.Net/DefaultSendHttpRequestMessage.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,31 @@ public async Task<HttpResponseMessage> SendHttpRequestMessage<TRequestBody>(
2929

3030
logger.LogTrace(Messages.InfoAttemptingToSend, request);
3131

32-
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, request.CancellationToken).ConfigureAwait(false);
3332

34-
logger.LogInformation(Messages.InfoSendReturnedNoException);
33+
try
34+
{
35+
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, request.CancellationToken).ConfigureAwait(false);
3536

36-
return httpResponseMessage;
37+
logger.LogInformation(Messages.InfoSendReturnedNoException);
38+
39+
return httpResponseMessage;
40+
41+
}
42+
catch (ArgumentException aex)
43+
{
44+
if (aex.Message == "The value cannot be null or empty. (Parameter 'mediaType')")
45+
{
46+
var isRequest = httpRequestMessage.Content?.Headers.ContentType == null;
47+
var errorTypePart = isRequest ? $"The request is missing the {HeadersExtensions.ContentTypeHeaderName} header" :
48+
$"The request has the {HeadersExtensions.ContentTypeHeaderName} header so perhaps the response doesn't include it";
49+
throw
50+
new MissingHeaderException(
51+
$"The media type header is missing. {errorTypePart}",
52+
isRequest,
53+
aex); ;
54+
}
55+
throw;
56+
}
3757
}
3858
catch (OperationCanceledException oce)
3959
{

0 commit comments

Comments
 (0)