Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 04e64b1

Browse files
author
alessandro.gherardi
committed
Fixed style and added test
1 parent 60d2330 commit 04e64b1

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

connectors/apache-connector/src/test/java/org/glassfish/jersey/apache/connector/AuthTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.apache.http.auth.AuthScope;
6565
import org.apache.http.auth.UsernamePasswordCredentials;
6666
import org.apache.http.client.CredentialsProvider;
67+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
6768
import org.junit.Ignore;
6869
import org.junit.Test;
6970
import static org.junit.Assert.assertEquals;
@@ -168,6 +169,38 @@ public String getFilter(@Context HttpHeaders h) {
168169
return "GET";
169170
}
170171

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+
171204
@POST
172205
public String post(@Context HttpHeaders h, String e) {
173206
requestCount++;
@@ -259,6 +292,37 @@ public void testAuthGetWithClientFilter() {
259292
assertEquals("GET", r.request().get(String.class));
260293
}
261294

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+
262326
@Test
263327
@Ignore("JERSEY-1750: Cannot retry request with a non-repeatable request entity. How to buffer the entity?"
264328
+ " Allow repeatable write in jersey?")

core-client/src/main/java/org/glassfish/jersey/client/authentication/HttpAuthenticationFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public void filter(ClientRequestContext request, ClientResponseContext response)
235235
}
236236

237237
if (result == null) {
238-
// unknown authentication -> this filter cannot authenticate with this method
238+
// unknown authentication -> this filter cannot authenticate with this method
239239
return;
240240
}
241241
}
@@ -334,8 +334,8 @@ static boolean repeatRequest(ClientRequestContext request, ClientResponseContext
334334

335335
// Copy other properties, if any, from the original request
336336
for (String propertyName : request.getPropertyNames()) {
337-
Object propertyValue = request.getProperty(propertyName);
338-
builder.property(propertyName, propertyValue);
337+
Object propertyValue = request.getProperty(propertyName);
338+
builder.property(propertyName, propertyValue);
339339
}
340340

341341
Invocation invocation;

0 commit comments

Comments
 (0)