Skip to content

Commit bd79431

Browse files
committed
Add support for avoiding exceptions thrown in EnsureSuccessStatusCodeHttpMessageHandler and handling it yourself
1 parent b364a59 commit bd79431

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/Dibix.Http.Client/Client/HttpClientBuilderExtensions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
using System.Net.Http;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
25
using Dibix.Http.Client;
36

47
namespace Microsoft.Extensions.DependencyInjection
58
{
69
public static class HttpClientBuilderExtensions
710
{
8-
public static IHttpClientBuilder AddBuiltinHttpMessageHandlers(this IHttpClientBuilder builder)
11+
public static IHttpClientBuilder AddBuiltinHttpMessageHandlers(this IHttpClientBuilder builder, params Type[] excludedHandlers)
912
{
10-
AddHttpMessageHandler<TraceProxyHttpMessageHandler>(builder);
11-
AddHttpMessageHandler<EnsureSuccessStatusCodeHttpMessageHandler>(builder);
12-
AddHttpMessageHandler<TraceSourceHttpMessageHandler>(builder);
13+
AddHttpMessageHandler<TraceProxyHttpMessageHandler>(builder, excludedHandlers);
14+
AddHttpMessageHandler<EnsureSuccessStatusCodeHttpMessageHandler>(builder, excludedHandlers);
15+
AddHttpMessageHandler<TraceSourceHttpMessageHandler>(builder, excludedHandlers);
1316
return builder;
1417
}
1518

16-
private static void AddHttpMessageHandler<T>(IHttpClientBuilder builder) where T : DelegatingHandler, new()
19+
private static void AddHttpMessageHandler<T>(IHttpClientBuilder builder, IEnumerable<Type> excludedHandlers) where T : DelegatingHandler, new()
1720
{
21+
if (excludedHandlers.Contains(typeof(T)))
22+
return;
23+
1824
builder.AddHttpMessageHandler(() => new T());
1925
}
2026
}

src/Dibix.Http.Client/Client/HttpException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private protected HttpException(HttpRequestMessage request, string requestConten
2222
this.ResponseContentText = responseContentText;
2323
}
2424

25-
internal static async Task<HttpException> Create(HttpRequestMessage request, HttpResponseMessage response)
25+
public static async Task<HttpException> Create(HttpRequestMessage request, HttpResponseMessage response)
2626
{
2727
string requestContentText = null;
2828
if (request.Content != null)

tests/Dibix.Http.Client.Tests/HttpMessageFormattingTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class HttpMessageFormattingTests
1515
[TestMethod]
1616
public async Task HttpException_GetFormattedText_WithMaskedSecret()
1717
{
18-
MethodInfo createMethod = typeof(HttpException).SafeGetMethod("Create", BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpRequestMessage), typeof(HttpResponseMessage) });
18+
MethodInfo createMethod = typeof(HttpException).SafeGetMethod("Create", BindingFlags.Public | BindingFlags.Static, new[] { typeof(HttpRequestMessage), typeof(HttpResponseMessage) });
1919
HttpRequestMessage request = new HttpRequestMessage();
2020
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
2121
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
@@ -60,7 +60,7 @@ GET HTTP/1.1
6060
[TestMethod]
6161
public async Task HttpException_GetFormattedText_WithNoMaskedSecret()
6262
{
63-
MethodInfo createMethod = typeof(HttpException).SafeGetMethod("Create", BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpRequestMessage), typeof(HttpResponseMessage) });
63+
MethodInfo createMethod = typeof(HttpException).SafeGetMethod("Create", BindingFlags.Public | BindingFlags.Static, new[] { typeof(HttpRequestMessage), typeof(HttpResponseMessage) });
6464
HttpRequestMessage request = new HttpRequestMessage();
6565
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Secret!");
6666
request.Content = new FormUrlEncodedContent(new[]
@@ -96,7 +96,7 @@ GET HTTP/1.1
9696
[TestMethod]
9797
public async Task HttpException_GetFormattedText_WithUnmaskedSecret()
9898
{
99-
MethodInfo createMethod = typeof(HttpException).SafeGetMethod("Create", BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(HttpRequestMessage), typeof(HttpResponseMessage) });
99+
MethodInfo createMethod = typeof(HttpException).SafeGetMethod("Create", BindingFlags.Public | BindingFlags.Static, new[] { typeof(HttpRequestMessage), typeof(HttpResponseMessage) });
100100
HttpRequestMessage request = new HttpRequestMessage();
101101
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Okay");
102102

0 commit comments

Comments
 (0)