forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultContractTest.java
More file actions
282 lines (236 loc) · 12.8 KB
/
DefaultContractTest.java
File metadata and controls
282 lines (236 loc) · 12.8 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feign;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.gson.reflect.TypeToken;
import org.testng.annotations.Test;
import javax.inject.Named;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.List;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
/**
* Tests interfaces defined per {@link feign.Contract.Default} are interpreted into expected {@link feign
* .RequestTemplate template}
* instances.
*/
@Test
public class DefaultContractTest {
Contract.Default contract = new Contract.Default();
interface Methods {
@RequestLine("POST /") void post();
@RequestLine("PUT /") void put();
@RequestLine("GET /") void get();
@RequestLine("DELETE /") void delete();
}
@Test public void httpMethods() throws Exception {
assertEquals(contract.parseAndValidatateMetadata(Methods.class.getDeclaredMethod("post")).template().method(),
"POST");
assertEquals(contract.parseAndValidatateMetadata(Methods.class.getDeclaredMethod("put")).template().method(),
"PUT");
assertEquals(contract.parseAndValidatateMetadata(Methods.class.getDeclaredMethod("get")).template().method(),
"GET");
assertEquals(contract.parseAndValidatateMetadata(Methods.class.getDeclaredMethod("delete")).template().method(),
"DELETE");
}
interface BodyParams {
@RequestLine("POST") Response post(List<String> body);
@RequestLine("POST") Response tooMany(List<String> body, List<String> body2);
}
@Test public void bodyParamIsGeneric() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(BodyParams.class.getDeclaredMethod("post",
List.class));
assertNull(md.template().body());
assertNull(md.template().bodyTemplate());
assertNull(md.urlIndex());
assertEquals(md.bodyIndex(), Integer.valueOf(0));
assertEquals(md.bodyType(), new TypeToken<List<String>>() {
}.getType());
}
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "Method has too many Body.*")
public void tooManyBodies() throws Exception {
contract.parseAndValidatateMetadata(BodyParams.class.getDeclaredMethod("tooMany", List.class, List.class));
}
interface CustomMethodAndURIParam {
@RequestLine("PATCH") Response patch(URI nextLink);
}
@Test public void requestLineOnlyRequiresMethod() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(CustomMethodAndURIParam.class.getDeclaredMethod("patch",
URI.class));
assertEquals(md.template().method(), "PATCH");
assertEquals(md.template().url(), "");
assertTrue(md.template().queries().isEmpty());
assertTrue(md.template().headers().isEmpty());
assertNull(md.template().body());
assertNull(md.template().bodyTemplate());
assertEquals(md.urlIndex(), Integer.valueOf(0));
}
interface WithQueryParamsInPath {
@RequestLine("GET /") Response none();
@RequestLine("GET /?Action=GetUser") Response one();
@RequestLine("GET /?Action=GetUser&Version=2010-05-08") Response two();
@RequestLine("GET /?Action=GetUser&Version=2010-05-08&limit=1") Response three();
@RequestLine("GET /?flag&Action=GetUser&Version=2010-05-08") Response empty();
}
@Test public void queryParamsInPathExtract() throws Exception {
{
MethodMetadata md = contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("none"));
assertEquals(md.template().url(), "/");
assertTrue(md.template().queries().isEmpty());
assertEquals(md.template().toString(), "GET / HTTP/1.1\n");
}
{
MethodMetadata md = contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("one"));
assertEquals(md.template().url(), "/");
assertEquals(md.template().queries().get("Action"), ImmutableSet.of("GetUser"));
assertEquals(md.template().toString(), "GET /?Action=GetUser HTTP/1.1\n");
}
{
MethodMetadata md = contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("two"));
assertEquals(md.template().url(), "/");
assertEquals(md.template().queries().get("Action"), ImmutableSet.of("GetUser"));
assertEquals(md.template().queries().get("Version"), ImmutableSet.of("2010-05-08"));
assertEquals(md.template().toString(), "GET /?Action=GetUser&Version=2010-05-08 HTTP/1.1\n");
}
{
MethodMetadata md = contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("three"));
assertEquals(md.template().url(), "/");
assertEquals(md.template().queries().get("Action"), ImmutableSet.of("GetUser"));
assertEquals(md.template().queries().get("Version"), ImmutableSet.of("2010-05-08"));
assertEquals(md.template().queries().get("limit"), ImmutableSet.of("1"));
assertEquals(md.template().toString(), "GET /?Action=GetUser&Version=2010-05-08&limit=1 HTTP/1.1\n");
}
{
MethodMetadata md = contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("empty"));
assertEquals(md.template().url(), "/");
assertTrue(md.template().queries().containsKey("flag"));
assertEquals(md.template().queries().get("Action"), ImmutableSet.of("GetUser"));
assertEquals(md.template().queries().get("Version"), ImmutableSet.of("2010-05-08"));
assertEquals(md.template().toString(), "GET /?flag&Action=GetUser&Version=2010-05-08 HTTP/1.1\n");
}
}
interface BodyWithoutParameters {
@RequestLine("POST /")
@Headers("Content-Type: application/xml")
@Body("<v01:getAccountsListOfUser/>") Response post();
}
@Test public void bodyWithoutParameters() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(BodyWithoutParameters.class.getDeclaredMethod("post"));
assertEquals(md.template().body(), "<v01:getAccountsListOfUser/>");
assertFalse(md.template().bodyTemplate() != null);
assertTrue(md.formParams().isEmpty());
assertTrue(md.indexToName().isEmpty());
}
@Test public void producesAddsContentTypeHeader() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(BodyWithoutParameters.class.getDeclaredMethod("post"));
assertEquals(md.template().headers().get("Content-Type"), ImmutableSet.of("application/xml"));
}
interface WithURIParam {
@RequestLine("GET /{1}/{2}") Response uriParam(@Named("1") String one, URI endpoint, @Named("2") String two);
}
@Test public void methodCanHaveUriParam() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(WithURIParam.class.getDeclaredMethod("uriParam", String.class,
URI.class, String.class));
assertEquals(md.urlIndex(), Integer.valueOf(1));
}
@Test public void pathParamsParseIntoIndexToName() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(WithURIParam.class.getDeclaredMethod("uriParam", String.class,
URI.class, String.class));
assertEquals(md.template().url(), "/{1}/{2}");
assertEquals(md.indexToName().get(0), ImmutableSet.of("1"));
assertEquals(md.indexToName().get(2), ImmutableSet.of("2"));
}
interface WithPathAndQueryParams {
@RequestLine("GET /domains/{domainId}/records?name={name}&type={type}")
Response recordsByNameAndType(@Named("domainId") int id, @Named("name") String nameFilter,
@Named("type") String typeFilter);
}
@Test public void mixedRequestLineParams() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(WithPathAndQueryParams.class.getDeclaredMethod
("recordsByNameAndType", int.class, String.class, String.class));
assertNull(md.template().body());
assertNull(md.template().bodyTemplate());
assertTrue(md.template().headers().isEmpty());
assertEquals(md.template().url(), "/domains/{domainId}/records");
assertEquals(md.template().queries().get("name"), ImmutableSet.of("{name}"));
assertEquals(md.template().queries().get("type"), ImmutableSet.of("{type}"));
assertEquals(md.indexToName().get(0), ImmutableSet.of("domainId"));
assertEquals(md.indexToName().get(1), ImmutableSet.of("name"));
assertEquals(md.indexToName().get(2), ImmutableSet.of("type"));
assertEquals(md.template().toString(), "GET /domains/{domainId}/records?name={name}&type={type} HTTP/1.1\n");
}
interface FormParams {
@RequestLine("POST /")
@Body("%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D")
void login(
@Named("customer_name") String customer,
@Named("user_name") String user, @Named("password") String password);
}
@Test public void formParamsParseIntoIndexToName() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(FormParams.class.getDeclaredMethod("login", String.class,
String.class, String.class));
assertFalse(md.template().body() != null);
assertEquals(md.template().bodyTemplate(),
"%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D");
assertEquals(md.formParams(), ImmutableList.of("customer_name", "user_name", "password"));
assertEquals(md.indexToName().get(0), ImmutableSet.of("customer_name"));
assertEquals(md.indexToName().get(1), ImmutableSet.of("user_name"));
assertEquals(md.indexToName().get(2), ImmutableSet.of("password"));
}
interface HeaderParams {
@RequestLine("POST /")
@Headers("Auth-Token: {Auth-Token}") void logout(@Named("Auth-Token") String token);
}
@Test public void headerParamsParseIntoIndexToName() throws Exception {
MethodMetadata md = contract.parseAndValidatateMetadata(HeaderParams.class.getDeclaredMethod("logout", String.class));
assertEquals(md.template().headers().get("Auth-Token"), ImmutableSet.of("{Auth-Token}"));
assertEquals(md.indexToName().get(0), ImmutableSet.of("Auth-Token"));
}
interface WithIncrementalCallback {
@RequestLine("GET /") void valid(IncrementalCallback<List<String>> one);
@RequestLine("GET /{path}") void badOrder(IncrementalCallback<List<String>> one, @Named("path") String path);
@RequestLine("GET /") Response returnType(IncrementalCallback<List<String>> one);
@RequestLine("GET /") void wildcardExtends(IncrementalCallback<? extends List<String>> one);
@RequestLine("GET /") void subtype(ParameterizedIncrementalCallback<List<String>> one);
}
static final List<String> listString = null;
interface ParameterizedIncrementalCallback<T extends List<String>> extends IncrementalCallback<T> {
}
@Test public void methodCanHaveIncrementalCallbackParam() throws Exception {
contract.parseAndValidatateMetadata(WithIncrementalCallback.class.getDeclaredMethod("valid", IncrementalCallback.class));
}
@Test public void methodMetadataReturnTypeOnObservableMethodIsItsTypeParameter() throws Exception {
Type listStringType = getClass().getDeclaredField("listString").getGenericType();
MethodMetadata md = contract.parseAndValidatateMetadata(WithIncrementalCallback.class.getDeclaredMethod("valid", IncrementalCallback.class));
assertEquals(md.decodeInto(), listStringType);
md = contract.parseAndValidatateMetadata(WithIncrementalCallback.class.getDeclaredMethod("wildcardExtends", IncrementalCallback.class));
assertEquals(md.decodeInto(), listStringType);
md = contract.parseAndValidatateMetadata(WithIncrementalCallback.class.getDeclaredMethod("subtype", ParameterizedIncrementalCallback.class));
assertEquals(md.decodeInto(), listStringType);
}
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*the last parameter.*")
public void incrementalCallbackParamMustBeLast() throws Exception {
contract.parseAndValidatateMetadata(WithIncrementalCallback.class.getDeclaredMethod("badOrder", IncrementalCallback.class, String.class));
}
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*must return void.*")
public void incrementalCallbackMethodMustReturnVoid() throws Exception {
contract.parseAndValidatateMetadata(WithIncrementalCallback.class.getDeclaredMethod("returnType", IncrementalCallback.class));
}
}