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

loresoft / FluentRest / 13399786835

18 Feb 2025 08:48PM UTC coverage: 57.756% (-0.4%) from 58.107%
13399786835

push

github

pwelter34
add support for nullable, minor cleanup, improve UrlBuilder

293 of 646 branches covered (45.36%)

Branch coverage included in aggregate %.

238 of 380 new or added lines in 20 files covered. (62.63%)

3 existing lines in 3 files now uncovered.

824 of 1288 relevant lines covered (63.98%)

56.98 hits per line

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

18.07
/src/FluentRest/HeaderBuilder.cs
1
using System.Net.Http.Headers;
2

3
namespace FluentRest;
4

5
/// <summary>
6
/// Fluent header builder
7
/// </summary>
8
public class HeaderBuilder : HeaderBuilder<HeaderBuilder>
9
{
10
    /// <summary>
11
    /// Initializes a new instance of the <see cref="HeaderBuilder"/> class.
12
    /// </summary>
13
    /// <param name="requestMessage">The fluent HTTP request being built.</param>
14
    public HeaderBuilder(HttpRequestMessage requestMessage) : base(requestMessage)
16✔
15
    {
16
    }
16✔
17
}
18

19
/// <summary>
20
/// Fluent header builder
21
/// </summary>
22
/// <typeparam name="TBuilder">The type of the builder.</typeparam>
23
public abstract class HeaderBuilder<TBuilder> : RequestBuilder<TBuilder>
24
        where TBuilder : HeaderBuilder<TBuilder>
