forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTemplateTest.java
More file actions
347 lines (277 loc) · 11.1 KB
/
RequestTemplateTest.java
File metadata and controls
347 lines (277 loc) · 11.1 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* Copyright 2012-2018 The Feign Authors
*
* 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 org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import static feign.RequestTemplate.expand;
import static feign.assertj.FeignAssertions.assertThat;
import static java.util.Arrays.asList;
import static org.assertj.core.data.MapEntry.entry;
public class RequestTemplateTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
/**
* Avoid depending on guava solely for map literals.
*/
private static <K, V> Map<K, V> mapOf(K key, V val) {
Map<K, V> result = new LinkedHashMap<K, V>();
result.put(key, val);
return result;
}
private static <K, V> Map<K, V> mapOf(K k1, V v1, K k2, V v2) {
Map<K, V> result = mapOf(k1, v1);
result.put(k2, v2);
return result;
}
private static <K, V> Map<K, V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3) {
Map<K, V> result = mapOf(k1, v1, k2, v2);
result.put(k3, v3);
return result;
}
@Test
public void expandNotUrlEncoded() {
for (String val : Arrays.asList("apples", "sp ace", "unic???de", "qu?stion")) {
assertThat(expand("/users/{user}", mapOf("user", val)))
.isEqualTo("/users/" + val);
}
}
@Test
public void expandMultipleParams() {
assertThat(expand("/users/{user}/{repo}", mapOf("user", "unic???de", "repo", "foo")))
.isEqualTo("/users/unic???de/foo");
}
@Test
public void expandParamKeyHyphen() {
assertThat(expand("/{user-dir}", mapOf("user-dir", "foo")))
.isEqualTo("/foo");
}
@Test
public void expandMissingParamProceeds() {
assertThat(expand("/{user-dir}", mapOf("user_dir", "foo")))
.isEqualTo("/{user-dir}");
}
@Test
public void resolveTemplateWithParameterizedPathSkipsEncodingSlash() {
RequestTemplate template = new RequestTemplate().method("GET")
.append("{zoneId}");
template.resolve(mapOf("zoneId", "/hostedzone/Z1PA6795UKMFR9"));
assertThat(template)
.hasUrl("/hostedzone/Z1PA6795UKMFR9");
}
@Test
public void canInsertAbsoluteHref() {
RequestTemplate template = new RequestTemplate().method("GET")
.append("/hostedzone/Z1PA6795UKMFR9");
template.insert(0, "https://route53.amazonaws.com/2012-12-12");
assertThat(template)
.hasUrl("https://route53.amazonaws.com/2012-12-12/hostedzone/Z1PA6795UKMFR9");
}
@Test
public void resolveTemplateWithBaseAndParameterizedQuery() {
RequestTemplate template = new RequestTemplate().method("GET")
.append("/?Action=DescribeRegions").query("RegionName.1", "{region}");
template.resolve(mapOf("region", "eu-west-1"));
assertThat(template)
.hasQueries(
entry("Action", asList("DescribeRegions")),
entry("RegionName.1", asList("eu-west-1")));
}
@Test
public void resolveTemplateWithBaseAndParameterizedIterableQuery() {
RequestTemplate template = new RequestTemplate().method("GET")
.append("/?Query=one").query("Queries", "{queries}");
template.resolve(mapOf("queries", Arrays.asList("us-east-1", "eu-west-1")));
assertThat(template)
.hasQueries(
entry("Query", asList("one")),
entry("Queries", asList("us-east-1", "eu-west-1")));
}
@Test
public void resolveTemplateWithHeaderSubstitutions() {
RequestTemplate template = new RequestTemplate().method("GET")
.header("Auth-Token", "{authToken}");
template.resolve(mapOf("authToken", "1234"));
assertThat(template)
.hasHeaders(entry("Auth-Token", asList("1234")));
}
@Test
public void resolveTemplateWithHeaderSubstitutionsNotAtStart() {
RequestTemplate template = new RequestTemplate().method("GET")
.header("Authorization", "Bearer {token}");
template.resolve(mapOf("token", "1234"));
assertThat(template)
.hasHeaders(entry("Authorization", asList("Bearer 1234")));
}
@Test
public void resolveTemplateWithHeaderWithEscapedCurlyBrace() {
RequestTemplate template = new RequestTemplate().method("GET")
.header("Encoded", "{{{{dont_expand_me}}");
template.resolve(mapOf("dont_expand_me", "1234"));
assertThat(template)
.hasHeaders(entry("Encoded", asList("{{dont_expand_me}}")));
}
/** This ensures we don't mess up vnd types */
@Test
public void resolveTemplateWithHeaderIncludingSpecialCharacters() {
RequestTemplate template = new RequestTemplate().method("GET")
.header("Accept", "application/vnd.github.v3+{type}");
template.resolve(mapOf("type", "json"));
assertThat(template)
.hasHeaders(entry("Accept", asList("application/vnd.github.v3+json")));
}
@Test
public void resolveTemplateWithHeaderEmptyResult() {
RequestTemplate template = new RequestTemplate().method("GET")
.header("Encoded", "{var}");
template.resolve(mapOf("var", ""));
assertThat(template)
.hasHeaders(entry("Encoded", asList("")));
}
@Test
public void resolveTemplateWithMixedRequestLineParams() throws Exception {
RequestTemplate template = new RequestTemplate().method("GET")//
.append("/domains/{domainId}/records")//
.query("name", "{name}")//
.query("type", "{type}");
template = template.resolve(
mapOf("domainId", 1001, "name", "denominator.io", "type", "CNAME"));
assertThat(template)
.hasUrl("/domains/1001/records")
.hasQueries(
entry("name", asList("denominator.io")),
entry("type", asList("CNAME")));
}
@Test
public void insertHasQueryParams() throws Exception {
RequestTemplate template = new RequestTemplate().method("GET")//
.append("/domains/1001/records")//
.query("name", "denominator.io")//
.query("type", "CNAME");
template.insert(0, "https://host/v1.0/1234?provider=foo");
assertThat(template)
.hasUrl("https://host/v1.0/1234/domains/1001/records")
.hasQueries(
entry("provider", asList("foo")),
entry("name", asList("denominator.io")),
entry("type", asList("CNAME")));
}
@Test
public void resolveTemplateWithBodyTemplateSetsBodyAndContentLength() {
RequestTemplate template = new RequestTemplate().method("POST")
.bodyTemplate(
"%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", " +
"\"password\": \"{password}\"%7D");
template = template.resolve(
mapOf(
"customer_name", "netflix",
"user_name", "denominator",
"password", "password"));
assertThat(template)
.hasBody(
"{\"customer_name\": \"netflix\", \"user_name\": \"denominator\", \"password\": \"password\"}")
.hasHeaders(
entry("Content-Length", asList(String.valueOf(template.body().length))));
}
@Test
public void resolveTemplateWithBodyTemplateDoesNotDoubleDecode() {
RequestTemplate template = new RequestTemplate().method("POST")
.bodyTemplate(
"%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D");
template = template.resolve(
mapOf(
"customer_name", "netflix",
"user_name", "denominator",
"password", "abc+123%25d8"));
assertThat(template)
.hasBody(
"{\"customer_name\": \"netflix\", \"user_name\": \"denominator\", \"password\": \"abc+123%25d8\"}");
}
@Test
public void skipUnresolvedQueries() throws Exception {
RequestTemplate template = new RequestTemplate().method("GET")//
.append("/domains/{domainId}/records")//
.query("optional", "{optional}")//
.query("name", "{nameVariable}");
template = template.resolve(mapOf(
"domainId", 1001,
"nameVariable", "denominator.io"));
assertThat(template)
.hasUrl("/domains/1001/records")
.hasQueries(
entry("name", asList("denominator.io")));
}
@Test
public void allQueriesUnresolvable() throws Exception {
RequestTemplate template = new RequestTemplate().method("GET")//
.append("/domains/{domainId}/records")//
.query("optional", "{optional}")//
.query("optional2", "{optional2}");
template = template.resolve(mapOf("domainId", 1001));
assertThat(template)
.hasUrl("/domains/1001/records")
.hasQueries();
}
@Test
public void spaceEncodingInUrlParam() {
RequestTemplate template = new RequestTemplate().method("GET")//
.append("/api/{value1}?key={value2}");
template = template.resolve(mapOf("value1", "ABC 123", "value2", "XYZ 123"));
assertThat(template.request().url())
.isEqualTo("/api/ABC%20123?key=XYZ+123");
}
@Test
public void encodeSlashTest() throws Exception {
RequestTemplate template = new RequestTemplate().method("GET")
.append("/api/{vhost}")
.decodeSlash(false);
template.resolve(mapOf("vhost", "/"));
assertThat(template)
.hasUrl("/api/%2F");
}
/** Implementations have a bug if they pass junk as the http method. */
@Test
public void uriStuffedIntoMethod() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid HTTP Method: /path?queryParam={queryParam}");
new RequestTemplate().method("/path?queryParam={queryParam}");
}
@Test
public void encodedQueryClearedOnNull() throws Exception {
RequestTemplate template = new RequestTemplate();
template.query("param[]", "value");
assertThat(template).hasQueries(entry("param[]", asList("value")));
template.query("param[]", (String[]) null);
assertThat(template.queries()).isEmpty();
}
@Test
public void encodedQuery() throws Exception {
RequestTemplate template = new RequestTemplate().query(true, "params[]", "foo%20bar");
assertThat(template.queryLine()).isEqualTo("?params[]=foo%20bar");
assertThat(template).hasQueries(entry("params[]", asList("foo bar")));
}
@Test
public void encodedQueryWithUnsafeCharactersMixedWithUnencoded() throws Exception {
RequestTemplate template = new RequestTemplate()
.query(false, "params[]", "not encoded") // stored as "param%5D%5B"
.query(true, "params[]", "encoded"); // stored as "param[]"
// We can't ensure consistent behavior, because decode("param[]") == decode("param%5B%5D")
assertThat(template.queryLine()).isEqualTo("?params%5B%5D=not+encoded¶ms[]=encoded");
assertThat(template.queries()).doesNotContain(entry("params[]", asList("not encoded")));
assertThat(template.queries()).contains(entry("params[]", asList("encoded")));
}
}