/
JerseyClientTest.java
321 lines (262 loc) · 10.3 KB
/
JerseyClientTest.java
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
/*
* Copyright (c) 2011, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.client;
import java.io.IOException;
import java.util.concurrent.Future;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;
import javax.inject.Inject;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.internal.Version;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* {@link JerseyClient} unit test.
*
* @author Marek Potociar
*/
public class JerseyClientTest {
private JerseyClient client;
public JerseyClientTest() {
}
@Before
public void setUp() {
this.client = (JerseyClient) ClientBuilder.newClient();
}
@After
public void tearDown() {
}
@Test
public void testCreateClient() {
assertNotNull(client);
}
@Test
public void testClose() {
client.close();
assertTrue(client.isClosed());
client.close(); // closing multiple times is ok
try {
client.getConfiguration();
fail("IllegalStateException expected if a method is called on a closed client instance.");
} catch (IllegalStateException ex) {
// ignored
}
try {
client.target("http://jersey.java.net/examples");
fail("IllegalStateException expected if a method is called on a closed client instance.");
} catch (IllegalStateException ex) {
// ignored
}
}
@Test
public void testConfiguration() {
final ClientConfig configuration = client.getConfiguration();
assertNotNull(configuration);
configuration.property("hello", "world");
assertEquals("world", client.getConfiguration().getProperty("hello"));
}
@Test
public void testTarget() {
final JerseyWebTarget target = client.target("http://jersey.java.net/examples");
assertNotNull(target);
assertEquals(client.getConfiguration(), target.getConfiguration());
}
@Test(expected = IllegalArgumentException.class)
public void testTargetIAE() {
final UriBuilder uriBuilder = UriBuilder.fromUri(":xxx:8080//yyy:90090//jaxrs ");
}
public static class TestProvider implements ReaderInterceptor {
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
return context.proceed();
}
}
// Reproducer JERSEY-1637
@Test
public void testRegisterNullOrEmptyContracts() {
final TestProvider provider = new TestProvider();
client.register(TestProvider.class, (Class<?>[]) null);
assertFalse(client.getConfiguration().isRegistered(TestProvider.class));
client.register(provider, (Class<?>[]) null);
assertFalse(client.getConfiguration().isRegistered(TestProvider.class));
assertFalse(client.getConfiguration().isRegistered(provider));
client.register(TestProvider.class, new Class[0]);
assertFalse(client.getConfiguration().isRegistered(TestProvider.class));
client.register(provider, new Class[0]);
assertFalse(client.getConfiguration().isRegistered(TestProvider.class));
assertFalse(client.getConfiguration().isRegistered(provider));
}
@Test
public void testTargetConfigUpdate() {
final JerseyWebTarget target = client.target("http://jersey.java.net/examples");
target.getConfiguration().register(new ClientRequestFilter() {
@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {
throw new UnsupportedOperationException("Not supported yet");
}
});
assertEquals(1, target.getConfiguration().getInstances().size());
}
/**
* Regression test for JERSEY-1192.
*/
@Test
public void testCreateLinkBasedInvocation() {
final JerseyClient jerseyClient = new JerseyClient();
try {
jerseyClient.invocation(null);
fail("NullPointerException expected.");
} catch (NullPointerException ex) {
// success.
}
try {
jerseyClient.invocation(null);
fail("NullPointerException expected.");
} catch (NullPointerException ex) {
// success.
}
Link link1 =
Link.fromUri(UriBuilder.fromPath("http://localhost:8080/").build())
.build();
Link link2 =
Link.fromUri(UriBuilder.fromPath("http://localhost:8080/").build())
.type("text/plain")
.build();
assertNotNull(jerseyClient.invocation(link1).buildPost(null));
assertNotNull(jerseyClient.invocation(link2).buildPost(null));
assertNotNull(jerseyClient.invocation(link1).buildPost(Entity.text("Test.")));
assertNotNull(jerseyClient.invocation(link2).buildPost(Entity.text("Test.")));
assertNotNull(jerseyClient.invocation(link1).buildPost(Entity.xml("Test.")));
assertNotNull(jerseyClient.invocation(link2).buildPost(Entity.xml("Test.")));
}
@Test
public void userAgentTest() {
final Client customClient = ClientBuilder.newClient(new ClientConfig().connectorProvider(new TestConnector()));
try {
customClient.target("test").request().get();
} catch (ProcessingException e) {
assertEquals("Jersey/" + Version.getVersion(), e.getMessage());
}
try {
customClient.target("test").request().async().get().get();
} catch (Exception e) {
assertEquals("Jersey/" + Version.getVersion(), e.getCause().getMessage());
}
}
/**
* JERSEY-2189 reproducer.
*/
@Test
public void customUserAgentTest() {
final Client customClient = ClientBuilder.newClient(new ClientConfig().connectorProvider(new TestConnector()));
try {
customClient.target("test").request().header(HttpHeaders.USER_AGENT, null).get();
} catch (Exception e) {
assertEquals("[null]", e.getMessage());
}
try {
customClient.target("test").request().header(HttpHeaders.USER_AGENT, null).async().get();
} catch (Exception e) {
assertEquals("[null]", e.getCause().getMessage());
}
try {
customClient.target("test").request().header(HttpHeaders.USER_AGENT, "custom").get();
} catch (Exception e) {
assertEquals("custom", e.getMessage());
}
try {
customClient.target("test").request().header(HttpHeaders.USER_AGENT, "custom").async().get();
} catch (Exception e) {
assertEquals("custom", e.getCause().getMessage());
}
}
public static interface CustomContract {
public String getFoo();
}
public static class CustomService implements CustomContract {
@Override
public String getFoo() {
return "Foo";
}
}
public static class CustomBinder extends AbstractBinder {
@Override
protected void configure() {
bind(CustomService.class).to(CustomContract.class);
}
}
public static class CustomProvider implements ClientRequestFilter {
@Inject
private CustomContract customContract;
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
requestContext.abortWith(Response.ok(customContract.getFoo()).build());
}
}
@Test
public void testCustomBinders() {
final CustomBinder binder = new CustomBinder();
Client client = ClientBuilder.newClient().register(binder).register(CustomProvider.class);
Response resp = client.target("test").request().get();
assertEquals("Foo", resp.readEntity(String.class));
}
private static class TestConnector implements Connector, ConnectorProvider {
@Override
public ClientResponse apply(ClientRequest request) throws ProcessingException {
final Object agent = request.getHeaders().getFirst(HttpHeaders.USER_AGENT);
throw new ProcessingException((agent == null) ? "[null]" : agent.toString());
}
@Override
public Future<?> apply(ClientRequest request, AsyncConnectorCallback callback) {
final Object agent = request.getHeaders().getFirst(HttpHeaders.USER_AGENT);
callback.failure(new ProcessingException((agent == null) ? "[null]" : agent.toString()));
return null;
}
@Override
public void close() {
// nothing
}
@Override
public String getName() {
return null;
}
@Override
public Connector getConnector(Client client, Configuration runtimeConfig) {
return this;
}
}
}