forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeignTest.java
More file actions
203 lines (169 loc) · 7.21 KB
/
FeignTest.java
File metadata and controls
203 lines (169 loc) · 7.21 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
/*
* 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.ImmutableMap;
import com.google.mockwebserver.MockResponse;
import com.google.mockwebserver.MockWebServer;
import com.google.mockwebserver.SocketPolicy;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.Map;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.net.ssl.SSLSocketFactory;
import dagger.Module;
import dagger.Provides;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.codec.ToStringDecoder;
import static org.testng.Assert.assertEquals;
@Test
public class FeignTest {
interface TestInterface {
@RequestLine("POST /") String post();
@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);
@RequestLine("GET /{1}/{2}") Response uriParam(@Named("1") String one, URI endpoint, @Named("2") String two);
@dagger.Module(overrides = true, library = true)
static class Module {
// until dagger supports real map binding, we need to recreate the
// entire map, as opposed to overriding a single entry.
@Provides @Singleton Map<String, Decoder> decoders() {
return ImmutableMap.<String, Decoder>of("TestInterface", new ToStringDecoder());
}
}
}
@Test
public void postTemplateParamsResolve() throws IOException, InterruptedException {
final MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(200).setBody("foo"));
server.play();
try {
TestInterface api = Feign.create(TestInterface.class, server.getUrl("").toString(), new TestInterface.Module());
api.login("netflix", "denominator", "password");
assertEquals(new String(server.takeRequest().getBody()),
"{\"customer_name\": \"netflix\", \"user_name\": \"denominator\", \"password\": \"password\"}");
} finally {
server.shutdown();
}
}
@Test public void toKeyMethodFormatsAsExpected() throws Exception {
assertEquals(Feign.configKey(TestInterface.class.getDeclaredMethod("post")), "TestInterface#post()");
assertEquals(Feign.configKey(TestInterface.class.getDeclaredMethod("uriParam", String.class, URI.class,
String.class)), "TestInterface#uriParam(String,URI,String)");
}
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "zone not found")
public void canOverrideErrorDecoderOnMethod() throws IOException, InterruptedException {
@dagger.Module(overrides = true, includes = TestInterface.Module.class) class Overrides {
@Provides @Singleton Map<String, ErrorDecoder> decoders() {
return ImmutableMap.<String, ErrorDecoder>of("TestInterface#post()", new ErrorDecoder() {
@Override
public Object decode(String methodKey, Response response, Type type) throws Throwable {
if (response.status() == 404)
throw new IllegalArgumentException("zone not found");
return ErrorDecoder.DEFAULT.decode(methodKey, response, type);
}
});
}
}
final MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(404).setBody("foo"));
server.play();
try {
TestInterface api = Feign.create(TestInterface.class, server.getUrl("").toString(), new Overrides());
api.post();
} finally {
server.shutdown();
}
}
@Test public void retriesLostConnectionBeforeRead() throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
server.enqueue(new MockResponse().setResponseCode(200).setBody("success!".getBytes()));
server.play();
try {
TestInterface api = Feign.create(TestInterface.class, server.getUrl("").toString(),
new TestInterface.Module());
api.post();
assertEquals(server.getRequestCount(), 2);
} finally {
server.shutdown();
}
}
@Test(expectedExceptions = FeignException.class, expectedExceptionsMessageRegExp = "error reading response POST http://.*")
public void doesntRetryAfterResponseIsSent() throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(200).setBody("success!".getBytes()));
server.play();
try {
@dagger.Module(overrides = true) class Overrides {
@Provides @Singleton Map<String, Decoder> decoders() {
return ImmutableMap.<String, Decoder>of("TestInterface", new Decoder() {
@Override
public Object decode(String methodKey, Reader reader, Type type) throws Throwable {
throw new IOException("error reading response");
}
});
}
}
TestInterface api = Feign.create(TestInterface.class, server.getUrl("").toString(), new Overrides());
api.post();
} finally {
server.shutdown();
assertEquals(server.getRequestCount(), 1);
}
}
@Module(injects = Client.Default.class, overrides = true)
static class TrustSSLSockets {
@Provides SSLSocketFactory trustingSSLSocketFactory() {
return TrustingSSLSocketFactory.get();
}
}
@Test public void canOverrideSSLSocketFactory() throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
server.useHttps(TrustingSSLSocketFactory.get(), false);
server.enqueue(new MockResponse().setResponseCode(200).setBody("success!".getBytes()));
server.play();
try {
TestInterface api = Feign.create(TestInterface.class, server.getUrl("").toString(),
new TestInterface.Module(), new TrustSSLSockets());
api.post();
} finally {
server.shutdown();
}
}
@Test public void retriesFailedHandshake() throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
server.useHttps(TrustingSSLSocketFactory.get(), false);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
server.enqueue(new MockResponse().setResponseCode(200).setBody("success!".getBytes()));
server.play();
try {
TestInterface api = Feign.create(TestInterface.class, server.getUrl("").toString(),
new TestInterface.Module(), new TrustSSLSockets());
api.post();
assertEquals(server.getRequestCount(), 2);
} finally {
server.shutdown();
}
}
}