|
| 1 | +using TestableHttpClient.Utils; |
| 2 | + |
| 3 | +namespace TestableHttpClient.Tests; |
| 4 | + |
| 5 | +public sealed class RequestBuilderExtensionsTests |
| 6 | +{ |
| 7 | + private readonly RequestBuilder sut = new(new()); |
| 8 | + |
| 9 | + [Fact] |
| 10 | + public void Get_WithNullBuilder_ShouldThrowArgumentNullException() |
| 11 | + { |
| 12 | + RequestBuilder sut = null!; |
| 13 | + Assert.Throws<ArgumentNullException>(() => sut.Get("https://localhost")); |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void Get_WithNullRequestUri_ShouldThrowArgumentNullException() |
| 18 | + { |
| 19 | + Assert.Throws<ArgumentNullException>(() => sut.Get(null!)); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void Get_ShouldSetHttpMethodAndRequestUri() |
| 24 | + { |
| 25 | + Request request = sut.Get("https://localhost").Build(); |
| 26 | + |
| 27 | + Assert.Equal(HttpMethod.Get, request.Method); |
| 28 | + Assert.Equal(UriPatternParser.Parse("https://localhost"), request.RequestUri); |
| 29 | + Assert.Null(request.Version); |
| 30 | + Assert.Null(request.Headers); |
| 31 | + Assert.Null(request.Content); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void Post_WithNullBuilder_ShouldThrowArgumentNullException() |
| 36 | + { |
| 37 | + RequestBuilder sut = null!; |
| 38 | + Assert.Throws<ArgumentNullException>(() => sut.Post("https://localhost", "{\"hello\":1}")); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void Post_WithNullRequestUri_ShouldThrowArgumentNullException() |
| 43 | + { |
| 44 | + Assert.Throws<ArgumentNullException>(() => sut.Post(null!, "{\"hello\":1}")); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void Post_WithNullContent_ShouldThrowArgumentNullException() |
| 49 | + { |
| 50 | + Assert.Throws<ArgumentNullException>(() => sut.Post("https://localhost", null!)); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void Post_ShouldBuildCorrectRequest() |
| 55 | + { |
| 56 | + Request request = sut.Post("https://localhost", "{\"hello\":1}").Build(); |
| 57 | + |
| 58 | + Assert.Equal(HttpMethod.Post, request.Method); |
| 59 | + Assert.Equal(UriPatternParser.Parse("https://localhost"), request.RequestUri); |
| 60 | + Assert.Null(request.Version); |
| 61 | + Assert.Null(request.Headers); |
| 62 | + Assert.Equal("{\"hello\":1}", request.Content); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void PostAsJson_WithNullBuilder_ShouldThrowArgumentNullException() |
| 67 | + { |
| 68 | + RequestBuilder sut = null!; |
| 69 | + |
| 70 | + Assert.Throws<ArgumentNullException>(() => sut.PostAsJson("https://localhost", new())); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void PostAsJson_WithNullRequestUri_ShouldThrowArgumentNullException() |
| 75 | + { |
| 76 | + Assert.Throws<ArgumentNullException>(() => sut.PostAsJson(null!, new())); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void PostAsJson_WithNullContent_ShouldBuildCorrectRequest() |
| 81 | + { |
| 82 | + Request request = sut.PostAsJson("https://localhost", null).Build(); |
| 83 | + |
| 84 | + Assert.Equal(HttpMethod.Post, request.Method); |
| 85 | + Assert.Equal(UriPatternParser.Parse("https://localhost"), request.RequestUri); |
| 86 | + Assert.Null(request.Version); |
| 87 | + Assert.Equivalent(new Dictionary<string, Value>() { ["Content-Type"] = Value.Pattern("application/json*") }, request.Headers); |
| 88 | + Assert.Equal("null", request.Content); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void PostAsJson_ShouldBuildCorrectRequest() |
| 93 | + { |
| 94 | + Request request = sut.PostAsJson("https://localhost", new { hello = 1 }).Build(); |
| 95 | + |
| 96 | + Assert.Equal(HttpMethod.Post, request.Method); |
| 97 | + Assert.Equal(UriPatternParser.Parse("https://localhost"), request.RequestUri); |
| 98 | + Assert.Null(request.Version); |
| 99 | + Assert.Equivalent(new Dictionary<string, Value>() { ["Content-Type"] = Value.Pattern("application/json*") }, request.Headers); |
| 100 | + Assert.Equal("{\"hello\":1}", request.Content); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void WithJsonContent_WithNullBuilder_ShouldThrowArgumentNullException() |
| 105 | + { |
| 106 | + RequestBuilder sut = null!; |
| 107 | + |
| 108 | + Assert.Throws<ArgumentNullException>(() => sut.WithJsonContent(new())); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void WithJsonContent_WithNullContent_ShouldBuildCorrectRequest() |
| 113 | + { |
| 114 | + Request request = sut.WithJsonContent(null).Build(); |
| 115 | + |
| 116 | + Assert.Null(request.Method); |
| 117 | + Assert.Null(request.RequestUri); |
| 118 | + Assert.Null(request.Version); |
| 119 | + Assert.Equivalent(new Dictionary<string, Value>() { ["Content-Type"] = Value.Pattern("application/json*") }, request.Headers); |
| 120 | + Assert.Equal("null", request.Content); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void WithJsonContent_ShouldBuildCorrectRequest() |
| 125 | + { |
| 126 | + Request request = sut.WithJsonContent(new { hello = 1 }).Build(); |
| 127 | + |
| 128 | + Assert.Null(request.Method); |
| 129 | + Assert.Null(request.RequestUri); |
| 130 | + Assert.Null(request.Version); |
| 131 | + Assert.Equivalent(new Dictionary<string, Value>() { ["Content-Type"] = Value.Pattern("application/json*") }, request.Headers); |
| 132 | + Assert.Equal("{\"hello\":1}", request.Content); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void WithFormUrlEncodedContent_WithNullBuilder_ShouldThrowArgumentNullException() |
| 137 | + { |
| 138 | + RequestBuilder sut = null!; |
| 139 | + |
| 140 | + Assert.Throws<ArgumentNullException>(() => sut.WithFormUrlEncodedContent([])); |
| 141 | + } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + public void WithFormUrlEncodedContent_WithNullFormValues_ShouldThrowArgumentNullException() |
| 145 | + { |
| 146 | + Assert.Throws<ArgumentNullException>(() => sut.WithFormUrlEncodedContent(null!)); |
| 147 | + } |
| 148 | + |
| 149 | + [Fact] |
| 150 | + public void WithFormUrlEncodedContent_ShouldBuildCorrectRequest() |
| 151 | + { |
| 152 | + Request request = sut.WithFormUrlEncodedContent([new KeyValuePair<string?, string?>("username", "alice")]).Build(); |
| 153 | + |
| 154 | + Assert.Null(request.Method); |
| 155 | + Assert.Null(request.RequestUri); |
| 156 | + Assert.Null(request.Version); |
| 157 | + Assert.Equivalent(new Dictionary<string, Value>() { ["Content-Type"] = Value.Pattern("application/x-www-form-urlencoded*") }, request.Headers); |
| 158 | + Assert.Equal("username=alice", request.Content); |
| 159 | + } |
| 160 | +} |
0 commit comments