|
| 1 | +using TestableHttpClient.Utils; |
| 2 | + |
| 3 | +namespace TestableHttpClient.Tests.Utils; |
| 4 | + |
| 5 | +public class HttpRequestMessagePatternTests |
| 6 | +{ |
| 7 | + private readonly HttpRequestMessagePatternMatchingOptions defaultOptions = new(); |
| 8 | + |
| 9 | + [Theory] |
| 10 | + [InlineData("GET")] |
| 11 | + [InlineData("POST")] |
| 12 | + [InlineData("PATCH")] |
| 13 | + [InlineData("PUT")] |
| 14 | + [InlineData("DELETE")] |
| 15 | + [InlineData("HEAD")] |
| 16 | + [InlineData("OPTIONS")] |
| 17 | + [InlineData("TRACE")] |
| 18 | + [InlineData("CONNECT")] |
| 19 | + public void Matches_HttpRequestMessageWithAnyHttpMethod_MatchesAllHttpMethods(string httpMethod) |
| 20 | + { |
| 21 | + HttpRequestMessagePattern sut = new(); |
| 22 | + |
| 23 | + using HttpRequestMessage input = new(new HttpMethod(httpMethod), "https://localhost"); |
| 24 | + |
| 25 | + Assert.True(sut.Matches(input, defaultOptions).Method); |
| 26 | + } |
| 27 | + |
| 28 | + [Theory] |
| 29 | + [InlineData("GET", true)] |
| 30 | + [InlineData("POST", true)] |
| 31 | + [InlineData("PATCH", false)] |
| 32 | + [InlineData("PUT", false)] |
| 33 | + [InlineData("DELETE", true)] |
| 34 | + [InlineData("HEAD", false)] |
| 35 | + [InlineData("OPTIONS", false)] |
| 36 | + [InlineData("TRACE", false)] |
| 37 | + [InlineData("CONNECT", false)] |
| 38 | + public void Matches_HttpRequestMessageWithSpecificHttpMethods_OnlyMatchesSpecifiedHttpMethods(string httpMethod, bool match) |
| 39 | + { |
| 40 | + HttpRequestMessagePattern sut = new() |
| 41 | + { |
| 42 | + Method = Value.OneOf(HttpMethod.Get, HttpMethod.Post, HttpMethod.Delete) |
| 43 | + }; |
| 44 | + |
| 45 | + using HttpRequestMessage input = new(new HttpMethod(httpMethod), "https://localhost"); |
| 46 | + |
| 47 | + Assert.Equal(match, sut.Matches(input, defaultOptions).Method); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void Matches_HttpRequestMessageWithSpecificUrl_MatchesUrl() |
| 52 | + { |
| 53 | + HttpRequestMessagePattern sut = new() |
| 54 | + { |
| 55 | + RequestUri = UriPatternParser.Parse("https://localhost/test/*") |
| 56 | + }; |
| 57 | + |
| 58 | + using HttpRequestMessage matchingInput = new(HttpMethod.Get, "https://localhost/test/123"); |
| 59 | + using HttpRequestMessage notMatchingInput = new(HttpMethod.Get, "https://localhost/something/123"); |
| 60 | + |
| 61 | + Assert.True(sut.Matches(matchingInput, defaultOptions).RequestUri); |
| 62 | + Assert.False(sut.Matches(notMatchingInput, defaultOptions).RequestUri); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void Matches_HttpRequestMessageWithSpecificVersion_MatchesExactVersion() |
| 67 | + { |
| 68 | + HttpRequestMessagePattern sut = new() |
| 69 | + { |
| 70 | + Version = Value.Exact(HttpVersion.Version11) |
| 71 | + }; |
| 72 | + |
| 73 | + using HttpRequestMessage matchingVersion = new() { Version = HttpVersion.Version11 }; |
| 74 | + using HttpRequestMessage notMatchingVersion = new() { Version = HttpVersion.Version10 }; |
| 75 | + |
| 76 | + Assert.True(sut.Matches(matchingVersion, defaultOptions).Version); |
| 77 | + Assert.False(sut.Matches(notMatchingVersion, defaultOptions).Version); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void Matches_HttpRequestMessageWithoutBody_MatchesAnyBody() |
| 82 | + { |
| 83 | + HttpRequestMessagePattern sut = new() |
| 84 | + { |
| 85 | + Content = Value.Any<string>() |
| 86 | + }; |
| 87 | + |
| 88 | + using HttpRequestMessage matchingRequest = new(); |
| 89 | + |
| 90 | + Assert.True(sut.Matches(matchingRequest, defaultOptions).Content); |
| 91 | + } |
| 92 | + |
| 93 | + [Theory] |
| 94 | + [InlineData("")] |
| 95 | + [InlineData("Some text")] |
| 96 | + [InlineData("{\"key\":\"value\"}")] |
| 97 | + public void Matches_HttpRequestMessageWithBody_MatchesExactBody(string content) |
| 98 | + { |
| 99 | + HttpRequestMessagePattern sut = new() |
| 100 | + { |
| 101 | + Content = Value.Exact(content) |
| 102 | + }; |
| 103 | + |
| 104 | + using HttpRequestMessage matchingRequest = new() |
| 105 | + { |
| 106 | + Content = new StringContent(content) |
| 107 | + }; |
| 108 | + |
| 109 | + Assert.True(sut.Matches(matchingRequest, defaultOptions).Content); |
| 110 | + } |
| 111 | + |
| 112 | + [Theory] |
| 113 | + [InlineData("")] |
| 114 | + [InlineData("Some text")] |
| 115 | + [InlineData("{\"key\":\"value\"}")] |
| 116 | + public void Matches_HttpRequestMessageWithNotMatchingBody_DoesNotMatchExactBody(string content) |
| 117 | + { |
| 118 | + HttpRequestMessagePattern sut = new() |
| 119 | + { |
| 120 | + Content = Value.Exact(content) |
| 121 | + }; |
| 122 | + |
| 123 | + using HttpRequestMessage matchingRequest = new() |
| 124 | + { |
| 125 | + Content = new StringContent("Example content") |
| 126 | + }; |
| 127 | + |
| 128 | + Assert.False(sut.Matches(matchingRequest, defaultOptions).Content); |
| 129 | + } |
| 130 | +} |
0 commit comments