|
| 1 | +using Byteology.TypedHttpClients.Tests.Mocks; |
| 2 | +using Byteology.TypedHttpClients.Tests.TestServices; |
| 3 | +using System; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Byteology.TypedHttpClients.Tests |
| 10 | +{ |
| 11 | + public class JsonHttpClientTests |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void Success() |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null); |
| 18 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 19 | + |
| 20 | + // Act |
| 21 | + Task result = service.NoResultActionAsync(); |
| 22 | + |
| 23 | + // Assert |
| 24 | + Assert.True(result.IsCompletedSuccessfully); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void Success_Generic() |
| 29 | + { |
| 30 | + // Arrange |
| 31 | + TestServiceResult response = new (); |
| 32 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, new StringContent(response.ToString())); |
| 33 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 34 | + |
| 35 | + // Act |
| 36 | + TestServiceResult result = service.ActionAsync().Result; |
| 37 | + |
| 38 | + // Assert |
| 39 | + Assert.Equal(response, result); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Error() |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.Forbidden, null); |
| 47 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 48 | + |
| 49 | + // Act |
| 50 | + Task result = service.NoResultActionAsync(); |
| 51 | + |
| 52 | + // Assert |
| 53 | + Assert.ThrowsAny<Exception>(() => result.Wait()); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void Error_Generic() |
| 58 | + { |
| 59 | + // Arrange |
| 60 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.Forbidden, null); |
| 61 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 62 | + |
| 63 | + // Act |
| 64 | + Task<TestServiceResult> result = service.ActionAsync(); |
| 65 | + |
| 66 | + // Assert |
| 67 | + Assert.ThrowsAny<Exception>(() => result.Wait()); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void Uri_Simple() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + string expectedUrl = "https://example.com/simpleuri"; |
| 75 | + void assertUri(HttpRequestMessage m) => Assert.Equal(expectedUrl, m.RequestUri?.ToString()); |
| 76 | + |
| 77 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null, assertUri); |
| 78 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 79 | + |
| 80 | + // Act |
| 81 | + Task result = service.SimpleUriAsync(); |
| 82 | + result.Wait(); |
| 83 | + |
| 84 | + // Assert |
| 85 | + Assert.True(result.IsCompletedSuccessfully); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void Uri_Simple_NoDash() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + static void assertUri(HttpRequestMessage m) => Assert.Equal("https://example.com/simpleuri", m.RequestUri?.ToString()); |
| 93 | + |
| 94 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null, assertUri); |
| 95 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 96 | + |
| 97 | + // Act |
| 98 | + Task result = service.SimpleUriNoDashAsync(); |
| 99 | + result.Wait(); |
| 100 | + |
| 101 | + // Assert |
| 102 | + Assert.True(result.IsCompletedSuccessfully); |
| 103 | + } |
| 104 | + |
| 105 | + [Theory] |
| 106 | + [InlineData("test")] |
| 107 | + [InlineData(5)] |
| 108 | + [InlineData(5.8f)] |
| 109 | + [InlineData(true)] |
| 110 | + [InlineData(null)] |
| 111 | + public void Uri_Param(object param) |
| 112 | + { |
| 113 | + // Arrange |
| 114 | + string expectedUrl = $"https://example.com/paramuri/{param}"; |
| 115 | + void assertUri(HttpRequestMessage m) => Assert.Equal(expectedUrl, m.RequestUri?.ToString()); |
| 116 | + |
| 117 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null, assertUri); |
| 118 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 119 | + |
| 120 | + // Act |
| 121 | + Task result = service.ParamUriAsync(param); |
| 122 | + result.Wait(); |
| 123 | + |
| 124 | + // Assert |
| 125 | + Assert.True(result.IsCompletedSuccessfully); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void Uri_Query() |
| 130 | + { |
| 131 | + // Arrange |
| 132 | + string expectedUrl = "https://example.com/query?i=5&s=asdf&b=True&f=5.4&n=&a=1&a=2&a=3&a="; |
| 133 | + void assertUri(HttpRequestMessage m) => Assert.Equal(expectedUrl, m.RequestUri?.ToString()); |
| 134 | + |
| 135 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null, assertUri); |
| 136 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 137 | + |
| 138 | + // Act |
| 139 | + Task result = service.QueryAsync(5, "asdf", true, 5.4f, null, new int?[] { 1, 2, 3, null }); |
| 140 | + result.Wait(); |
| 141 | + |
| 142 | + // Assert |
| 143 | + Assert.True(result.IsCompletedSuccessfully); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void Body() |
| 148 | + { |
| 149 | + // Arrange |
| 150 | + string expectedUrl = "https://example.com/actionbody"; |
| 151 | + TestServiceResult body = new(); |
| 152 | + void assertUri(HttpRequestMessage m) |
| 153 | + { |
| 154 | + Assert.Equal(expectedUrl, m.RequestUri?.ToString()); |
| 155 | + Assert.Equal(body.ToString(), m.Content.ReadAsStringAsync().Result); |
| 156 | + } |
| 157 | + |
| 158 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null, assertUri); |
| 159 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 160 | + |
| 161 | + // Act |
| 162 | + Task result = service.BodyActionAsync(body); |
| 163 | + result.Wait(); |
| 164 | + |
| 165 | + // Assert |
| 166 | + Assert.True(result.IsCompletedSuccessfully); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public void Verb() |
| 171 | + { |
| 172 | + // Arrange |
| 173 | + static void assertUri(HttpRequestMessage m) => Assert.Equal("VERB", m.Method.Method); |
| 174 | + |
| 175 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null, assertUri); |
| 176 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 177 | + |
| 178 | + // Act |
| 179 | + Task result = service.VerbAsync(); |
| 180 | + result.Wait(); |
| 181 | + |
| 182 | + // Assert |
| 183 | + Assert.True(result.IsCompletedSuccessfully); |
| 184 | + } |
| 185 | + |
| 186 | + [Fact] |
| 187 | + public void Invalid_OutParam() |
| 188 | + { |
| 189 | + // Arrange |
| 190 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null); |
| 191 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 192 | + |
| 193 | + // Act & Assert |
| 194 | + Assert.ThrowsAny<Exception>(() => service.OutParamAsync(out int p).Wait()); |
| 195 | + } |
| 196 | + |
| 197 | + [Fact] |
| 198 | + public void Invalid_MultipleBody() |
| 199 | + { |
| 200 | + // Arrange |
| 201 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null); |
| 202 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 203 | + |
| 204 | + // Act & Assert |
| 205 | + Assert.ThrowsAny<Exception>(() => service.MultipleBodyAsync(1, 2).Wait()); |
| 206 | + } |
| 207 | + |
| 208 | + [Fact] |
| 209 | + public void Invalid_NotDecorated() |
| 210 | + { |
| 211 | + // Arrange |
| 212 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null); |
| 213 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 214 | + |
| 215 | + // Act & Assert |
| 216 | + Assert.ThrowsAny<Exception>(() => service.NotDecoratedAsync().Wait()); |
| 217 | + } |
| 218 | + |
| 219 | + [Fact] |
| 220 | + public void Invalid_NotAsync() |
| 221 | + { |
| 222 | + // Arrange |
| 223 | + using HttpClient httpClient = getHttpClient(HttpStatusCode.OK, null); |
| 224 | + ITestService service = new JsonHttpClient<ITestService>(httpClient).Endpoints; |
| 225 | + |
| 226 | + // Act & Assert |
| 227 | + Assert.ThrowsAny<Exception>(() => service.NotAsync()); |
| 228 | + } |
| 229 | + |
| 230 | + private static HttpClient getHttpClient( |
| 231 | + HttpStatusCode statusCode, |
| 232 | + HttpContent content, |
| 233 | + Action<HttpRequestMessage> onBeforeSend = null) |
| 234 | + { |
| 235 | + HttpClient httpClient = MockHttpClientFactory.Create(statusCode, content, onBeforeSend); |
| 236 | + httpClient.BaseAddress = new Uri("https://example.com"); |
| 237 | + return httpClient; |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments