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

loresoft / FluentRest / 9388947971

05 Jun 2024 05:56PM UTC coverage: 57.76% (+0.2%) from 57.587%
9388947971

push

github

pwelter34
fix flaky tests

277 of 614 branches covered (45.11%)

Branch coverage included in aggregate %.

847 of 1332 relevant lines covered (63.59%)

73.6 hits per line

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

39.74
/src/FluentRest/QueryBuilder.cs
1
namespace FluentRest;
2

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

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

32
    /// <summary>
33
    /// Start a fluent header builder.
34
    /// </summary>
35
    /// <param name="builder">The builder factory.</param>
36
    /// <returns>A fluent request builder.</returns>
37
    /// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception>
38
    public TBuilder Header(Action<HeaderBuilder> builder)
39
    {
40
        if (builder == null)
24!
41
            throw new ArgumentNullException(nameof(builder));
×
42

43
        var headerBuilder = new HeaderBuilder(RequestMessage);
24✔
44
        builder(headerBuilder);
24✔
45

46
        return this as TBuilder;
24✔
47
    }
48

49
    /// <summary>
50
    /// Sets HTTP header with the specified <paramref name="name"/> and <paramref name="value"/>.
51
    /// </summary>
52
    /// <param name="name">The header name.</param>
53
    /// <param name="value">The header value.</param>
54
    /// <returns>A fluent request builder.</returns>
55
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
56
    public TBuilder Header(string name, string value)
57
    {
58
        if (name == null)
15!
59
            throw new ArgumentNullException(nameof(name));
×
60

61
        if (value == null)
15✔
62
            RequestMessage.Headers.Remove(name);
3✔
63
        else
64
            RequestMessage.Headers.Add(name, value);
12✔
65

66
        return this as TBuilder;
15✔
67
    }
68

69
    /// <summary>
70
    /// Sets HTTP header with the specified <paramref name="name"/> and <paramref name="value"/> if the specified <paramref name="condition"/> is true.
71
    /// </summary>
72
    /// <param name="condition">If condition is true, header will be added; otherwise ignore header.</param>
73
    /// <param name="name">The header name.</param>
74
    /// <param name="value">The header value.</param>
75
    /// <returns>A fluent request builder.</returns>
76
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
77
    public TBuilder HeaderIf(Func<bool> condition, string name, string value)
78
    {
79
        if (condition == null || !condition())
×
80
            return this as TBuilder;
×
81

82
        return Header(name, value);
×
83
    }
84

85

86
    /// <summary>
87
    /// Sets the base URI address used when sending requests.
88
    /// </summary>
89
    /// <param name="path">The path.</param>
90
    /// <returns>A fluent request builder.</returns>
91
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
92
    public TBuilder BaseUri(Uri path)
93
    {
94
        if (path == null)
27!
95
            throw new ArgumentNullException(nameof(path));
×
96

97
        var urlBuilder = new UrlBuilder(path);
27✔
98
        RequestMessage.SetUrlBuilder(urlBuilder);
27✔
99

100
        RequestMessage.Synchronize();
27✔
101

102

103
        return this as TBuilder;
27✔
104
    }
105

106
    /// <summary>
107
    /// Sets the base URI address used when sending requests.
108
    /// </summary>
109
    /// <param name="path">The path.</param>
110
    /// <returns>A fluent request builder.</returns>
111
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
112
    public TBuilder BaseUri(string path)
113
    {
114
        if (path == null)
24!
115
            throw new ArgumentNullException(nameof(path));
×
116

117
        var uri = new Uri(path, UriKind.Absolute);
24✔
118
        return BaseUri(uri);
24✔
119
    }
120

121

122
    /// <summary>
123
    /// Sets the base URI from the specified <paramref name="path"/>.
124
    /// </summary>
125
    /// <param name="path">The full Uri path.</param>
126
    /// <returns>A fluent request builder.</returns>
127
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
128
    public TBuilder FullUri(Uri path)
129
    {
130
        if (path == null)
×
131
            throw new ArgumentNullException(nameof(path));
×
132

133
        return BaseUri(path);
×
134
    }
135

136
    /// <summary>
137
    /// Sets the base URI from the specified <paramref name="path"/>.
138
    /// </summary>
139
    /// <param name="path">The full Uri path.</param>
140
    /// <returns>A fluent request builder.</returns>
141
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
142
    public TBuilder FullUri(string path)
143
    {
144
        if (path == null)
3!
145
            throw new ArgumentNullException(nameof(path));
×
146

147
        var u = new Uri(path, UriKind.Absolute);
3✔
148
        return BaseUri(u);
3✔
149
    }
150

151

152
    /// <summary>
153
    /// Appends the specified <paramref name="path"/> to the BaseUri of the request.
154
    /// </summary>
155
    /// <param name="path">The path to append.</param>
156
    /// <returns>A fluent request builder.</returns>
157
    public TBuilder AppendPath(Uri path)
158
    {
159
        if (path == null)
×
160
            return this as TBuilder;
×
161

162
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
163
        urlBuilder.AppendPath(path);
×
164

165
        RequestMessage.Synchronize();
×
166

167
        return this as TBuilder;
×
168
    }
169

170
    /// <summary>
171
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
172
    /// </summary>
173
    /// <param name="path">The path to append.</param>
174
    /// <returns>
175
    /// A fluent request builder.
176
    /// </returns>
177
    public TBuilder AppendPath(string path)
178
    {
179
        if (path == null)
228!
180
            return this as TBuilder;
×
181

182
        var urlBuilder = RequestMessage.GetUrlBuilder();
228✔
183
        urlBuilder.AppendPath(path);
228✔
184

185
        RequestMessage.Synchronize();
228✔
186

187
        return this as TBuilder;
228✔
188
    }
189

190
    /// <summary>
