|
35 | 35 |
|
36 | 36 | import org.glassfish.jersey.client.ClientConfig; |
37 | 37 | import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; |
| 38 | +import org.glassfish.jersey.client.authentication.ResponseAuthenticationException; |
38 | 39 | import org.glassfish.jersey.server.ResourceConfig; |
39 | 40 | import org.glassfish.jersey.test.JerseyTest; |
40 | 41 |
|
|
47 | 48 | import static org.junit.Assert.assertEquals; |
48 | 49 | import static org.junit.Assert.assertNotNull; |
49 | 50 | import static org.junit.Assert.assertTrue; |
| 51 | +import static org.junit.Assert.fail; |
50 | 52 |
|
51 | 53 | /** |
52 | 54 | * @author Paul Sandoz |
@@ -248,6 +250,40 @@ public String deleteFilterWithEntity(@Context HttpHeaders h, String e) { |
248 | 250 | return e; |
249 | 251 | } |
250 | 252 |
|
| 253 | + @GET |
| 254 | + @Path("content") |
| 255 | + public String getWithContent(@Context HttpHeaders h) { |
| 256 | + requestCount++; |
| 257 | + String value = h.getRequestHeaders().getFirst("Authorization"); |
| 258 | + if (value == null) { |
| 259 | + assertEquals(1, requestCount); |
| 260 | + throw new WebApplicationException( |
| 261 | + Response.status(401).header("WWW-Authenticate", "Basic realm=\"WallyWorld\"") |
| 262 | + .entity("Forbidden").build()); |
| 263 | + } else { |
| 264 | + assertTrue(requestCount > 1); |
| 265 | + } |
| 266 | + |
| 267 | + return "GET"; |
| 268 | + } |
| 269 | + |
| 270 | + @GET |
| 271 | + @Path("contentDigestAuth") |
| 272 | + public String getWithContentDigestAuth(@Context HttpHeaders h) { |
| 273 | + requestCount++; |
| 274 | + String value = h.getRequestHeaders().getFirst("Authorization"); |
| 275 | + if (value == null) { |
| 276 | + assertEquals(1, requestCount); |
| 277 | + throw new WebApplicationException( |
| 278 | + Response.status(401).header("WWW-Authenticate", "Digest nonce=\"1234\"") |
| 279 | + .entity("Forbidden").build()); |
| 280 | + } else { |
| 281 | + assertTrue(requestCount > 1); |
| 282 | + } |
| 283 | + |
| 284 | + return "GET"; |
| 285 | + } |
| 286 | + |
251 | 287 | @GET |
252 | 288 | @Path("queryParamsBasic") |
253 | 289 | public String getQueryParamsBasic(@Context HttpHeaders h, @Context UriInfo uriDetails) { |
@@ -455,6 +491,52 @@ public void testAuthInteractivePost() { |
455 | 491 | assertEquals("POST", r.request().post(Entity.text("POST"), String.class)); |
456 | 492 | } |
457 | 493 |
|
| 494 | + @Test |
| 495 | + public void testAuthGetWithBasicFilterAndContent() { |
| 496 | + ClientConfig cc = new ClientConfig(); |
| 497 | + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); |
| 498 | + cc.connectorProvider(new ApacheConnectorProvider()); |
| 499 | + cc.property(ApacheClientProperties.CONNECTION_MANAGER, cm); |
| 500 | + Client client = ClientBuilder.newClient(cc); |
| 501 | + client.register(HttpAuthenticationFeature.universalBuilder().build()); |
| 502 | + WebTarget r = client.target(getBaseUri()).path("test/content"); |
| 503 | + |
| 504 | + try { |
| 505 | + assertEquals("GET", r.request().get(String.class)); |
| 506 | + fail(); |
| 507 | + } catch (ResponseAuthenticationException ex) { |
| 508 | + // expected |
| 509 | + } |
| 510 | + |
| 511 | + // Verify the connection that was used for the request is available for reuse |
| 512 | + // and no connections are leased |
| 513 | + assertEquals(cm.getTotalStats().getAvailable(), 1); |
| 514 | + assertEquals(cm.getTotalStats().getLeased(), 0); |
| 515 | + } |
| 516 | + |
| 517 | + @Test |
| 518 | + public void testAuthGetWithDigestFilterAndContent() { |
| 519 | + ClientConfig cc = new ClientConfig(); |
| 520 | + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); |
| 521 | + cc.connectorProvider(new ApacheConnectorProvider()); |
| 522 | + cc.property(ApacheClientProperties.CONNECTION_MANAGER, cm); |
| 523 | + Client client = ClientBuilder.newClient(cc); |
| 524 | + client.register(HttpAuthenticationFeature.universalBuilder().build()); |
| 525 | + WebTarget r = client.target(getBaseUri()).path("test/contentDigestAuth"); |
| 526 | + |
| 527 | + try { |
| 528 | + assertEquals("GET", r.request().get(String.class)); |
| 529 | + fail(); |
| 530 | + } catch (ResponseAuthenticationException ex) { |
| 531 | + // expected |
| 532 | + } |
| 533 | + |
| 534 | + // Verify the connection that was used for the request is available for reuse |
| 535 | + // and no connections are leased |
| 536 | + assertEquals(cm.getTotalStats().getAvailable(), 1); |
| 537 | + assertEquals(cm.getTotalStats().getLeased(), 0); |
| 538 | + } |
| 539 | + |
458 | 540 | @Test |
459 | 541 | public void testAuthGetQueryParamsBasic() { |
460 | 542 | ClientConfig cc = new ClientConfig(); |
|
0 commit comments