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

loresoft / FluentRest / 7318593291

25 Dec 2023 02:55AM UTC coverage: 52.206%. Remained the same
7318593291

Pull #158

github

web-flow
Merge 38e3e6116 into 7b58509ff
Pull Request #158: Bump xunit.runner.visualstudio from 2.5.4 to 2.5.6

182 of 438 branches covered (0.0%)

Branch coverage included in aggregate %.

516 of 899 relevant lines covered (57.4%)

86.76 hits per line

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

17.65
/src/FluentRest/HeaderBuilder.cs
1
using System;
2
using System.Net.Http;
3
using System.Net.Http.Headers;
4

5
namespace FluentRest;
6

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

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

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

45
        var header = quality.HasValue
12!
46
            ? new MediaTypeWithQualityHeaderValue(mediaType, quality.Value)
12✔
47
            : new MediaTypeWithQualityHeaderValue(mediaType);
12✔
48

49
        RequestMessage.Headers.Accept.Add(header);
12✔
50

51
        return this as TBuilder;
12✔
52
    }
53

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

66
        RequestMessage.Headers.AcceptCharset.Add(header);
×
67

68
        return this as TBuilder;
×
69
    }
70

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

83
        RequestMessage.Headers.AcceptEncoding.Add(header);
×
84

85
        return this as TBuilder;
×
86
    }
87

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

100
        RequestMessage.Headers.AcceptLanguage.Add(header);
×
101

102
        return this as TBuilder;
×
103
    }
104

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

117
        var header = new AuthenticationHeaderValue(scheme, parameter);
18✔
118

119
        RequestMessage.Headers.Authorization = header;
18✔
120

121
        return this as TBuilder;
18✔
122
    }
123

124
    /// <summary>
125
    /// Sets the value of the Cache-Control header for an HTTP request.
126
    /// </summary>
127
    /// <param name="value">The header value.</param>
128
    /// <returns>A fluent header builder.</returns>
129
    /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/></exception>
130
    public TBuilder CacheControl(string value)
131
    {
132
        if (value == null)
×
133
            throw new ArgumentNullException(nameof(value));
×
134

135
        var header = CacheControlHeaderValue.Parse(value);
×
136

137
        RequestMessage.Headers.CacheControl = header;
×
138

139
        return this as TBuilder;
×
140
    }
141

142
    /// <summary>
143
    /// Append the value of the Expect header for an HTTP request.
144
    /// </summary>
145
    /// <param name="name">The header name.</param>
146
    /// <param name="value">The header value.</param>
147
    /// <returns>A fluent header builder.</returns>
148
    public TBuilder Expect(string name, string value = null)
149
    {
150
        var header = new NameValueWithParametersHeaderValue(name, value);
×
151

152
        RequestMessage.Headers.Expect.Add(header);
×
153

154
        return this as TBuilder;
×
155
    }
156

157
    /// <summary>
158
    /// Sets the value of the From header for an HTTP request.
159
    /// </summary>
160
    /// <param name="value">The header value.</param>
161
    /// <returns>A fluent header builder.</returns>
162
    public TBuilder From(string value)
163
    {
164
        RequestMessage.Headers.From = value;
×
165
        return this as TBuilder;
×
166
    }
167

168
    /// <summary>
169
    /// Sets the value of the Host header for an HTTP request.
170
    /// </summary>
171
    /// <param name="value">The header value.</param>
172
    /// <returns>A fluent header builder.</returns>
173
    public TBuilder Host(string value)
174
    {
175
        RequestMessage.Headers.Host = value;
×
176
        return this as TBuilder;
×
177
    }
178

179
    /// <summary>
180
    /// Sets the value of the If-Modified-Since header for an HTTP request.
181
    /// </summary>
182
    /// <param name="modifiedDate">The modified date.</param>
183
    /// <returns>A fluent header builder.</returns>
184
    public TBuilder IfModifiedSince(DateTimeOffset? modifiedDate)
185
    {
186
        RequestMessage.Headers.IfModifiedSince = modifiedDate;
×
187
        return this as TBuilder;
×
188
    }
189

190
    /// <summary>
191
    /// Sets the value of the If-Unmodified-Since header for an HTTP request.
192
    /// </summary>
193
    /// <param name="modifiedDate">The modified date.</param>
194
    /// <returns>A fluent header builder.</returns>
195
    public TBuilder IfUnmodifiedSince(DateTimeOffset? modifiedDate)
196
    {
197
        RequestMessage.Headers.IfUnmodifiedSince = modifiedDate;
×
198
        return this as TBuilder;
×
199
    }
200

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

213
        var header = new AuthenticationHeaderValue(scheme, parameter);
×
214
        RequestMessage.Headers.ProxyAuthorization = header;
×
215

216
        return this as TBuilder;
×
217
    }
218

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

230
        return this as TBuilder;
×
231
    }
232

233
    /// <summary>
234
    /// Sets the value of the Referrer header for an HTTP request.
235
    /// </summary>
236
    /// <param name="uri">The header URI.</param>
237
    /// <returns>A fluent header builder.</returns>
238
    public TBuilder Referrer(Uri uri)
239
    {
240
        RequestMessage.Headers.Referrer = uri;
×
241
        return this as TBuilder;
×
242
    }
243

244
    /// <summary>
245
    /// Sets the value of the Referrer header for an HTTP request.
246
    /// </summary>
247
    /// <param name="value">The header value.</param>
248
    /// <returns>A fluent header builder.</returns>
249
    public TBuilder Referrer(string value)
250
    {
251
        if (string.IsNullOrEmpty(value))
×
252
            return this as TBuilder;
×
253

254
        var uri = new Uri(value);
×
255
        RequestMessage.Headers.Referrer = uri;
×
256

257
        return this as TBuilder;
×
258
    }
259

260
    /// <summary>
261
    /// Sets the value of the User-Agent header for an HTTP request.
262
    /// </summary>
263
    /// <param name="value">The header value.</param>
264
    /// <returns>A fluent header builder.</returns>
265
    public TBuilder UserAgent(string value)
266
    {
267
        if (string.IsNullOrEmpty(value))
×
268
            return this as TBuilder;
×
269

270
        var header = ProductInfoHeaderValue.Parse(value);
×
271
        RequestMessage.Headers.UserAgent.Add(header);
×
272

273
        return this as TBuilder;
×
274
    }
275

276
    /// <summary>
277
    /// Sets the value of the X-HTTP-Method-Override header for an HTTP request.
278
    /// </summary>
279
    /// <param name="method">The HTTP method.</param>
280
    /// <returns>A fluent header builder.</returns>
281
    public TBuilder MethodOverride(HttpMethod method)
282
    {
283
        RequestMessage.Headers.Add(HttpRequestHeaders.MethodOverride, method.ToString());
×
284
        return this as TBuilder;
×
285
    }
286
}
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