Skip to content

Commit ab2304a

Browse files
committed
Added HasMatchingUri extension method to test the request uri
1 parent 39a0abb commit ab2304a

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

src/HttpClientTestHelpers/HttpRequestMessageExtensions.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net.Http;
3+
using System.Text.RegularExpressions;
34

45
namespace HttpClientTestHelpers
56
{
@@ -105,12 +106,45 @@ public static bool HasHeader(this HttpRequestMessage httpRequestMessage, string
105106
throw new ArgumentNullException(nameof(httpRequestMessage));
106107
}
107108

108-
if(string.IsNullOrEmpty(headerName))
109+
if (string.IsNullOrEmpty(headerName))
109110
{
110111
throw new ArgumentNullException(nameof(headerName));
111112
}
112113

113114
return httpRequestMessage.Headers.Contains(headerName);
114115
}
116+
117+
/// <summary>
118+
/// Determines whether the request uri matches a pattern.
119+
/// </summary>
120+
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
121+
/// <param name="pattern">A pattern to match with the request uri, support * as wildcards.</param>
122+
/// <returns>true when the request uri matches the pattern; otherwise, false.</returns>
123+
public static bool HasMatchingUri(this HttpRequestMessage httpRequestMessage, string pattern)
124+
{
125+
if (httpRequestMessage == null)
126+
{
127+
throw new ArgumentNullException(nameof(httpRequestMessage));
128+
}
129+
130+
if (pattern == null)
131+
{
132+
throw new ArgumentNullException(nameof(pattern));
133+
}
134+
135+
if (pattern == "*")
136+
{
137+
return true;
138+
}
139+
140+
if (string.IsNullOrEmpty(pattern))
141+
{
142+
return false;
143+
}
144+
145+
var regex = Regex.Escape(pattern).Replace("\\*", "(.*)");
146+
147+
return Regex.IsMatch(httpRequestMessage.RequestUri.AbsoluteUri, regex);
148+
}
115149
}
116150
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Net.Http;
3+
4+
using Xunit;
5+
6+
namespace HttpClientTestHelpers.Tests
7+
{
8+
public partial class HttpRequestMessageExtensionsTests
9+
{
10+
#nullable disable
11+
[Fact]
12+
public void HasMatchingUri_NullHttpRequestMessage_ThrowsArgumentNullException()
13+
{
14+
HttpRequestMessage sut = null;
15+
var exception = Assert.Throws<ArgumentNullException>(() => sut.HasMatchingUri("*"));
16+
Assert.Equal("httpRequestMessage", exception.ParamName);
17+
}
18+
19+
[Fact]
20+
public void HasMatchingUri_NullPattern_ThrowsArgumentNullException()
21+
{
22+
using var sut = new HttpRequestMessage { RequestUri = new Uri("https://example.com") };
23+
var exception = Assert.Throws<ArgumentNullException>(() => sut.HasMatchingUri(null));
24+
Assert.Equal("pattern", exception.ParamName);
25+
}
26+
#nullable restore
27+
28+
[Fact]
29+
public void HasMatchingUri_EmptyPattern_ReturnsFalse()
30+
{
31+
using var sut = new HttpRequestMessage { RequestUri = new Uri("https://example.com") };
32+
33+
Assert.False(sut.HasMatchingUri(""));
34+
}
35+
36+
[Theory]
37+
[InlineData("*")]
38+
[InlineData("https://example.com")]
39+
[InlineData("https://example.com/")]
40+
[InlineData("https://example.com/*")]
41+
[InlineData("https://*/")]
42+
[InlineData("https://*/*")]
43+
public void HasMatchingUri_WithPatternFor_ReturnsTrue(string pattern)
44+
{
45+
using var sut = new HttpRequestMessage { RequestUri = new Uri("https://example.com") };
46+
47+
Assert.True(sut.HasMatchingUri(pattern));
48+
}
49+
50+
[Fact]
51+
public void HasMatchingUri_PatternWithMultipleWildCards_ReturnsTrue()
52+
{
53+
using var sut = new HttpRequestMessage { RequestUri = new Uri("https://example.com/api/test/value") };
54+
55+
Assert.True(sut.HasMatchingUri("https://*/api/*"));
56+
}
57+
58+
[Theory]
59+
[InlineData("")]
60+
[InlineData("http://example.com")]
61+
[InlineData("http://*")]
62+
[InlineData("https://*/api/test")]
63+
public void HasMatchingUri_WithPatternThatDoesnotMatch_ReturnsFalse(string pattern)
64+
{
65+
using var sut = new HttpRequestMessage { RequestUri = new Uri("https://example.com") };
66+
67+
Assert.False(sut.HasMatchingUri(pattern));
68+
}
69+
70+
[Fact]
71+
public void HasMatchingUri_WithValidPatternIncludingQueryParameters_ReturnsTrue()
72+
{
73+
using var sut = new HttpRequestMessage { RequestUri = new Uri("https://example.com/list?id=test") };
74+
75+
Assert.True(sut.HasMatchingUri("*/list?id=test"));
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)