forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractClientTest.java
More file actions
337 lines (265 loc) · 11.2 KB
/
AbstractClientTest.java
File metadata and controls
337 lines (265 loc) · 11.2 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
/**
* 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.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import feign.Client;
import feign.CollectionFormat;
import feign.Feign.Builder;
import feign.FeignException;
import feign.Headers;
import feign.Logger;
import feign.Param;
import feign.RequestLine;
import feign.Response;
import feign.Util;
import feign.assertj.MockWebServerAssertions;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static feign.Util.UTF_8;
/**
* {@link AbstractClientTest} can be extended to run a set of tests against any {@link Client}
* implementation.
*/
public abstract class AbstractClientTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final MockWebServer server = new MockWebServer();
/**
* Create a Feign {@link Builder} with a client configured
*/
public abstract Builder newBuilder();
/**
* Some client implementation tests should override this test if the PATCH operation is
* unsupported.
*/
@Test
public void testPatch() throws Exception {
server.enqueue(new MockResponse().setBody("foo"));
server.enqueue(new MockResponse());
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
assertEquals("foo", api.patch(""));
MockWebServerAssertions.assertThat(server.takeRequest())
.hasHeaders("Accept: text/plain", "Content-Length: 0") // Note: OkHttp adds content length.
.hasNoHeaderNamed("Content-Type")
.hasMethod("PATCH");
}
@Test
public void parsesRequestAndResponse() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("foo").addHeader("Foo: Bar"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.post("foo");
assertThat(response.status()).isEqualTo(200);
assertThat(response.reason()).isEqualTo("OK");
assertThat(response.headers())
.containsEntry("Content-Length", asList("3"))
.containsEntry("Foo", asList("Bar"));
assertThat(response.body().asInputStream())
.hasContentEqualTo(new ByteArrayInputStream("foo".getBytes(UTF_8)));
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST")
.hasPath("/?foo=bar&foo=baz&qux=")
.hasHeaders("Foo: Bar", "Foo: Baz", "Qux: ", "Accept: */*", "Content-Length: 3")
.hasBody("foo");
}
@Test
public void reasonPhraseIsOptional() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setStatus("HTTP/1.1 " + 200));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.post("foo");
assertThat(response.status()).isEqualTo(200);
assertThat(response.reason()).isNullOrEmpty();
}
@Test
public void parsesErrorResponse() throws IOException, InterruptedException {
thrown.expect(FeignException.class);
thrown.expectMessage("status 500 reading TestInterface#get(); content:\n" + "ARGHH");
server.enqueue(new MockResponse().setResponseCode(500).setBody("ARGHH"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
api.get();
}
@Test
public void safeRebuffering() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("foo"));
TestInterface api = newBuilder()
.logger(new Logger() {
@Override
protected void log(String configKey, String format, Object... args) {}
})
.logLevel(Logger.Level.FULL) // rebuffers the body
.target(TestInterface.class, "http://localhost:" + server.getPort());
api.post("foo");
}
/** This shows that is a no-op or otherwise doesn't cause an NPE when there's no content. */
@Test
public void safeRebuffering_noContent() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setResponseCode(204));
TestInterface api = newBuilder()
.logger(new Logger() {
@Override
protected void log(String configKey, String format, Object... args) {}
})
.logLevel(Logger.Level.FULL) // rebuffers the body
.target(TestInterface.class, "http://localhost:" + server.getPort());
api.post("foo");
}
@Test
public void noResponseBodyForPost() {
server.enqueue(new MockResponse());
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
api.noPostBody();
}
@Test
public void noResponseBodyForPut() {
server.enqueue(new MockResponse());
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
api.noPutBody();
}
@Test
public void parsesResponseMissingLength() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setChunkedBody("foo", 1));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.post("testing");
assertThat(response.status()).isEqualTo(200);
assertThat(response.reason()).isEqualTo("OK");
assertThat(response.body().length()).isNull();
assertThat(response.body().asInputStream())
.hasContentEqualTo(new ByteArrayInputStream("foo".getBytes(UTF_8)));
}
@Test
public void postWithSpacesInPath() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("foo"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.post("current documents", "foo");
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST")
.hasPath("/path/current%20documents/resource")
.hasBody("foo");
}
@Test
public void testVeryLongResponseNullLength() throws Exception {
server.enqueue(new MockResponse()
.setBody("AAAAAAAA")
.addHeader("Content-Length", Long.MAX_VALUE));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.post("foo");
// Response length greater than Integer.MAX_VALUE should be null
assertThat(response.body().length()).isNull();
}
@Test
public void testResponseLength() throws Exception {
server.enqueue(new MockResponse()
.setBody("test"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Integer expected = 4;
Response response = api.post("");
Integer actual = response.body().length();
assertEquals(expected, actual);
}
@Test
public void testContentTypeWithCharset() throws Exception {
server.enqueue(new MockResponse()
.setBody("AAAAAAAA"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.postWithContentType("foo", "text/plain;charset=utf-8");
// Response length should not be null
assertEquals("AAAAAAAA", Util.toString(response.body().asReader()));
}
@Test
public void testContentTypeWithoutCharset() throws Exception {
server.enqueue(new MockResponse()
.setBody("AAAAAAAA"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.postWithContentType("foo", "text/plain");
// Response length should not be null
assertEquals("AAAAAAAA", Util.toString(response.body().asReader()));
}
@Test
public void testContentTypeDefaultsToRequestCharset() throws Exception {
server.enqueue(new MockResponse().setBody("foo"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
// should use utf-8 encoding by default
api.postWithContentType("àáâãäåèéêë", "text/plain");
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST")
.hasBody("àáâãäåèéêë");
}
@Test
public void testDefaultCollectionFormat() throws Exception {
server.enqueue(new MockResponse().setBody("body"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.get(Arrays.asList(new String[] {"bar", "baz"}));
assertThat(response.status()).isEqualTo(200);
assertThat(response.reason()).isEqualTo("OK");
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("GET")
.hasPath("/?foo=bar&foo=baz");
}
@Test
public void testAlternativeCollectionFormat() throws Exception {
server.enqueue(new MockResponse().setBody("body"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.getCSV(Arrays.asList(new String[] {"bar", "baz"}));
assertThat(response.status()).isEqualTo(200);
assertThat(response.reason()).isEqualTo("OK");
// Some HTTP libraries percent-encode commas in query parameters and others don't.
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("GET")
.hasOneOfPath("/?foo=bar,baz", "/?foo=bar%2Cbaz");
}
public interface TestInterface {
@RequestLine("POST /?foo=bar&foo=baz&qux=")
@Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: text/plain"})
Response post(String body);
@RequestLine("POST /path/{to}/resource")
@Headers("Accept: text/plain")
Response post(@Param("to") String to, String body);
@RequestLine("GET /")
@Headers("Accept: text/plain")
String get();
@RequestLine("GET /?foo={multiFoo}")
Response get(@Param("multiFoo") List<String> multiFoo);
@RequestLine(value = "GET /?foo={multiFoo}", collectionFormat = CollectionFormat.CSV)
Response getCSV(@Param("multiFoo") List<String> multiFoo);
@RequestLine("PATCH /")
@Headers("Accept: text/plain")
String patch(String body);
@RequestLine("POST")
String noPostBody();
@RequestLine("PUT")
String noPutBody();
@RequestLine("POST /?foo=bar&foo=baz&qux=")
@Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: {contentType}"})
Response postWithContentType(String body, @Param("contentType") String contentType);
}
}