|
| 1 | +using MockHttp.Responses; |
| 2 | + |
| 3 | +namespace MockHttp.Matchers; |
| 4 | + |
| 5 | +public class RequestUriMatcherTests |
| 6 | +{ |
| 7 | + [Theory] |
| 8 | + [InlineData("", UriKind.Relative, "http://127.0.0.1/", true)] |
| 9 | + [InlineData("relative.htm", UriKind.Relative, "http://127.0.0.1/relative.htm", true)] |
| 10 | + [InlineData("/folder/relative.htm", UriKind.Relative, "http://127.0.0.1/relative.htm", false)] |
| 11 | + [InlineData("relative.htm", UriKind.Relative, "http://127.0.0.1/folder/relative.htm", false)] |
| 12 | + [InlineData("folder/relative.htm", UriKind.Relative, "http://127.0.0.1/folder/relative.htm", true)] |
| 13 | + [InlineData("/folder/relative.htm", UriKind.Relative, "http://127.0.0.1/folder/relative.htm", true)] |
| 14 | + [InlineData("http://127.0.0.1/absolute.htm", UriKind.Absolute, "http://127.0.0.1/absolute.htm", true)] |
| 15 | + [InlineData("http://127.0.0.1/absolute.htm", UriKind.Absolute, "http://127.0.0.1/folder/absolute.htm", false)] |
| 16 | + public void Given_uri_when_matching_should_match(string matchUri, UriKind uriKind, string requestUri, bool isMatch) |
| 17 | + { |
| 18 | + var request = new HttpRequestMessage { RequestUri = new Uri(requestUri, UriKind.Absolute) }; |
| 19 | + var sut = new RequestUriMatcher(new Uri(matchUri, uriKind)); |
| 20 | + |
| 21 | + // Act & assert |
| 22 | + sut.IsMatch(new MockHttpRequestContext(request)).Should().Be(isMatch); |
| 23 | + } |
| 24 | + |
| 25 | + [Theory] |
| 26 | + [InlineData("relative.htm", true, "http://127.0.0.1/relative.htm", true)] |
| 27 | + [InlineData("/folder/relative.htm", true, "http://127.0.0.1/relative.htm", false)] |
| 28 | + [InlineData("relative.htm", true, "http://127.0.0.1/folder/relative.htm", false)] |
| 29 | + [InlineData("folder/relative.htm", true, "http://127.0.0.1/folder/relative.htm", true)] |
| 30 | + [InlineData("/folder/relative.htm", true, "http://127.0.0.1/folder/relative.htm", true)] |
| 31 | + [InlineData("http://127.0.0.1/absolute.htm", true, "http://127.0.0.1/absolute.htm", true)] |
| 32 | + [InlineData("http://127.0.0.1/absolute.htm", true, "http://127.0.0.1/folder/absolute.htm", false)] |
| 33 | + [InlineData("*.htm", true, "http://127.0.0.1/relative.htm", true)] |
| 34 | + [InlineData("*/relative.htm", true, "http://127.0.0.1/relative.htm", true)] |
| 35 | + [InlineData("/*/relative.htm", true, "http://127.0.0.1/folder/relative.htm", false)] |
| 36 | + [InlineData("/*/relative.htm", true, "http://127.0.0.1/relative.htm", false)] |
| 37 | + [InlineData("/folder/*.htm", true, "http://127.0.0.1/folder/relative.htm", false)] |
| 38 | + [InlineData("*/folder/*.htm", true, "http://127.0.0.1/folder/relative.htm", true)] |
| 39 | + [InlineData("/folder/*.htm", true, "http://127.0.0.1/relative.htm", false)] |
| 40 | + [InlineData("/*/*/relative.*", true, "http://127.0.0.1/folder1/folder2/relative.htm", false)] |
| 41 | + [InlineData("*/folder1/*/relative.*", true, "http://127.0.0.1/folder1/folder2/relative.htm", true)] |
| 42 | + [InlineData("/*/*/relative.*", true, "http://127.0.0.1/folder1/relative.htm", false)] |
| 43 | + [InlineData("http://127.0.0.1/*.htm", true, "http://127.0.0.1/absolute.htm", true)] |
| 44 | + [InlineData("http://127.0.0.1/*.htm", true, "http://127.0.0.1/folder/absolute.htm", true)] |
| 45 | + public void Given_uriString_when_matching_should_match(string uriString, bool hasWildcard, string requestUri, bool isMatch) |
| 46 | + { |
| 47 | + var request = new HttpRequestMessage { RequestUri = new Uri(requestUri, UriKind.Absolute) }; |
| 48 | + var sut = new RequestUriMatcher(uriString, hasWildcard); |
| 49 | + |
| 50 | + // Act & assert |
| 51 | + sut.IsMatch(new MockHttpRequestContext(request)).Should().Be(isMatch); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void Given_null_uri_when_creating_matcher_should_throw() |
| 56 | + { |
| 57 | + Uri? uri = null; |
| 58 | + |
| 59 | + // Act |
| 60 | + Func<RequestUriMatcher> act = () => new RequestUriMatcher(uri!); |
| 61 | + |
| 62 | + // Assert |
| 63 | + act.Should() |
| 64 | + .Throw<ArgumentNullException>() |
| 65 | + .WithParameterName(nameof(uri)); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void Given_null_uriString_when_creating_matcher_should_throw() |
| 70 | + { |
| 71 | + string? uriString = null; |
| 72 | + |
| 73 | + // Act |
| 74 | + Func<RequestUriMatcher> act = () => new RequestUriMatcher(uriString!, false); |
| 75 | + |
| 76 | + // Assert |
| 77 | + act.Should() |
| 78 | + .Throw<ArgumentNullException>() |
| 79 | + .WithParameterName(nameof(uriString)); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void When_formatting_should_return_human_readable_representation() |
| 84 | + { |
| 85 | + const string expectedText = "RequestUri: '*/controller/*'"; |
| 86 | + var sut = new RequestUriMatcher("*/controller/*"); |
| 87 | + |
| 88 | + // Act |
| 89 | + string displayText = sut.ToString(); |
| 90 | + |
| 91 | + // Assert |
| 92 | + displayText.Should().Be(expectedText); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void Given_null_context_when_matching_it_should_throw() |
| 97 | + { |
| 98 | + var sut = new RequestUriMatcher("*/controller/*"); |
| 99 | + MockHttpRequestContext? requestContext = null; |
| 100 | + |
| 101 | + // Act |
| 102 | + Action act = () => sut.IsMatch(requestContext!); |
| 103 | + |
| 104 | + // Assert |
| 105 | + act.Should() |
| 106 | + .Throw<ArgumentNullException>() |
| 107 | + .WithParameterName(nameof(requestContext)); |
| 108 | + } |
| 109 | +} |
0 commit comments