public void testSecurity() {
try {
startComponent();
String uri = "http://localhost:" + TEST_PORT + "/test1";
ClientResource resource = new ClientResource(uri);
// TEST SERIES 1
// Try without authentication
try {
resource.get();
} catch (ResourceException e) {
}
resource.release();
assertEquals(Status.CLIENT_ERROR_UNAUTHORIZED, resource.getStatus());
// Try with authentication
resource.setChallengeResponse(new ChallengeResponse(
ChallengeScheme.HTTP_BASIC, "stiger", "pwd"));
try {
resource.get();
} catch (ResourceException e) {
}
resource.release();
assertEquals(Status.SUCCESS_OK, resource.getStatus());
// TEST SERIES 2
uri = "http://localhost:" + TEST_PORT + "/test2";
resource = new ClientResource(uri);
try {
resource.get();
} catch (ResourceException e) {
}
resource.release();
assertEquals(Status.SUCCESS_OK, resource.getStatus());
// TEST SERIES 3
uri = "http://localhost:" + TEST_PORT + "/test3";
resource = new ClientResource(uri);
try {
resource.get();
} catch (ResourceException e) {
}
resource.release();
assertEquals(Status.CLIENT_ERROR_FORBIDDEN, resource.getStatus());
// TEST SERIES 4
uri = "http://localhost:" + TEST_PORT + "/test4";
resource = new ClientResource(uri);
resource.setChallengeResponse(new ChallengeResponse(
ChallengeScheme.HTTP_BASIC, "stiger", "pwd"));
try {
resource.get();
} catch (ResourceException e) {
}
resource.release();
assertEquals(Status.CLIENT_ERROR_FORBIDDEN, resource.getStatus());
// Try again with another user
resource.setChallengeResponse(new ChallengeResponse(
ChallengeScheme.HTTP_BASIC, "larmstrong", "pwd"));
try {
resource.get();
} catch (ResourceException e) {
}
resource.release();
assertEquals(Status.SUCCESS_OK, resource.getStatus());
// TEST SERIES 5
uri = "http://localhost:" + TEST_PORT + "/test5";
resource = new ClientResource(uri);
resource.setChallengeResponse(new ChallengeResponse(
ChallengeScheme.HTTP_BASIC, "stiger", "pwd"));
try {
resource.get();
} catch (ResourceException e) {
}
resource.release();
assertEquals(Status.SUCCESS_OK, resource.getStatus());
// Try again with another user
resource.setChallengeResponse(new ChallengeResponse(
ChallengeScheme.HTTP_BASIC, "larmstrong", "pwd"));
try {
resource.get();
} catch (ResourceException e) {
}
resource.release();
assertEquals(Status.CLIENT_ERROR_FORBIDDEN, resource.getStatus());
stopServer();
} catch (Exception e) {
e.printStackTrace();
}