-
-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathExplicitInterfaceRefitTests.cs
More file actions
253 lines (190 loc) · 8.12 KB
/
ExplicitInterfaceRefitTests.cs
File metadata and controls
253 lines (190 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Refit;
using RichardSzalay.MockHttp;
using Xunit;
namespace Refit.Tests;
public class ExplicitInterfaceRefitTests
{
sealed class SyncCapableMockHttpMessageHandler : MockHttpMessageHandler
{
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) =>
SendAsync(request, cancellationToken).GetAwaiter().GetResult();
}
public interface IFoo
{
int Bar();
}
// Internal interface with a default implementation of IFoo.Bar that calls an internal Refit method
internal interface IInternalFoo : IFoo
{
int IFoo.Bar() => InternalBar() + 1;
[Get("/bar")]
internal int InternalBar();
}
// Derived interface that explicitly implements IFoo.Bar and marks it as a Refit method
public interface IRemoteFoo2 : IFoo
{
[Get("/bar")]
abstract int IFoo.Bar();
}
// Interfaces used to test the full sync pipeline
public interface ISyncPipelineApi
{
[Get("/resource")]
internal string GetString();
[Get("/resource")]
internal HttpResponseMessage GetHttpResponseMessage();
[Get("/resource")]
internal HttpContent GetHttpContent();
[Get("/resource")]
internal Stream GetStream();
[Get("/resource")]
internal IApiResponse<string> GetApiResponse();
[Get("/resource")]
internal IApiResponse GetRawApiResponse();
[Get("/resource")]
internal void DoVoid();
}
[Fact]
public void DefaultInterfaceImplementation_calls_internal_refit_method()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/bar")
.Respond("application/json", "41");
var fixture = RestService.For<IInternalFoo>("http://foo", settings);
var result = ((IFoo)fixture).Bar();
Assert.Equal(42, result);
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Explicit_interface_member_with_refit_attribute_is_invoked()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/bar")
.Respond("application/json", "41");
var fixture = RestService.For<IRemoteFoo2>("http://foo", settings);
var result = ((IFoo)fixture).Bar();
Assert.Equal(41, result);
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Sync_method_throws_ApiException_on_error_response()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/resource")
.Respond(HttpStatusCode.NotFound);
var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings);
var ex = Assert.Throws<ApiException>(() => fixture.GetString());
Assert.Equal(HttpStatusCode.NotFound, ex.StatusCode);
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Sync_method_returns_HttpResponseMessage_without_running_ExceptionFactory()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/resource")
.Respond(HttpStatusCode.NotFound);
var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings);
// Should not throw even for a 404 – caller owns the response
using var resp = fixture.GetHttpResponseMessage();
Assert.Equal(HttpStatusCode.NotFound, resp.StatusCode);
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Sync_method_returns_HttpContent_without_disposing_response()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/resource")
.Respond("text/plain", "hello");
var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings);
var content = fixture.GetHttpContent();
Assert.NotNull(content);
var text = content.ReadAsStringAsync().GetAwaiter().GetResult();
Assert.Equal("hello", text);
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Sync_method_returns_Stream_without_disposing_response()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/resource")
.Respond("text/plain", "hello");
var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings);
using var stream = fixture.GetStream();
Assert.NotNull(stream);
using var reader = new StreamReader(stream);
Assert.Equal("hello", reader.ReadToEnd());
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Sync_method_returns_IApiResponse_with_error_on_bad_status()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/resource")
.Respond(HttpStatusCode.InternalServerError);
var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings);
using var apiResp = fixture.GetApiResponse();
Assert.False(apiResp.IsSuccessStatusCode);
Assert.NotNull(apiResp.Error);
Assert.Equal(HttpStatusCode.InternalServerError, apiResp.Error!.StatusCode);
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Sync_method_returns_IApiResponse_with_content_on_success()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/resource")
.Respond("application/json", "\"hello\"");
var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings);
using var apiResp = fixture.GetApiResponse();
Assert.True(apiResp.IsSuccessStatusCode);
Assert.Null(apiResp.Error);
// The string branch reads the raw stream (no JSON unwrapping), same as the async path
Assert.Equal("\"hello\"", apiResp.Content);
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Sync_void_method_throws_ApiException_on_error_response()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/resource")
.Respond(HttpStatusCode.BadRequest);
var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings);
var ex = Assert.Throws<ApiException>(() => fixture.DoVoid());
Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode);
mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public void Sync_void_method_succeeds_on_ok_response()
{
var mockHttp = new SyncCapableMockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "http://foo/resource")
.Respond(HttpStatusCode.OK);
var fixture = RestService.For<ISyncPipelineApi>("http://foo", settings);
fixture.DoVoid(); // should not throw
mockHttp.VerifyNoOutstandingExpectation();
}
}