Skip to content

Commit c5d8e47

Browse files
Add missing coverage
- Add unit tests for missing coverage. - Simplify some tests.
1 parent 08dbee8 commit c5d8e47

File tree

1 file changed

+216
-70
lines changed

1 file changed

+216
-70
lines changed

tests/HttpClientInterception.Tests/HttpRequestInterceptionBuilderTests.cs

Lines changed: 216 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,108 +2238,254 @@ public static async Task Builder_For_Posted_Json_To_Match_Intercepts_Requests_Th
22382238
public static void Builder_ForAll_Throws_ArgumentNullException_If_Custom_Matching_Delegate_Is_Null()
22392239
{
22402240
// Arrange
2241-
static HttpRequestInterceptionBuilder InterceptionBuilder() =>
2242-
new HttpRequestInterceptionBuilder()
2243-
.Requests()
2244-
.ForHttps()
2245-
.ForHost("api.github.com")
2246-
.ForPath("orgs/justeattakeaway")
2247-
.ForAll((Predicate<HttpRequestMessage>[])null)
2248-
.Responds()
2249-
.WithStatus(HttpStatusCode.OK);
2241+
var builder = new HttpRequestInterceptionBuilder();
2242+
2243+
Predicate<HttpRequestMessage>[] predicates = null;
22502244

2251-
// Act & Assert
2252-
Should.Throw<ArgumentNullException>(InterceptionBuilder).ParamName.ShouldBe("predicates");
2245+
// Act and Assert
2246+
Should.Throw<ArgumentNullException>(() => builder.ForAll(predicates)).ParamName.ShouldBe("predicates");
22532247
}
22542248

22552249
[Fact]
22562250
public static void Builder_ForAll_Throws_InvalidOperationException_If_Custom_Matching_Delegate_Is_Empty()
22572251
{
22582252
// Arrange
2259-
static HttpRequestInterceptionBuilder InterceptionBuilder() =>
2260-
new HttpRequestInterceptionBuilder()
2261-
.Requests()
2262-
.ForHttps()
2263-
.ForHost("api.github.com")
2264-
.ForPath("orgs/justeattakeaway")
2265-
.ForAll(Array.Empty<Predicate<HttpRequestMessage>>())
2266-
.Responds()
2267-
.WithStatus(HttpStatusCode.OK);
2253+
var builder = new HttpRequestInterceptionBuilder();
2254+
2255+
var predicates = Array.Empty<Predicate<HttpRequestMessage>>();
22682256

2269-
// Act & Assert
2270-
Should.Throw<InvalidOperationException>(InterceptionBuilder);
2257+
// Act and Assert
2258+
Should.Throw<InvalidOperationException>(() => builder.ForAll(predicates));
22712259
}
22722260

22732261
[Fact]
22742262
public static void Builder_ForAll_Throws_ArgumentNullException_If_Async_Custom_Matching_Delegate_Is_Null()
22752263
{
22762264
// Arrange
2277-
static HttpRequestInterceptionBuilder InterceptionBuilder() =>
2278-
new HttpRequestInterceptionBuilder()
2279-
.Requests()
2280-
.ForHttps()
2281-
.ForHost("api.github.com")
2282-
.ForPath("orgs/justeattakeaway")
2283-
.ForAll((Func<HttpRequestMessage, Task<bool>>[])null)
2284-
.Responds()
2285-
.WithStatus(HttpStatusCode.OK);
2265+
var builder = new HttpRequestInterceptionBuilder();
22862266

2287-
// Act & Assert
2288-
Should.Throw<ArgumentNullException>(InterceptionBuilder).ParamName.ShouldBe("predicates");
2267+
Func<HttpRequestMessage, Task<bool>>[] predicates = null;
2268+
2269+
// Act and Assert
2270+
Should.Throw<ArgumentNullException>(() => builder.ForAll(predicates)).ParamName.ShouldBe("predicates");
22892271
}
22902272

22912273
[Fact]
22922274
public static void Builder_ForAll_Throws_InvalidOperationException_If_Async_Custom_Matching_Delegate_Is_Empty()
22932275
{
22942276
// Arrange
2295-
static HttpRequestInterceptionBuilder InterceptionBuilder() =>
2296-
new HttpRequestInterceptionBuilder()
2297-
.Requests()
2298-
.ForHttps()
2299-
.ForHost("api.github.com")
2300-
.ForPath("orgs/justeattakeaway")
2301-
.ForAll(Array.Empty<Func<HttpRequestMessage, Task<bool>>>())
2302-
.Responds()
2303-
.WithStatus(HttpStatusCode.OK);
2277+
var builder = new HttpRequestInterceptionBuilder();
2278+
2279+
var predicates = Array.Empty<Func<HttpRequestMessage, Task<bool>>>();
23042280

2305-
// Act & Assert
2306-
Should.Throw<InvalidOperationException>(InterceptionBuilder);
2281+
// Act and Assert
2282+
Should.Throw<InvalidOperationException>(() => builder.ForAll(predicates));
23072283
}
23082284

23092285
[Fact]
23102286
public static void Builder_ForAll_Throws_InvalidOperationException_If_At_Least_One_Async_Custom_Matching_Delegate_Is_Null()
23112287
{
23122288
// Arrange
2313-
static HttpRequestInterceptionBuilder InterceptionBuilder() =>
2314-
new HttpRequestInterceptionBuilder()
2315-
.Requests()
2316-
.ForHttps()
2317-
.ForHost("api.github.com")
2318-
.ForPath("orgs/justeattakeaway")
2319-
.ForAll(new Func<HttpRequestMessage, Task<bool>>[] { _ => Task.FromResult(true), null })
2320-
.Responds()
2321-
.WithStatus(HttpStatusCode.OK);
2289+
var builder = new HttpRequestInterceptionBuilder();
2290+
2291+
Func<HttpRequestMessage, Task<bool>>[] predicates =
2292+
[
2293+
_ => Task.FromResult(true),
2294+
null,
2295+
];
23222296

2323-
// Act & Assert
2324-
Should.Throw<InvalidOperationException>(InterceptionBuilder);
2297+
// Act and Assert
2298+
Should.Throw<InvalidOperationException>(() => builder.ForAll(predicates));
23252299
}
23262300

23272301
[Fact]
23282302
public static void Builder_ForAll_Throws_InvalidOperationException_If_At_Least_One_Custom_Matching_Delegate_Is_Null()
23292303
{
23302304
// Arrange
2331-
static HttpRequestInterceptionBuilder InterceptionBuilder() =>
2332-
new HttpRequestInterceptionBuilder()
2333-
.Requests()
2334-
.ForHttps()
2335-
.ForHost("api.github.com")
2336-
.ForPath("orgs/justeattakeaway")
2337-
.ForAll(new Predicate<HttpRequestMessage>[] { _ => true, null })
2338-
.Responds()
2339-
.WithStatus(HttpStatusCode.OK);
2305+
var builder = new HttpRequestInterceptionBuilder();
2306+
2307+
Predicate<HttpRequestMessage>[] predicates =
2308+
[
2309+
_ => true,
2310+
null,
2311+
];
2312+
2313+
// Act and Assert
2314+
Should.Throw<InvalidOperationException>(() => builder.ForAll(predicates));
2315+
}
2316+
2317+
[Fact]
2318+
public static async Task Builder_ForAll_Matches_Request_With_One_Predicate()
2319+
{
2320+
// Arrange
2321+
string expected = Guid.NewGuid().ToString();
2322+
Predicate<HttpRequestMessage>[] predicates =
2323+
[
2324+
_ => true,
2325+
];
2326+
2327+
var builder = new HttpRequestInterceptionBuilder()
2328+
.Requests()
2329+
.ForAll(predicates)
2330+
.Responds()
2331+
.WithContent(expected);
2332+
2333+
var options = new HttpClientInterceptorOptions()
2334+
.ThrowsOnMissingRegistration()
2335+
.Register(builder);
2336+
2337+
using var client = options.CreateHttpClient();
2338+
2339+
// Act
2340+
string actual = await client.GetStringAsync("https://google.com/");
2341+
2342+
// Assert
2343+
actual.ShouldBe(expected);
2344+
}
2345+
2346+
[Fact]
2347+
public static async Task Builder_ForAll_Matches_Request_With_One_Async_Predicate()
2348+
{
2349+
// Arrange
2350+
string expected = Guid.NewGuid().ToString();
2351+
Func<HttpRequestMessage, Task<bool>>[] predicates =
2352+
[
2353+
_ => Task.FromResult(true),
2354+
];
2355+
2356+
var builder = new HttpRequestInterceptionBuilder()
2357+
.Requests()
2358+
.ForAll(predicates)
2359+
.Responds()
2360+
.WithContent(expected);
2361+
2362+
var options = new HttpClientInterceptorOptions()
2363+
.ThrowsOnMissingRegistration()
2364+
.Register(builder);
2365+
2366+
using var client = options.CreateHttpClient();
2367+
2368+
// Act
2369+
string actual = await client.GetStringAsync("https://google.com/");
2370+
2371+
// Assert
2372+
actual.ShouldBe(expected);
2373+
}
2374+
2375+
[Fact]
2376+
public static async Task Builder_ForAll_Matches_Request_With_Multiple_Predicates()
2377+
{
2378+
// Arrange
2379+
string expected = Guid.NewGuid().ToString();
2380+
Predicate<HttpRequestMessage>[] predicates =
2381+
[
2382+
_ => true,
2383+
_ => true,
2384+
];
2385+
2386+
var builder = new HttpRequestInterceptionBuilder()
2387+
.Requests()
2388+
.ForAll(predicates)
2389+
.Responds()
2390+
.WithContent(expected);
2391+
2392+
var options = new HttpClientInterceptorOptions()
2393+
.ThrowsOnMissingRegistration()
2394+
.Register(builder);
2395+
2396+
using var client = options.CreateHttpClient();
2397+
2398+
// Act
2399+
string actual = await client.GetStringAsync("https://google.com/");
2400+
2401+
// Assert
2402+
actual.ShouldBe(expected);
2403+
}
2404+
2405+
[Fact]
2406+
public static async Task Builder_ForAll_Matches_Request_With_Multiple_Async_Predicates()
2407+
{
2408+
// Arrange
2409+
string expected = Guid.NewGuid().ToString();
2410+
Func<HttpRequestMessage, Task<bool>>[] predicates =
2411+
[
2412+
_ => Task.FromResult(true),
2413+
_ => Task.FromResult(true),
2414+
];
2415+
2416+
var builder = new HttpRequestInterceptionBuilder()
2417+
.Requests()
2418+
.ForAll(predicates)
2419+
.Responds()
2420+
.WithContent(expected);
2421+
2422+
var options = new HttpClientInterceptorOptions()
2423+
.ThrowsOnMissingRegistration()
2424+
.Register(builder);
2425+
2426+
using var client = options.CreateHttpClient();
2427+
2428+
// Act
2429+
string actual = await client.GetStringAsync("https://google.com/");
2430+
2431+
// Assert
2432+
actual.ShouldBe(expected);
2433+
}
23402434

2341-
// Act & Assert
2342-
Should.Throw<InvalidOperationException>(InterceptionBuilder);
2435+
[Fact]
2436+
public static async Task Builder_ForAll_Does_Not_Match_Request_With_Multiple_Predicates_If_Any_False()
2437+
{
2438+
// Arrange
2439+
string expected = Guid.NewGuid().ToString();
2440+
Predicate<HttpRequestMessage>[] predicates =
2441+
[
2442+
_ => true,
2443+
_ => false,
2444+
_ => true,
2445+
];
2446+
2447+
var builder = new HttpRequestInterceptionBuilder()
2448+
.Requests()
2449+
.ForAll(predicates)
2450+
.Responds()
2451+
.WithContent(expected);
2452+
2453+
var options = new HttpClientInterceptorOptions()
2454+
.ThrowsOnMissingRegistration()
2455+
.Register(builder);
2456+
2457+
using var client = options.CreateHttpClient();
2458+
2459+
// Act and Assert
2460+
await Should.ThrowAsync<HttpRequestNotInterceptedException>(() => client.GetStringAsync("https://google.com/"));
2461+
}
2462+
2463+
[Fact]
2464+
public static async Task Builder_ForAll_Matches_Request_With_Multiple_Async_Predicates_If_Any_False()
2465+
{
2466+
// Arrange
2467+
string expected = Guid.NewGuid().ToString();
2468+
Func<HttpRequestMessage, Task<bool>>[] predicates =
2469+
[
2470+
_ => Task.FromResult(true),
2471+
_ => Task.FromResult(false),
2472+
_ => Task.FromResult(true),
2473+
];
2474+
2475+
var builder = new HttpRequestInterceptionBuilder()
2476+
.Requests()
2477+
.ForAll(predicates)
2478+
.Responds()
2479+
.WithContent(expected);
2480+
2481+
var options = new HttpClientInterceptorOptions()
2482+
.ThrowsOnMissingRegistration()
2483+
.Register(builder);
2484+
2485+
using var client = options.CreateHttpClient();
2486+
2487+
// Act and Assert
2488+
await Should.ThrowAsync<HttpRequestNotInterceptedException>(() => client.GetStringAsync("https://google.com/"));
23432489
}
23442490

23452491
[Fact]
@@ -2422,19 +2568,19 @@ public static async Task Can_Do_Custom_Matching_Even_Number_Requests_Return_200_
24222568

24232569
using var client = options.CreateHttpClient();
24242570

2425-
// Act & Assert (First request)
2571+
// Act and Assert (First request)
24262572
HttpResponseMessage firstResponse = await client.GetAsync(requestUri);
24272573
firstResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
24282574

2429-
// Act & Assert (Second request)
2575+
// Act and Assert (Second request)
24302576
HttpResponseMessage secondResponse = await client.GetAsync(requestUri);
24312577
secondResponse.StatusCode.ShouldBe(HttpStatusCode.TooManyRequests);
24322578

2433-
// Act & Assert (Third request)
2579+
// Act and Assert (Third request)
24342580
HttpResponseMessage thirdResponse = await client.GetAsync(requestUri);
24352581
thirdResponse.StatusCode.ShouldBe(HttpStatusCode.OK);
24362582

2437-
// Act & Assert (Fourth request)
2583+
// Act and Assert (Fourth request)
24382584
HttpResponseMessage fourthResponse = await client.GetAsync(requestUri);
24392585
fourthResponse.StatusCode.ShouldBe(HttpStatusCode.TooManyRequests);
24402586
}

0 commit comments

Comments
 (0)