191
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
192
    /// </summary>
193
    /// <typeparam name="TValue">The type of the value.</typeparam>
194
    /// <param name="path">The path to append.</param>
195
    /// <returns>A fluent request builder.</returns>
196
    public TBuilder AppendPath<TValue>(TValue path)
197
    {
198
        if (path == null)
×
199
            return this as TBuilder;
×
200

201
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
202
        urlBuilder.AppendPath(path);
×
203

204
        RequestMessage.Synchronize();
×
205

206
        return this as TBuilder;
×
207
    }
208

209
    /// <summary>
210
    /// Appends the specified <paramref name="paths"/> to the BaseUri of the request.
211
    /// </summary>
212
    /// <param name="paths">The paths to append.</param>
213
    /// <returns>A fluent request builder.</returns>
214
    public TBuilder AppendPath(IEnumerable<string> paths)
215
    {
216
        if (paths == null)
3!
217
            return this as TBuilder;
×
218

219
        var urlBuilder = RequestMessage.GetUrlBuilder();
3✔
220
        urlBuilder.AppendPath(paths);
3✔
221

222
        RequestMessage.Synchronize();
3✔
223

224
        return this as TBuilder;
3✔
225
    }
226

227
    /// <summary>
228
    /// Appends the specified <paramref name="paths"/> to the BaseUri of the request.
229
    /// </summary>
230
    /// <param name="paths">The paths to append.</param>
231
    /// <returns>A fluent request builder.</returns>
232
    public TBuilder AppendPath(params string[] paths)
233
    {
234
        if (paths == null)
3!
235
            return this as TBuilder;
×
236

237
        var urlBuilder = RequestMessage.GetUrlBuilder();
3✔
238
        urlBuilder.AppendPaths(paths);
3✔
239

240
        RequestMessage.Synchronize();
3✔
241

242
        return this as TBuilder;
3✔
243
    }
244

245
    /// <summary>
246
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
247
    /// </summary>
248
    /// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
249
    /// <param name="path">The path to append.</param>
250
    /// <returns>
251
    /// A fluent request builder.
252
    /// </returns>
253
    public TBuilder AppendPathIf(Func<bool> condition, string path)
254
    {
255
        if (path == null)
×
256
            return this as TBuilder;
×
257

258
        if (condition == null || !condition())
×
259
            return this as TBuilder;
×
260

261
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
262
        urlBuilder.AppendPath(path);
×
263

264
        RequestMessage.Synchronize();
×
265

266
        return this as TBuilder;
×
267
    }
268

269
    /// <summary>
270
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
271
    /// </summary>
272
    /// <typeparam name="TValue">The type of the value.</typeparam>
273
    /// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
274
    /// <param name="path">The path to append.</param>
275
    /// <returns>A fluent request builder.</returns>
276
    public TBuilder AppendPathIf<TValue>(Func<bool> condition, TValue path)
277
    {
278
        if (path == null)
×
279
            return this as TBuilder;
×
280

281
        if (condition == null || !condition())
×
282
            return this as TBuilder;
×
283

284
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
285
        urlBuilder.AppendPath(path);
×
286

287
        RequestMessage.Synchronize();
×
288

289
        return this as TBuilder;
×
290
    }
291

292

293
    /// <summary>
294
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri.
295
    /// </summary>
296
    /// <param name="name">The query parameter name.</param>
297
    /// <param name="value">The query parameter value.</param>
298
    /// <returns>A fluent request builder.</returns>
299
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
300
    public TBuilder QueryString(string name, string value)
301
    {
302
        if (name == null)
117!
303
            throw new ArgumentNullException(nameof(name));
×
304

305

306
        var urlBuilder = RequestMessage.GetUrlBuilder();
117✔
307
        urlBuilder.AppendQuery(name, value);
117✔
308

309
        RequestMessage.Synchronize();
117✔
310

311
        return this as TBuilder;
117✔
312

313
    }
314

315
    /// <summary>
316
    /// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri.
317
    /// </summary>
318
    /// <typeparam name="TValue">The type of the value.</typeparam>
319
    /// <param name="name">The query parameter name.</param>
320
    /// <param name="value">The query parameter value.</param>
321
    /// <returns>
322
    /// A fluent request builder.
323
    /// </returns>
324
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
325
    public TBuilder QueryString<TValue>(string name, TValue value)
326
    {
327
        if (name == null)
108!
328
            throw new ArgumentNullException(nameof(name));
×
329

330
        var v = value != null ? value.ToString() : string.Empty;
108!
331
        return QueryString(name, v);
108✔
332
    }
333

334
    /// <summary>
335
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri if the specified <paramref name="condition"/> is true.
336
    /// </summary>
337
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
338
    /// <param name="name">The query parameter name.</param>
339
    /// <param name="value">The query parameter value.</param>
340
    /// <returns>A fluent request builder.</returns>
341
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
342
    public TBuilder QueryStringIf(Func<bool> condition, string name, string value)
343
    {
344
        if (condition == null || !condition())
×
345
            return this as TBuilder;
×
346

347
        return QueryString(name, value);
×
348
    }
349

350
    /// <summary>
351
    /// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri if the specified <paramref name="condition" /> is true.
352
    /// </summary>
353
    /// <typeparam name="TValue">The type of the value.</typeparam>
354
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
355
    /// <param name="name">The query parameter name.</param>
356
    /// <param name="value">The query parameter value.</param>
357
    /// <returns>
358
    /// A fluent request builder.
359
    /// </returns>
360
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
361
    public TBuilder QueryStringIf<TValue>(Func<bool> condition, string name, TValue value)
362
    {
363
        if (condition == null || !condition())
×
364
            return this as TBuilder;
×
365

366
        return QueryString(name, value);
×
367
    }
368
}
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