forked from reactiveui/refit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MethodOverloads.cs
234 lines (177 loc) · 7.35 KB
/
MethodOverloads.cs
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Refit;
using RichardSzalay.MockHttp;
using Xunit;
namespace Refit.Tests
{
public interface IUseOverloadedMethods
{
[Get("")]
Task<string> Get();
[Get("/status/{httpstatuscode}")]
Task<HttpResponseMessage> Get(int httpstatuscode);
}
public interface IUseOverloadedGenericMethods<TResponse, in TParam, in THeader>
where TResponse : class
where THeader : struct
{
[Get("")]
Task<string> Get();
[Get("/get")]
Task<TResponse> Get(TParam param, [Header("X-Refit")] THeader header);
[Get("/get")]
Task<TResponse> Get(THeader param, [Header("X-Refit")] TParam header);
[Get("/status/{httpstatuscode}")]
Task<HttpResponseMessage> Get(int httpstatuscode);
[Get("/get")]
Task<TValue> Get<TValue>(int someVal);
[Get("/get")]
Task<TValue> Get<TValue, TInput>(TInput input);
[Get("/get")]
Task Get<TInput1, TInput2>(TInput1 input1, TInput2 input2);
}
public class MethodOverladTests
{
[Fact]
public async Task BasicMethodOverloadTest()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/")
.Respond(HttpStatusCode.OK, "text/html", "OK");
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/status/403")
.Respond(HttpStatusCode.Forbidden);
var fixture = RestService.For<IUseOverloadedMethods>("https://httpbin.org/", settings);
var plainText = await fixture.Get();
var resp = await fixture.Get(403);
Assert.True(!string.IsNullOrWhiteSpace(plainText));
Assert.Equal(HttpStatusCode.Forbidden, resp.StatusCode);
}
[Fact]
public async Task GenericMethodOverloadTest1()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/")
.Respond(HttpStatusCode.OK, "text/html", "OK");
var fixture = RestService.For<IUseOverloadedGenericMethods<HttpBinGet, string, int>>(
"https://httpbin.org/",
settings
);
var plainText = await fixture.Get();
Assert.True(!string.IsNullOrWhiteSpace(plainText));
}
[Fact]
public async Task GenericMethodOverloadTest2()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/status/403")
.Respond(HttpStatusCode.Forbidden);
var fixture = RestService.For<IUseOverloadedGenericMethods<HttpBinGet, string, int>>(
"https://httpbin.org/",
settings
);
var resp = await fixture.Get(403);
Assert.Equal(HttpStatusCode.Forbidden, resp.StatusCode);
}
[Fact]
public async Task GenericMethodOverloadTest3()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/get")
.WithQueryString("someVal", "201")
.Respond("application/json", "some-T-value");
var fixture = RestService.For<IUseOverloadedGenericMethods<HttpBinGet, string, int>>(
"https://httpbin.org/",
settings
);
var result = await fixture.Get<string>(201);
Assert.Equal("some-T-value", result);
}
[Fact]
public async Task GenericMethodOverloadTest4()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/get")
.WithHeaders("X-Refit", "99")
.WithQueryString("param", "foo")
.Respond(
"application/json",
"{\"url\": \"https://httpbin.org/get\", \"args\": {\"param\": \"foo\"}}"
);
var fixture = RestService.For<IUseOverloadedGenericMethods<HttpBinGet, string, int>>(
"https://httpbin.org/",
settings
);
var result = await fixture.Get("foo", 99);
Assert.Equal("foo", result.Args["param"]);
}
[Fact]
public async Task GenericMethodOverloadTest5()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/get")
.WithHeaders("X-Refit", "foo")
.WithQueryString("param", "99")
.Respond(
"application/json",
"{\"url\": \"https://httpbin.org/get\", \"args\": {\"param\": \"99\"}}"
);
var fixture = RestService.For<IUseOverloadedGenericMethods<HttpBinGet, string, int>>(
"https://httpbin.org/",
settings
);
var result = await fixture.Get(99, "foo");
Assert.Equal("99", result.Args["param"]);
}
[Fact]
public async Task GenericMethodOverloadTest6()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/get")
.WithQueryString("input", "99")
.Respond("application/json", "generic-output");
var fixture = RestService.For<IUseOverloadedGenericMethods<HttpBinGet, string, int>>(
"https://httpbin.org/",
settings
);
var result = await fixture.Get<string, int>(99);
Assert.Equal("generic-output", result);
}
[Fact]
public async Task GenericMethodOverloadTest7()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
mockHttp
.Expect(HttpMethod.Get, "https://httpbin.org/get")
.WithQueryString(
new Dictionary<string, string>() { { "input1", "str" }, { "input2", "3" } }
)
.Respond("application/json", "Ok");
var fixture = RestService.For<IUseOverloadedGenericMethods<HttpBinGet, string, int>>(
"https://httpbin.org/",
settings
);
await fixture.Get<string, int>("str", 3);
mockHttp.VerifyNoOutstandingExpectation();
}
}
}