|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Xunit; |
| 6 | +using netmockery; |
| 7 | + |
| 8 | +namespace UnitTests |
| 9 | +{ |
| 10 | + public class TestMatchHttpMethod |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void MatchAnyMethod() |
| 14 | + { |
| 15 | + var matcher = new JSONRequestMatcher().CreateRequestMatcher() as AnyMatcher; |
| 16 | + Assert.NotNull(matcher); |
| 17 | + Assert.True(matcher.MatchesHttpMethod("POST")); |
| 18 | + Assert.True(matcher.MatchesHttpMethod("GET")); |
| 19 | + Assert.True(matcher.MatchesHttpMethod("PUT")); |
| 20 | + Assert.True(matcher.MatchesHttpMethod("HEAD")); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void MatchOnlySpecific() |
| 25 | + { |
| 26 | + var jsonMatcher = new JSONRequestMatcher |
| 27 | + { |
| 28 | + methods = "POST PUT" |
| 29 | + }; |
| 30 | + |
| 31 | + var matcher = jsonMatcher.CreateRequestMatcher() as AnyMatcher; |
| 32 | + Assert.NotNull(matcher); |
| 33 | + Assert.True(matcher.MatchesHttpMethod("POST")); |
| 34 | + Assert.False(matcher.MatchesHttpMethod("GET")); |
| 35 | + Assert.True(matcher.MatchesHttpMethod("PUT")); |
| 36 | + Assert.False(matcher.MatchesHttpMethod("HEAD")); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void IsCaseInsensitive() |
| 41 | + { |
| 42 | + var jsonMatcher = new JSONRequestMatcher |
| 43 | + { |
| 44 | + methods = "poST Put" |
| 45 | + }; |
| 46 | + |
| 47 | + var matcher = jsonMatcher.CreateRequestMatcher() as AnyMatcher; |
| 48 | + Assert.NotNull(matcher); |
| 49 | + Assert.True(matcher.MatchesHttpMethod("post")); |
| 50 | + Assert.True(matcher.MatchesHttpMethod("PUT")); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void RegexMatcher() |
| 55 | + { |
| 56 | + var matcher = (new JSONRequestMatcher { methods = "post", regex = "foobar" }).CreateRequestMatcher() as RegexMatcher; |
| 57 | + Assert.NotNull(matcher); |
| 58 | + Assert.True(matcher.MatchesHttpMethod("post")); |
| 59 | + Assert.False(matcher.MatchesHttpMethod("get")); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void TestCaseDefaultMethodIsGet() |
| 64 | + { |
| 65 | + var testCase = (new JSONTest { requestpath = "/foo/bar" }).CreateTestCase("."); |
| 66 | + Assert.Equal("GET", testCase.Method); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void CanSpecifyMethodInJson() |
| 71 | + { |
| 72 | + var testCase = (new JSONTest { requestpath = "/foo/bar", method = "POST" }).CreateTestCase("."); |
| 73 | + Assert.Equal("POST", testCase.Method); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void Foo() |
| 78 | + { |
| 79 | + var endpoint = (new JSONEndpoint |
| 80 | + { |
| 81 | + name = "endpoint", |
| 82 | + pathregex = "/", |
| 83 | + responses = new[] |
| 84 | + { |
| 85 | + new JSONResponse { match = new JSONRequestMatcher { methods = "POST" }, literal = "Response from POST" }, |
| 86 | + new JSONResponse { match = new JSONRequestMatcher {methods ="GET" }, literal = "Response from GET" } |
| 87 | + } |
| 88 | + }).CreateEndpoint(".", null); |
| 89 | + Assert.NotNull(endpoint); |
| 90 | + Assert.Equal(2, endpoint.Responses.Count()); |
| 91 | + |
| 92 | + var getTestCase = new NetmockeryTestCase { Method = "GET", RequestPath = "/", ExpectedResponseBody = "Response from GET" }; |
| 93 | + Assert.True(getTestCase.Execute(EndpointCollection.WithEndpoints(endpoint)).OK); |
| 94 | + |
| 95 | + var postTestCase = new NetmockeryTestCase { Method = "POST", RequestPath = "/", ExpectedResponseBody = "Response from POST" }; |
| 96 | + Assert.True(postTestCase.Execute(EndpointCollection.WithEndpoints(endpoint)).OK); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments