• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Aldaviva / Unfucked / 23378203323

21 Mar 2026 10:59AM UTC coverage: 35.442% (-11.7%) from 47.183%
23378203323

push

github

Aldaviva
Seal all possible classes for allegedly higher performance, since they weren't actually subclassable anyway due to C# not making methods virtual by default. If this change does more harm than good, blame Stephen Toub.

573 of 1629 branches covered (35.17%)

14 of 72 new or added lines in 15 files covered. (19.44%)

488 existing lines in 30 files now uncovered.

975 of 2751 relevant lines covered (35.44%)

162.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/HTTP/Extensions.cs
1
using System.Diagnostics;
2
using System.Diagnostics.Contracts;
3
using System.Text;
4
using Unfucked.HTTP.Config;
5
using Unfucked.HTTP.Exceptions;
6
using Unfucked.HTTP.Filters;
7
using Unfucked.HTTP.Serialization;
8

9
namespace Unfucked.HTTP;
10

11
public static class Extensions {
12

13
    #region Targeting
14

15
    /// <summary>
16
    /// Begin building an HTTP request for a given URL
17
    /// </summary>
18
    /// <param name="httpClient">HTTP client</param>
19
    /// <param name="uri">URL to send request to</param>
20
    /// <returns>Web target, on which you can call builder methods to set up the request, ending in one of the verb methods like <c>Get</c> to send the built request</returns>
21
    [Pure]
22
    public static IWebTarget Target(this HttpClient httpClient, Uri uri) => new WebTarget(HttpClientWrapper.Wrap(httpClient), uri);
×
23

24
    /// <inheritdoc cref="Target(System.Net.Http.HttpClient,System.Uri)" />
25
    [Pure]
26
    public static IWebTarget Target<H>(this H httpClient, Uri uri) where H: IHttpClient => new WebTarget(httpClient, uri);
×
27

28
    /// <inheritdoc cref="Target(System.Net.Http.HttpClient,System.Uri)" />
29
    /// <param name="isTemplate"><c>false</c> if <paramref name="uri"/> is a regular URI, or <c>true</c> if it is a URI Template with <c>{placeholders}</c></param>
30
    [Pure]
31
    public static IWebTarget Target(this HttpClient httpClient, string uri, bool isTemplate = false) {
UNCOV
32
        IHttpClient client = HttpClientWrapper.Wrap(httpClient);
×
UNCOV
33
        return isTemplate ? new WebTarget(client, UrlBuilder.FromTemplate(uri)) : new WebTarget(client, uri);
×
34
    }
35

36
    /// <inheritdoc cref="Target(System.Net.Http.HttpClient,string,bool)" />
37
    [Pure]
38
    public static IWebTarget Target<H>(this H httpClient, string uri, bool isTemplate = false) where H: IHttpClient =>
UNCOV
39
        isTemplate ? new WebTarget(httpClient, UrlBuilder.FromTemplate(uri)) : new WebTarget(httpClient, uri);
×
40

41
    /// <summary>
42
    /// Begin building an HTTP request for a given URL
43
    /// </summary>
44
    /// <param name="httpClient">HTTP client</param>
45
    /// <param name="urlBuilder">URL to send request to</param>
46
    /// <returns>Web target, on which you can call builder methods to set up the request, ending in one of the verb methods like <c>Get</c> to send the built request</returns>
47
    [Pure]
UNCOV
48
    public static IWebTarget Target(this HttpClient httpClient, UrlBuilder urlBuilder) => new WebTarget(HttpClientWrapper.Wrap(httpClient), urlBuilder);
×
49

50
    /// <inheritdoc cref="Target(System.Net.Http.HttpClient,UrlBuilder)" />
51
    [Pure]
UNCOV
52
    public static IWebTarget Target<H>(this H httpClient, UrlBuilder urlBuilder) where H: IHttpClient => new WebTarget(httpClient, urlBuilder);
×
53

54
    /// <summary>
55
    /// Begin building an HTTP request for a given URL
56
    /// </summary>
57
    /// <param name="httpClient">HTTP client</param>
58
    /// <param name="uriBuilder">URL to send request to</param>
59
    /// <returns>Web target, on which you can call builder methods to set up the request, ending in one of the verb methods like <c>Get</c> to send the built request</returns>
60
    [Pure]
UNCOV
61
    public static IWebTarget Target(this HttpClient httpClient, UriBuilder uriBuilder) => new WebTarget(HttpClientWrapper.Wrap(httpClient), uriBuilder);
×
62

63
    /// <inheritdoc cref="Target(System.Net.Http.HttpClient,UriBuilder)" />
64
    [Pure]
UNCOV
65
    public static IWebTarget Target<H>(this H httpClient, UriBuilder uriBuilder) where H: IHttpClient => new WebTarget(httpClient, uriBuilder);
×
66

67
    #endregion
68

69
    #region Configuration
70

71
    /// <inheritdoc cref="Register(IHttpClient,ClientRequestFilter,int)" />
72
    public static H Register<H>(this H httpClient, ClientRequestFilter filter, int position = ClientConfig.LAST_FILTER_POSITION) where H: HttpClient {
UNCOV
73
        _ = UnfuckedHttpHandler.FindHandler(httpClient)?.Register(filter, position) ?? throw WebTarget.ConfigUnavailable;
×
UNCOV
74
        return httpClient;
×
75
    }
76

77
    /// <summary>Register a client request filter to run before requests are sent for this client</summary>
78
    /// <returns>This mutated instance</returns>
79
    /// <exception cref="InvalidOperationException">the <paramref name="httpClient"/> does not have a usable configuration because of how it was constructed</exception>
80
    public static IHttpClient Register(this IHttpClient httpClient, ClientRequestFilter filter, int position = ClientConfig.LAST_FILTER_POSITION) {
UNCOV
81
        _ = httpClient.Handler?.Register(filter, position) ?? throw WebTarget.ConfigUnavailable;
×
82
        return httpClient;
×
83
    }
84

85
    /// <inheritdoc cref="Register(IHttpClient,ClientResponseFilter,int)" />
86
    public static H Register<H>(this H httpClient, ClientResponseFilter filter, int position = ClientConfig.LAST_FILTER_POSITION) where H: HttpClient {
UNCOV
87
        _ = UnfuckedHttpHandler.FindHandler(httpClient)?.Register(filter, position) ?? throw WebTarget.ConfigUnavailable;
×
UNCOV
88
        return httpClient;
×
89
    }
90

91
    /// <summary>Register a client response filter to run after requests are received for this client</summary>
92
    /// <returns>This mutated instance</returns>
93
    /// <exception cref="InvalidOperationException">the <paramref name="httpClient"/> does not have a usable configuration because of how it was constructed</exception>
94
    public static IHttpClient Register(this IHttpClient httpClient, ClientResponseFilter filter, int position = ClientConfig.LAST_FILTER_POSITION) {
UNCOV
95
        _ = httpClient.Handler?.Register(filter, position) ?? throw WebTarget.ConfigUnavailable;
×
96
        return httpClient;
×
97
    }
98

99
    /// <inheritdoc cref="Register(IHttpClient,MessageBodyReader)" />
100
    public static H Register<H>(this H httpClient, MessageBodyReader reader) where H: HttpClient {
UNCOV
101
        _ = UnfuckedHttpHandler.FindHandler(httpClient)?.Register(reader) ?? throw WebTarget.ConfigUnavailable;
×
UNCOV
102
        return httpClient;
×
103
    }
104

105
    /// <summary>Register a message body reader to deserialize responses for this client</summary>
106
    /// <returns>This mutated instance</returns>
107
    /// <exception cref="InvalidOperationException">the <paramref name="httpClient"/> does not have a usable configuration because of how it was constructed</exception>
108
    public static IHttpClient Register(this IHttpClient httpClient, MessageBodyReader reader) {
UNCOV
109
        _ = httpClient.Handler?.Register(reader) ?? throw WebTarget.ConfigUnavailable;
×
110
        return httpClient;
×
111
    }
112

113
    /// <inheritdoc cref="Property{T}(IHttpClient,PropertyKey{T},T)" />
114
    public static H Property<H, T>(this H httpClient, PropertyKey<T> key, T? newValue) where H: HttpClient where T: notnull {
UNCOV
115
        _ = UnfuckedHttpHandler.FindHandler(httpClient)?.Property(key, newValue) ?? throw WebTarget.ConfigUnavailable;
×
UNCOV
116
        return httpClient;
×
117
    }
118

119
    /// <summary>Set a property on the client.</summary>
120
    /// <exception cref="InvalidOperationException">the <paramref name="httpClient"/> does not have a usable configuration because of how it was constructed</exception>
121
    public static IHttpClient Property<T>(this IHttpClient httpClient, PropertyKey<T> key, T? newValue) where T: notnull {
UNCOV
122
        _ = httpClient.Handler?.Property(key, newValue) ?? throw WebTarget.ConfigUnavailable;
×
UNCOV
123
        return httpClient;
×
124
    }
125

126
    /// <inheritdoc cref="Property{T}(IHttpClient,PropertyKey{T},out T)" />
127
    [Pure]
128
    public static bool Property<H, T>(this H httpClient, PropertyKey<T> key, out T? existingValue) where H: HttpClient where T: notnull =>
UNCOV
129
        UnfuckedHttpHandler.FindHandler(httpClient) is {} handler ? handler.Property(key, out existingValue) : throw WebTarget.ConfigUnavailable;
×
130

131
    /// <summary>Get a property from the client.</summary>
132
    /// <exception cref="InvalidOperationException">the <paramref name="httpClient"/> does not have a usable configuration because of how it was constructed</exception>
133
    [Pure]
134
    public static bool Property<T>(this IHttpClient httpClient, PropertyKey<T> key, out T? existingValue) where T: notnull =>
UNCOV
135
        httpClient.Handler is {} handler ? handler.Property(key, out existingValue) : throw WebTarget.ConfigUnavailable;
×
136

137
    #endregion
138

139
    /// <summary>
140
    /// <para>Immediately throw a <see cref="WebApplicationException"/> if this HTTP response did not have a successful status code.</para>
141
    /// <para>This is a per-request, imperative alternative to leaving <see cref="PropertyKey.ThrowOnUnsuccessfulStatusCode"/> set to <c>true</c> on your <see cref="IClientConfig"/>, <see cref="IUnfuckedHttpHandler"/>, <see cref="HttpClient"/>, or <see cref="IWebTarget"/>, which applies to all requests sent. Since that property defaults to <c>true</c>, calling this method is only useful if you manually changed that property to <c>false</c>.</para>
142
    /// </summary>
143
    /// <param name="response">completed HTTP response</param>
144
    /// <param name="cancellationToken">cancel reading response body</param>
145
    /// <returns>a Task that resolves with no return value if if the response status code is successful</returns>
146
    /// <exception cref="WebApplicationException">the response status code was not successful</exception>
UNCOV
147
    public static Task ThrowIfUnsuccessful(this HttpResponseMessage response, CancellationToken cancellationToken = default) => WebTarget.ThrowIfUnsuccessful(response, cancellationToken);
×
148

149
    extension(HttpContent httpContent) {
150

151
        // Copied from .NET BCL JsonHelpers.GetEncoding
152
        internal Encoding? Encoding {
153
            get {
NEW
154
                Encoding? encoding = null;
×
155

NEW
156
                if (httpContent.Headers.ContentType?.CharSet is {} charset) {
×
157
                    try {
158
                        // Remove at most a single set of quotes.
NEW
159
                        if (charset.Length > 2 && charset[0] == '\"' && charset[charset.Length - 1] == '\"') {
×
NEW
160
                            encoding = Encoding.GetEncoding(charset.Substring(1, charset.Length - 2));
×
161
                        } else {
NEW
162
                            encoding = Encoding.GetEncoding(charset);
×
163
                        }
NEW
164
                    } catch (ArgumentException e) {
×
165
                        // leave encoding null
NEW
166
                    }
×
167

168
                    Debug.Assert(encoding != null);
169
                }
170

NEW
171
                return encoding;
×
172
            }
173
        }
174

175
    }
176

177
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc