Skip to content

Commit

Permalink
Use collection expressions
Browse files Browse the repository at this point in the history
Apply code fixer to use collection expressions where relevant.
  • Loading branch information
martincostello committed Jan 6, 2025
1 parent 5fa99b7 commit c0c97b8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 55 deletions.
12 changes: 6 additions & 6 deletions src/HttpClientInterception/HttpRequestInterceptionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public HttpRequestInterceptionBuilder WithContentStream(Func<Task<Stream>> conte
/// <paramref name="name"/> is <see langword="null"/>.
/// </exception>
public HttpRequestInterceptionBuilder WithContentHeader(string name, string value)
=> WithContentHeader(name, new[] { value });
=> WithContentHeader(name, [value]);

/// <summary>
/// Sets a custom HTTP content header to use with multiple values.
Expand Down Expand Up @@ -530,7 +530,7 @@ public HttpRequestInterceptionBuilder WithContentHeaders(IDictionary<string, str

foreach (var pair in headers)
{
copy[pair.Key] = new[] { pair.Value };
copy[pair.Key] = [pair.Value];
}

_contentHeaders = copy;
Expand Down Expand Up @@ -574,7 +574,7 @@ public HttpRequestInterceptionBuilder WithContentHeaders(
/// <paramref name="name"/> is <see langword="null"/>.
/// </exception>
public HttpRequestInterceptionBuilder WithResponseHeader(string name, string value)
=> WithResponseHeader(name, new[] { value });
=> WithResponseHeader(name, [value]);

/// <summary>
/// Sets a custom HTTP response header to use with multiple values.
Expand Down Expand Up @@ -653,7 +653,7 @@ public HttpRequestInterceptionBuilder WithResponseHeaders(IDictionary<string, st

foreach (var pair in headers)
{
copy[pair.Key] = new[] { pair.Value };
copy[pair.Key] = [pair.Value];
}

_responseHeaders = copy;
Expand Down Expand Up @@ -906,7 +906,7 @@ public HttpRequestInterceptionBuilder HavingPriority(int? priority)
/// HTTP request headers are only tested for interception if the URI requested was registered for interception.
/// </remarks>
public HttpRequestInterceptionBuilder ForRequestHeader(string name, string value)
=> ForRequestHeader(name, new[] { value });
=> ForRequestHeader(name, [value]);

/// <summary>
/// Sets an HTTP request header to intercept with multiple values.
Expand Down Expand Up @@ -994,7 +994,7 @@ public HttpRequestInterceptionBuilder ForRequestHeaders(IDictionary<string, stri

foreach (var pair in headers)
{
copy[pair.Key] = new[] { pair.Value };
copy[pair.Key] = [pair.Value];
}

_requestHeaders = copy;
Expand Down
24 changes: 12 additions & 12 deletions tests/HttpClientInterception.Tests/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static async Task Intercept_Http_Get_For_Raw_Bytes()
.ForHost("files.domain.com")
.ForPath("setup.exe")
.WithMediaType("application/octet-stream")
.WithContent(() => new byte[] { 0, 1, 2, 3, 4 });
.WithContent(() => [0, 1, 2, 3, 4]);

var options = new HttpClientInterceptorOptions()
.Register(builder);
Expand Down Expand Up @@ -276,7 +276,7 @@ public static async Task Intercept_Custom_Http_Method()
.ForMethod(new HttpMethod("custom"))
.ForHost("custom.domain.com")
.ForQuery("length=2")
.WithContent(() => new byte[] { 0, 1 });
.WithContent(() => [0, 1]);

var options = new HttpClientInterceptorOptions()
.Register(builder);
Expand All @@ -300,7 +300,7 @@ public static void Intercept_Custom_Synchronous_Http_Method()
.ForMethod(new HttpMethod("custom"))
.ForHost("custom.domain.com")
.ForQuery("length=2")
.WithContent(() => new byte[] { 0, 1 });
.WithContent(() => [0, 1]);

var options = new HttpClientInterceptorOptions()
.Register(builder);
Expand Down Expand Up @@ -746,7 +746,7 @@ static bool IsHttpGetForJustEatGitHubOrg(HttpRequestMessage request)

// Register an HTTP 429 error for the first three requests.
var builder1 = new HttpRequestInterceptionBuilder()
.ForAll(new Predicate<HttpRequestMessage>[] { IsHttpGetForJustEatGitHubOrg, _ => requestCount < 2 })
.ForAll([IsHttpGetForJustEatGitHubOrg, _ => requestCount < 2])
.WithInterceptionCallback(IncrementRequestCount)
.Responds()
.WithStatus(HttpStatusCode.TooManyRequests)
Expand All @@ -755,7 +755,7 @@ static bool IsHttpGetForJustEatGitHubOrg(HttpRequestMessage request)

// Register another request for an HTTP 200 for all subsequent requests.
var builder2 = new HttpRequestInterceptionBuilder()
.ForAll(new Predicate<HttpRequestMessage>[] { IsHttpGetForJustEatGitHubOrg, _ => requestCount >= 2 })
.ForAll([IsHttpGetForJustEatGitHubOrg, _ => requestCount >= 2])
.WithInterceptionCallback(IncrementRequestCount)
.Responds()
.WithStatus(HttpStatusCode.OK)
Expand Down Expand Up @@ -804,21 +804,21 @@ public static async Task Dynamically_Compute_Http_Headers()
{
return new Dictionary<string, ICollection<string>>()
{
["x-sequence"] = new[] { (++requestHeadersCounter).ToString(CultureInfo.InvariantCulture) },
["x-sequence"] = [(++requestHeadersCounter).ToString(CultureInfo.InvariantCulture)],
};
})
.WithContentHeaders(() =>
{
return new Dictionary<string, ICollection<string>>()
{
["content-type"] = new[] { "application/json; v=" + (++contentHeadersCounter).ToString(CultureInfo.InvariantCulture) },
["content-type"] = ["application/json; v=" + (++contentHeadersCounter).ToString(CultureInfo.InvariantCulture)],
};
})
.WithResponseHeaders(() =>
{
return new Dictionary<string, ICollection<string>>()
{
["x-count"] = new[] { (++responseHeadersCounter).ToString(CultureInfo.InvariantCulture) },
["x-count"] = [(++responseHeadersCounter).ToString(CultureInfo.InvariantCulture)],
};
});

Expand All @@ -839,11 +839,11 @@ public static async Task Dynamically_Compute_Http_Headers()
// Assert
response1.Headers.TryGetValues("x-count", out var values).ShouldBeTrue();
values.ShouldNotBeNull();
values.ShouldBe(new[] { "1" });
values.ShouldBe(["1"]);

response1.Content.Headers.TryGetValues("content-type", out values).ShouldBeTrue();
values.ShouldNotBeNull();
values.ShouldBe(new[] { "application/json; v=1" });
values.ShouldBe(["application/json; v=1"]);

// Act
using var request2 = new HttpRequestMessage(method, requestUri);
Expand All @@ -853,11 +853,11 @@ public static async Task Dynamically_Compute_Http_Headers()
// Assert
response2.Headers.TryGetValues("x-count", out values).ShouldBeTrue();
values.ShouldNotBeNull();
values.ShouldBe(new[] { "2" });
values.ShouldBe(["2"]);

response2.Content.Headers.TryGetValues("content-type", out values).ShouldBeTrue();
values.ShouldNotBeNull();
values.ShouldBe(new[] { "application/json; v=2" });
values.ShouldBe(["application/json; v=2"]);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public static async Task Register_For_Collection_Registers_Interceptions()
.WithContent("bar");

var options = new HttpClientInterceptorOptions()
.Register(new[] { builder1, builder2 });
.Register([builder1, builder2]);

string actual1;
string actual2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo
actual.RequestMessage.ShouldBe(request);
actual.Content.ShouldNotBeNull();
actual.Content.Headers.ContentLength.ShouldBe(0);
actual.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Headers.GetValues("c").ShouldBe(new[] { "d" });
actual.Headers.GetValues("a").ShouldBe(["b"]);
actual.Headers.GetValues("c").ShouldBe(["d"]);
}

[Fact]
Expand Down Expand Up @@ -283,8 +283,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo

var responseHeaders = new Dictionary<string, IEnumerable<string>>()
{
["a"] = new[] { "b" },
["c"] = new[] { "d", "e" },
["a"] = ["b"],
["c"] = ["d", "e"],
};

var options = new HttpClientInterceptorOptions()
Expand All @@ -300,8 +300,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo
actual.RequestMessage.ShouldBe(request);
actual.Content.ShouldNotBeNull();
actual.Content.Headers.ContentLength.ShouldBe(0);
actual.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Headers.GetValues("c").ShouldBe(new[] { "d", "e" });
actual.Headers.GetValues("a").ShouldBe(["b"]);
actual.Headers.GetValues("c").ShouldBe(["d", "e"]);
}

[Fact]
Expand Down Expand Up @@ -330,8 +330,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo
actual.RequestMessage.ShouldBe(request);
actual.Content.ShouldNotBeNull();
actual.Content.Headers.ContentLength.ShouldBe(0);
actual.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Headers.GetValues("c").ShouldBe(new[] { "d" });
actual.Headers.GetValues("a").ShouldBe(["b"]);
actual.Headers.GetValues("c").ShouldBe(["d"]);
}

[Fact]
Expand All @@ -343,8 +343,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo

var responseHeaders = new Dictionary<string, IEnumerable<string>>()
{
["a"] = new[] { "b" },
["c"] = new[] { "d", "e" },
["a"] = ["b"],
["c"] = ["d", "e"],
};

var options = new HttpClientInterceptorOptions()
Expand All @@ -360,8 +360,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo
actual.RequestMessage.ShouldBe(request);
actual.Content.ShouldNotBeNull();
actual.Content.Headers.ContentLength.ShouldBe(0);
actual.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Headers.GetValues("c").ShouldBe(new[] { "d", "e" });
actual.Headers.GetValues("a").ShouldBe(["b"]);
actual.Headers.GetValues("c").ShouldBe(["d", "e"]);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo

// Assert
actual.ShouldNotBeNull();
actual.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Headers.GetValues("c").ShouldBe(new[] { "d" });
actual.Headers.GetValues("a").ShouldBe(["b"]);
actual.Headers.GetValues("c").ShouldBe(["d"]);
}

[Fact]
Expand All @@ -401,8 +401,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo

var headers = new Dictionary<string, ICollection<string>>()
{
["a"] = new[] { "b" },
["c"] = new[] { "d", "e" },
["a"] = ["b"],
["c"] = ["d", "e"],
};

var builder = new HttpRequestInterceptionBuilder()
Expand All @@ -419,8 +419,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Respo

// Assert
actual.ShouldNotBeNull();
actual.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Headers.GetValues("c").ShouldBe(new[] { "d", "e" });
actual.Headers.GetValues("a").ShouldBe(["b"]);
actual.Headers.GetValues("c").ShouldBe(["d", "e"]);
}

[Fact]
Expand All @@ -445,8 +445,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Individual_C

// Assert
actual.ShouldNotBeNull();
actual.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Headers.GetValues("c").ShouldBe(new[] { "d", "e" });
actual.Headers.GetValues("a").ShouldBe(["b"]);
actual.Headers.GetValues("c").ShouldBe(["d", "e"]);
}

[Fact]
Expand Down Expand Up @@ -475,8 +475,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Conte

// Assert
actual.ShouldNotBeNull();
actual.Content.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Content.Headers.GetValues("c").ShouldBe(new[] { "d" });
actual.Content.Headers.GetValues("a").ShouldBe(["b"]);
actual.Content.Headers.GetValues("c").ShouldBe(["d"]);
}

[Fact]
Expand All @@ -487,8 +487,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Conte

var headers = new Dictionary<string, ICollection<string>>()
{
["a"] = new[] { "b" },
["c"] = new[] { "d", "e" },
["a"] = ["b"],
["c"] = ["d", "e"],
};

var builder = new HttpRequestInterceptionBuilder()
Expand All @@ -505,8 +505,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Custom_Conte

// Assert
actual.ShouldNotBeNull();
actual.Content.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Content.Headers.GetValues("c").ShouldBe(new[] { "d", "e" });
actual.Content.Headers.GetValues("a").ShouldBe(["b"]);
actual.Content.Headers.GetValues("c").ShouldBe(["d", "e"]);
}

[Fact]
Expand All @@ -533,8 +533,8 @@ public static async Task GetResponseAsync_Returns_Empty_Response_If_Individual_C
actual.ShouldNotBeNull();
actual.ReasonPhrase.ShouldBe("OK");
actual.Version.ShouldBe(new Version(1, 1));
actual.Content.Headers.GetValues("a").ShouldBe(new[] { "b" });
actual.Content.Headers.GetValues("c").ShouldBe(new[] { "d", "e" });
actual.Content.Headers.GetValues("a").ShouldBe(["b"]);
actual.Content.Headers.GetValues("c").ShouldBe(["d", "e"]);
}

[Fact]
Expand Down Expand Up @@ -1461,7 +1461,7 @@ public static async Task Builder_With_One_Multivalue_Header_To_Match_Intercepts_

using (var client = options.CreateHttpClient())
{
client.DefaultRequestHeaders.Add("x-forwarded-for", new[] { "192.168.1.1", "192.168.1.2" });
client.DefaultRequestHeaders.Add("x-forwarded-for", ["192.168.1.1", "192.168.1.2"]);

// Act
json = await client.GetStringAsync("https://public.je-apis.com/consumer/order-history", TestContext.Current.CancellationToken);
Expand Down Expand Up @@ -1493,8 +1493,8 @@ public static async Task Builder_With_Many_Multivalue_Headers_To_Match_Intercept

using (var client = options.CreateHttpClient())
{
client.DefaultRequestHeaders.Add("x-forwarded-for", new[] { "192.168.1.1", "192.168.1.2" });
client.DefaultRequestHeaders.Add("x-forwarded-proto", new[] { "http", "https" });
client.DefaultRequestHeaders.Add("x-forwarded-for", ["192.168.1.1", "192.168.1.2"]);
client.DefaultRequestHeaders.Add("x-forwarded-proto", ["http", "https"]);

// Act
json = await client.GetStringAsync("https://public.je-apis.com/consumer/order-history", TestContext.Current.CancellationToken);
Expand All @@ -1511,8 +1511,8 @@ public static async Task Builder_With_Many_Multivalue_Headers_From_Dictionary_To
// Arrange
var headers = new Dictionary<string, ICollection<string>>()
{
["x-forwarded-for"] = new[] { "192.168.1.1", "192.168.1.2" },
["x-forwarded-proto"] = new[] { "http", "https" },
["x-forwarded-for"] = ["192.168.1.1", "192.168.1.2"],
["x-forwarded-proto"] = ["http", "https"],
};

var builder = new HttpRequestInterceptionBuilder()
Expand All @@ -1531,8 +1531,8 @@ public static async Task Builder_With_Many_Multivalue_Headers_From_Dictionary_To

using (var client = options.CreateHttpClient())
{
client.DefaultRequestHeaders.Add("x-forwarded-for", new[] { "192.168.1.1", "192.168.1.2" });
client.DefaultRequestHeaders.Add("x-forwarded-proto", new[] { "http", "https" });
client.DefaultRequestHeaders.Add("x-forwarded-for", ["192.168.1.1", "192.168.1.2"]);
client.DefaultRequestHeaders.Add("x-forwarded-proto", ["http", "https"]);

// Act
json = await client.GetStringAsync("https://public.je-apis.com/consumer/order-history", TestContext.Current.CancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static async Task SendAsync_Calls_Inner_Handler_If_Registration_Missing()

handler.GetType()
.GetMethod("SendAsync", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(handler, new object[] { request, Arg.Any<CancellationToken>() })
.Invoke(handler, [request, Arg.Any<CancellationToken>()])
.Returns(Task.FromResult(expected));

using var httpClient = options.CreateHttpClient(handler);
Expand Down

0 comments on commit c0c97b8

Please sign in to comment.