|
64 | 64 | import org.apache.http.auth.AuthScope;
|
65 | 65 | import org.apache.http.auth.UsernamePasswordCredentials;
|
66 | 66 | import org.apache.http.client.CredentialsProvider;
|
| 67 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
67 | 68 | import org.junit.Ignore;
|
68 | 69 | import org.junit.Test;
|
69 | 70 | import static org.junit.Assert.assertEquals;
|
@@ -168,6 +169,38 @@ public String getFilter(@Context HttpHeaders h) {
|
168 | 169 | return "GET";
|
169 | 170 | }
|
170 | 171 |
|
| 172 | + @GET |
| 173 | + @Path("digest") |
| 174 | + public String getDigest(@Context HttpHeaders h) { |
| 175 | + String value = h.getRequestHeaders().getFirst("Authorization"); |
| 176 | + if (value == null) { |
| 177 | + throw new WebApplicationException( |
| 178 | + Response.status(401).header("WWW-Authenticate", "Digest realm=\"WallyWorld\"") |
| 179 | + .entity("Forbidden").build()); |
| 180 | + } |
| 181 | + |
| 182 | + return "GET"; |
| 183 | + } |
| 184 | + |
| 185 | + @GET |
| 186 | + @Path("basicAndDigest") |
| 187 | + public String getBasicAndDigest(@Context HttpHeaders h) { |
| 188 | + String value = h.getRequestHeaders().getFirst("Authorization"); |
| 189 | + if (value == null) { |
| 190 | + throw new WebApplicationException( |
| 191 | + Response.status(401).header("WWW-Authenticate", "Basic realm=\"WallyWorld\"") |
| 192 | + .header("WWW-Authenticate", "Digest realm=\"WallyWorld\"") |
| 193 | + .entity("Forbidden").build()); |
| 194 | + } else if (value.startsWith("Basic")) { |
| 195 | + throw new WebApplicationException( |
| 196 | + Response.status(401).header("WWW-Authenticate", "Basic realm=\"WallyWorld\"") |
| 197 | + .header("WWW-Authenticate", "Digest realm=\"WallyWorld\"") |
| 198 | + .entity("Digest authentication expected").build()); |
| 199 | + } |
| 200 | + |
| 201 | + return "GET"; |
| 202 | + } |
| 203 | + |
171 | 204 | @POST
|
172 | 205 | public String post(@Context HttpHeaders h, String e) {
|
173 | 206 | requestCount++;
|
@@ -259,6 +292,37 @@ public void testAuthGetWithClientFilter() {
|
259 | 292 | assertEquals("GET", r.request().get(String.class));
|
260 | 293 | }
|
261 | 294 |
|
| 295 | + @Test |
| 296 | + public void testAuthGetWithDigestFilter() { |
| 297 | + ClientConfig cc = new ClientConfig(); |
| 298 | + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); |
| 299 | + cc.connectorProvider(new ApacheConnectorProvider()); |
| 300 | + cc.property(ApacheClientProperties.CONNECTION_MANAGER, cm); |
| 301 | + Client client = ClientBuilder.newClient(cc); |
| 302 | + client.register(HttpAuthenticationFeature.universal("name", "password")); |
| 303 | + WebTarget r = client.target(getBaseUri()).path("test/digest"); |
| 304 | + |
| 305 | + assertEquals("GET", r.request().get(String.class)); |
| 306 | + |
| 307 | + // Verify the connection that was used for the request is available for reuse |
| 308 | + // and no connections are leased |
| 309 | + assertEquals(cm.getTotalStats().getAvailable(), 1); |
| 310 | + assertEquals(cm.getTotalStats().getLeased(), 0); |
| 311 | + } |
| 312 | + |
| 313 | + @Test |
| 314 | + public void testAuthGetWithBasicAndDigestFilter() { |
| 315 | + ClientConfig cc = new ClientConfig(); |
| 316 | + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); |
| 317 | + cc.connectorProvider(new ApacheConnectorProvider()); |
| 318 | + cc.property(ApacheClientProperties.CONNECTION_MANAGER, cm); |
| 319 | + Client client = ClientBuilder.newClient(cc); |
| 320 | + client.register(HttpAuthenticationFeature.universal("name", "password")); |
| 321 | + WebTarget r = client.target(getBaseUri()).path("test/basicAndDigest"); |
| 322 | + |
| 323 | + assertEquals("GET", r.request().get(String.class)); |
| 324 | + } |
| 325 | + |
262 | 326 | @Test
|
263 | 327 | @Ignore("JERSEY-1750: Cannot retry request with a non-repeatable request entity. How to buffer the entity?"
|
264 | 328 | + " Allow repeatable write in jersey?")
|
|
0 commit comments