25
{
26
    /// <summary>
27
    /// Initializes a new instance of the <see cref="HeaderBuilder{TBuilder}"/> class.
28
    /// </summary>
29
    /// <param name="requestMessage">The fluent HTTP request being built.</param>
30
    protected HeaderBuilder(HttpRequestMessage requestMessage) : base(requestMessage)
16✔
31
    {
32
    }
16✔
33

34
    /// <summary>
35
    /// Append the media-type to the Accept header for an HTTP request.
36
    /// </summary>
37
    /// <param name="mediaType">The media-type header value.</param>
38
    /// <param name="quality">The quality associated with the header value.</param>
39
    /// <returns>A fluent header builder.</returns>
40
    public TBuilder Accept(string mediaType, double? quality = null)
41
    {
42

43
        var header = quality.HasValue
8!
44
            ? new MediaTypeWithQualityHeaderValue(mediaType, quality.Value)
8✔
45
            : new MediaTypeWithQualityHeaderValue(mediaType);
8✔
46

47
        RequestMessage.Headers.Accept.Add(header);
8✔
48

49
        return (TBuilder)this;
8✔
50
    }
51

52
    /// <summary>
53
    /// Append the value to the Accept-Charset header for an HTTP request.
54
    /// </summary>
55
    /// <param name="value">The header value.</param>
56
    /// <param name="quality">The quality associated with the header value.</param>
57
    /// <returns>A fluent header builder.</returns>
58
    public TBuilder AcceptCharset(string value, double? quality = null)
59
    {
60
        var header = quality.HasValue
×
61
            ? new StringWithQualityHeaderValue(value, quality.Value)
×
62
            : new StringWithQualityHeaderValue(value);
×
63

64
        RequestMessage.Headers.AcceptCharset.Add(header);
×
65

NEW
66
        return (TBuilder)this;
×
67
    }
68

69
    /// <summary>
70
    /// Append the value to the Accept-Encoding header for an HTTP request.
71
    /// </summary>
72
    /// <param name="value">The header value.</param>
73
    /// <param name="quality">The quality associated with the header value.</param>
74
    /// <returns>A fluent header builder.</returns>
75
    public TBuilder AcceptEncoding(string value, double? quality = null)
76
    {
77
        var header = quality.HasValue
×
78
            ? new StringWithQualityHeaderValue(value, quality.Value)
×
79
            : new StringWithQualityHeaderValue(value);
×
80

81
        RequestMessage.Headers.AcceptEncoding.Add(header);
×
82

NEW
83
        return (TBuilder)this;
×
84
    }
85

86
    /// <summary>
87
    /// Append the value to the Accept-Language header for an HTTP request.
88
    /// </summary>
89
    /// <param name="value">The header value.</param>
90
    /// <param name="quality">The quality associated with the header value.</param>
91
    /// <returns>A fluent header builder.</returns>
92
    public TBuilder AcceptLanguage(string value, double? quality = null)
93
    {
94
        var header = quality.HasValue
×
95
            ? new StringWithQualityHeaderValue(value, quality.Value)
×
96
            : new StringWithQualityHeaderValue(value);
×
97

98
        RequestMessage.Headers.AcceptLanguage.Add(header);
×
99

NEW
100
        return (TBuilder)this;
×
101
    }
102

103
    /// <summary>
104
    /// Sets the value of the Authorization header for an HTTP request.
105
    /// </summary>
106
    /// <param name="scheme">The scheme to use for authorization.</param>
107
    /// <param name="parameter">The credentials containing the authentication information.</param>
108
    /// <returns>A fluent header builder.</returns>
109
    /// <exception cref="System.ArgumentNullException"></exception>
110
    public TBuilder Authorization(string scheme, string? parameter = null)
111
    {
112
        if (scheme is null)
12!
113
            throw new ArgumentNullException(nameof(scheme));
×
114

115
        var header = new AuthenticationHeaderValue(scheme, parameter);
12✔
116

117
        RequestMessage.Headers.Authorization = header;
12✔
118

119
        return (TBuilder)this;
12✔
120
    }
121

122
    /// <summary>
123
    /// Sets the value of the Cache-Control header for an HTTP request.
124
    /// </summary>
125
    /// <param name="value">The header value.</param>
126
    /// <returns>A fluent header builder.</returns>
127
    public TBuilder CacheControl(string? value)
128
    {
129
        var header = CacheControlHeaderValue.Parse(value);
×
130

131
        RequestMessage.Headers.CacheControl = header;
×
132

NEW
133
        return (TBuilder)this;
×
134
    }
135

136
    /// <summary>
137
    /// Append the value of the Expect header for an HTTP request.
138
    /// </summary>
139
    /// <param name="name">The header name.</param>
140
    /// <param name="value">The header value.</param>
141
    /// <returns>A fluent header builder.</returns>
142
    public TBuilder Expect(string name, string? value = null)
143
    {
144
        var header = new NameValueWithParametersHeaderValue(name, value);
×
145

146
        RequestMessage.Headers.Expect.Add(header);
×
147

NEW
148
        return (TBuilder)this;
×
149
    }
150

151
    /// <summary>
152
    /// Sets the value of the From header for an HTTP request.
153
    /// </summary>
154
    /// <param name="value">The header value.</param>
155
    /// <returns>A fluent header builder.</returns>
156
    public TBuilder From(string? value)
157
    {
158
        RequestMessage.Headers.From = value;
×
NEW
159
        return (TBuilder)this;
×
160
    }
161

162
    /// <summary>
163
    /// Sets the value of the Host header for an HTTP request.
164
    /// </summary>
165
    /// <param name="value">The header value.</param>
166
    /// <returns>A fluent header builder.</returns>
167
    public TBuilder Host(string? value)
168
    {
169
        RequestMessage.Headers.Host = value;
×
NEW
170
        return (TBuilder)this;
×
171
    }
172

173
    /// <summary>
174
    /// Sets the value of the If-Modified-Since header for an HTTP request.
175
    /// </summary>
176
    /// <param name="modifiedDate">The modified date.</param>
177
    /// <returns>A fluent header builder.</returns>
178
    public TBuilder IfModifiedSince(DateTimeOffset? modifiedDate)
179
    {
180
        RequestMessage.Headers.IfModifiedSince = modifiedDate;
×
NEW
181
        return (TBuilder)this;
×
182
    }
183

184
    /// <summary>
185
    /// Sets the value of the If-Unmodified-Since header for an HTTP request.
186
    /// </summary>
187
    /// <param name="modifiedDate">The modified date.</param>
188
    /// <returns>A fluent header builder.</returns>
189
    public TBuilder IfUnmodifiedSince(DateTimeOffset? modifiedDate)
190
    {
191
        RequestMessage.Headers.IfUnmodifiedSince = modifiedDate;
×
NEW
192
        return (TBuilder)this;
×
193
    }
194

195
    /// <summary>
196
    /// Sets the value of the Proxy-Authorization header for an HTTP request.
197
    /// </summary>
198
    /// <param name="scheme">The scheme to use for authorization.</param>
199
    /// <param name="parameter">The credentials containing the authentication information.</param>
200
    /// <returns>A fluent header builder.</returns>
201
    /// <exception cref="ArgumentNullException"><paramref name="scheme"/> is <see langword="null"/></exception>
202
    public TBuilder ProxyAuthorization(string scheme, string? parameter = null)
203
    {
NEW
204
        if (scheme is null)
×
205
            throw new ArgumentNullException(nameof(scheme));
×
206

207
        var header = new AuthenticationHeaderValue(scheme, parameter);
×
208
        RequestMessage.Headers.ProxyAuthorization = header;
×
209

NEW
210
        return (TBuilder)this;
×
211
    }
212

213
    /// <summary>
214
    /// Sets the value of the Range header for an HTTP request.
215
    /// </summary>
216
    /// <param name="from">The position at which to start sending data.</param>
217
    /// <param name="to">The position at which to stop sending data.</param>
218
    /// <returns>A fluent header builder.</returns>
219
    public TBuilder Range(long? from, long? to)
220
    {
221
        var header = new RangeHeaderValue(from, to);
×
222
        RequestMessage.Headers.Range = header;
×
223

NEW
224
        return (TBuilder)this;
×
225
    }
226

227
    /// <summary>
228
    /// Sets the value of the Referrer header for an HTTP request.
229
    /// </summary>
230
    /// <param name="uri">The header URI.</param>
231
    /// <returns>A fluent header builder.</returns>
232
    public TBuilder Referrer(Uri? uri)
233
    {
234
        RequestMessage.Headers.Referrer = uri;
×
NEW
235
        return (TBuilder)this;
×
236
    }
237

238
    /// <summary>
239
    /// Sets the value of the Referrer header for an HTTP request.
240
    /// </summary>
241
    /// <param name="value">The header value.</param>
242
    /// <returns>A fluent header builder.</returns>
243
    public TBuilder Referrer(string? value)
244
    {
245
        if (string.IsNullOrEmpty(value))
×
NEW
246
            return (TBuilder)this;
×
247

248
        var uri = new Uri(value);
×
249
        RequestMessage.Headers.Referrer = uri;
×
250

NEW
251
        return (TBuilder)this;
×
252
    }
253

254
    /// <summary>
255
    /// Sets the value of the User-Agent header for an HTTP request.
256
    /// </summary>
257
    /// <param name="value">The header value.</param>
258
    /// <returns>A fluent header builder.</returns>
259
    public TBuilder UserAgent(string? value)
260
    {
261
        if (string.IsNullOrEmpty(value))
×
NEW
262
            return (TBuilder)this;
×
263

264
        var header = ProductInfoHeaderValue.Parse(value);
×
265
        RequestMessage.Headers.UserAgent.Add(header);
×
266

NEW
267
        return (TBuilder)this;
×
268
    }
269

270
    /// <summary>
271
    /// Sets the value of the X-HTTP-Method-Override header for an HTTP request.
272
    /// </summary>
273
    /// <param name="method">The HTTP method.</param>
274
    /// <returns>A fluent header builder.</returns>
275
    public TBuilder MethodOverride(HttpMethod? method)
276
    {
NEW
277
        RequestMessage.Headers.Add(HttpRequestHeaders.MethodOverride, method?.ToString());
×
NEW
278
        return (TBuilder)this;
×
279
    }
280
}
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