|
1 | | -using System.Net.Http; |
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Net.Http.Headers; |
2 | 4 | using System.Threading.Tasks; |
3 | 5 | using Refit; |
4 | 6 | using RichardSzalay.MockHttp; |
@@ -35,6 +37,31 @@ public interface IRemoteFoo2 : IFoo |
35 | 37 | abstract int IFoo.Bar(); |
36 | 38 | } |
37 | 39 |
|
| 40 | + // Interfaces used to test the full sync pipeline |
| 41 | + public interface ISyncPipelineApi |
| 42 | + { |
| 43 | + [Get("/resource")] |
| 44 | + internal string GetString(); |
| 45 | + |
| 46 | + [Get("/resource")] |
| 47 | + internal HttpResponseMessage GetHttpResponseMessage(); |
| 48 | + |
| 49 | + [Get("/resource")] |
| 50 | + internal HttpContent GetHttpContent(); |
| 51 | + |
| 52 | + [Get("/resource")] |
| 53 | + internal Stream GetStream(); |
| 54 | + |
| 55 | + [Get("/resource")] |
| 56 | + internal IApiResponse<string> GetApiResponse(); |
| 57 | + |
| 58 | + [Get("/resource")] |
| 59 | + internal IApiResponse GetRawApiResponse(); |
| 60 | + |
| 61 | + [Get("/resource")] |
| 62 | + internal void DoVoid(); |
| 63 | + } |
| 64 | + |
38 | 65 | [Fact] |
39 | 66 | public void DefaultInterfaceImplementation_calls_internal_refit_method() |
40 | 67 | { |
@@ -70,4 +97,157 @@ public void Explicit_interface_member_with_refit_attribute_is_invoked() |
70 | 97 |
|
71 | 98 | mockHttp.VerifyNoOutstandingExpectation(); |
72 | 99 | } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void Sync_method_throws_ApiException_on_error_response() |
| 103 | + { |
| 104 | + var mockHttp = new SyncCapableMockHttpMessageHandler(); |
| 105 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 106 | + |
| 107 | + mockHttp |
| 108 | + .Expect(HttpMethod.Get, "http://foo/resource") |
| 109 | + .Respond(HttpStatusCode.NotFound); |
| 110 | + |
| 111 | + var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings); |
| 112 | + |
| 113 | + var ex = Assert.Throws<ApiException>(() => fixture.GetString()); |
| 114 | + Assert.Equal(HttpStatusCode.NotFound, ex.StatusCode); |
| 115 | + |
| 116 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void Sync_method_returns_HttpResponseMessage_without_running_ExceptionFactory() |
| 121 | + { |
| 122 | + var mockHttp = new SyncCapableMockHttpMessageHandler(); |
| 123 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 124 | + |
| 125 | + mockHttp |
| 126 | + .Expect(HttpMethod.Get, "http://foo/resource") |
| 127 | + .Respond(HttpStatusCode.NotFound); |
| 128 | + |
| 129 | + var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings); |
| 130 | + |
| 131 | + // Should not throw even for a 404 – caller owns the response |
| 132 | + using var resp = fixture.GetHttpResponseMessage(); |
| 133 | + Assert.Equal(HttpStatusCode.NotFound, resp.StatusCode); |
| 134 | + |
| 135 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void Sync_method_returns_HttpContent_without_disposing_response() |
| 140 | + { |
| 141 | + var mockHttp = new SyncCapableMockHttpMessageHandler(); |
| 142 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 143 | + |
| 144 | + mockHttp |
| 145 | + .Expect(HttpMethod.Get, "http://foo/resource") |
| 146 | + .Respond("text/plain", "hello"); |
| 147 | + |
| 148 | + var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings); |
| 149 | + |
| 150 | + var content = fixture.GetHttpContent(); |
| 151 | + Assert.NotNull(content); |
| 152 | + var text = content.ReadAsStringAsync().GetAwaiter().GetResult(); |
| 153 | + Assert.Equal("hello", text); |
| 154 | + |
| 155 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 156 | + } |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public void Sync_method_returns_Stream_without_disposing_response() |
| 160 | + { |
| 161 | + var mockHttp = new SyncCapableMockHttpMessageHandler(); |
| 162 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 163 | + |
| 164 | + mockHttp |
| 165 | + .Expect(HttpMethod.Get, "http://foo/resource") |
| 166 | + .Respond("text/plain", "hello"); |
| 167 | + |
| 168 | + var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings); |
| 169 | + |
| 170 | + using var stream = fixture.GetStream(); |
| 171 | + Assert.NotNull(stream); |
| 172 | + using var reader = new StreamReader(stream); |
| 173 | + Assert.Equal("hello", reader.ReadToEnd()); |
| 174 | + |
| 175 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + public void Sync_method_returns_IApiResponse_with_error_on_bad_status() |
| 180 | + { |
| 181 | + var mockHttp = new SyncCapableMockHttpMessageHandler(); |
| 182 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 183 | + |
| 184 | + mockHttp |
| 185 | + .Expect(HttpMethod.Get, "http://foo/resource") |
| 186 | + .Respond(HttpStatusCode.InternalServerError); |
| 187 | + |
| 188 | + var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings); |
| 189 | + |
| 190 | + using var apiResp = fixture.GetApiResponse(); |
| 191 | + Assert.False(apiResp.IsSuccessStatusCode); |
| 192 | + Assert.NotNull(apiResp.Error); |
| 193 | + Assert.Equal(HttpStatusCode.InternalServerError, apiResp.Error!.StatusCode); |
| 194 | + |
| 195 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 196 | + } |
| 197 | + |
| 198 | + [Fact] |
| 199 | + public void Sync_method_returns_IApiResponse_with_content_on_success() |
| 200 | + { |
| 201 | + var mockHttp = new SyncCapableMockHttpMessageHandler(); |
| 202 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 203 | + |
| 204 | + mockHttp |
| 205 | + .Expect(HttpMethod.Get, "http://foo/resource") |
| 206 | + .Respond("application/json", "\"hello\""); |
| 207 | + |
| 208 | + var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings); |
| 209 | + |
| 210 | + using var apiResp = fixture.GetApiResponse(); |
| 211 | + Assert.True(apiResp.IsSuccessStatusCode); |
| 212 | + Assert.Null(apiResp.Error); |
| 213 | + // The string branch reads the raw stream (no JSON unwrapping), same as the async path |
| 214 | + Assert.Equal("\"hello\"", apiResp.Content); |
| 215 | + |
| 216 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 217 | + } |
| 218 | + |
| 219 | + [Fact] |
| 220 | + public void Sync_void_method_throws_ApiException_on_error_response() |
| 221 | + { |
| 222 | + var mockHttp = new SyncCapableMockHttpMessageHandler(); |
| 223 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 224 | + |
| 225 | + mockHttp |
| 226 | + .Expect(HttpMethod.Get, "http://foo/resource") |
| 227 | + .Respond(HttpStatusCode.BadRequest); |
| 228 | + |
| 229 | + var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings); |
| 230 | + |
| 231 | + var ex = Assert.Throws<ApiException>(() => fixture.DoVoid()); |
| 232 | + Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode); |
| 233 | + |
| 234 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 235 | + } |
| 236 | + |
| 237 | + [Fact] |
| 238 | + public void Sync_void_method_succeeds_on_ok_response() |
| 239 | + { |
| 240 | + var mockHttp = new SyncCapableMockHttpMessageHandler(); |
| 241 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 242 | + |
| 243 | + mockHttp |
| 244 | + .Expect(HttpMethod.Get, "http://foo/resource") |
| 245 | + .Respond(HttpStatusCode.OK); |
| 246 | + |
| 247 | + var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings); |
| 248 | + |
| 249 | + fixture.DoVoid(); // should not throw |
| 250 | + |
| 251 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 252 | + } |
73 | 253 | } |
0 commit comments