Java Code Examples for javax.ws.rs.core.Response#getCookies()

The following examples show how to use javax.ws.rs.core.Response#getCookies() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void getCurrentUserTest() throws Exception {
    Response response = target().path("session").request().get();
    assertEquals(response.getStatus(), 200);
    verify(tokenManager).getTokenString(any(ContainerRequestContext.class));
    verify(tokenManager).verifyToken(TOKEN_STRING);
    verify(tokenManager, never()).generateUnauthToken();
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
    try {
        JSONObject result = JSONObject.fromObject(response.readEntity(String.class));
        assertEquals(removeWhitespace(VALID_USER), removeWhitespace(result.toString()));
    } catch (Exception e) {
        fail("Expected no exception, but got: " + e.getMessage());
    }
}
 
Example 2
@Test
public void getCurrentUserNotVerifiedTest() throws Exception {
    // Setup:
    when(tokenManager.getTokenString(any(ContainerRequestContext.class))).thenReturn(ERROR);

    Response response = target().path("session").request().get();
    assertEquals(response.getStatus(), 200);
    verify(tokenManager).getTokenString(any(ContainerRequestContext.class));
    verify(tokenManager).verifyToken(ERROR);
    verify(tokenManager).generateUnauthToken();
    Map<String, NewCookie> cookies = response.getCookies();
    assertTrue(cookies.containsKey(TOKEN_NAME));
    assertEquals(ANON, cookies.get(TOKEN_NAME).getValue());
    try {
        JSONObject result = JSONObject.fromObject(response.readEntity(String.class));
        assertEquals(removeWhitespace(ANON_USER), removeWhitespace(result.toString()));
    } catch (Exception e) {
        fail("Expected no exception, but got: " + e.getMessage());
    }
}
 
Example 3
@Test
public void loginAuthValidNoPrincipalsTest() throws Exception {
    // Setup:
    String authorization = USERNAME + ":" + PASSWORD;
    when(RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(USERNAME), eq(PASSWORD), eq(mobiConfiguration))).thenReturn(true);

    Response response = target().path("session").request()
            .header("Authorization", "Basic " + Base64.encode(authorization.getBytes())).post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verifyStatic();
    RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(USERNAME), eq(PASSWORD), eq(mobiConfiguration));
    verify(tokenManager, never()).generateAuthToken(anyString());
    verify(engineManager, times(0)).getUserRoles(anyString());
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 4
@Test
public void loginAuthValidNoRequiredRoleTest() throws Exception {
    // Setup:
    String authorization = USERNAME + ":" + PASSWORD;
    when(engineManager.getUserRoles(USERNAME)).thenReturn(Collections.singleton(otherRole));

    Response response = target().path("session").request()
            .header("Authorization", "Basic " + Base64.encode(authorization.getBytes())).post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verifyStatic();
    RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(USERNAME), eq(PASSWORD), eq(mobiConfiguration));
    verify(tokenManager, never()).generateAuthToken(anyString());
    verify(engineManager).getUserRoles(USERNAME);
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 5
@Test
public void loginAuthValidTest() throws Exception {
    // Setup:
    String authorization = USERNAME + ":" + PASSWORD;

    Response response = target().path("session").request()
            .header("Authorization", "Basic " + Base64.encode(authorization.getBytes())).post(Entity.json(""));
    assertEquals(response.getStatus(), 200);
    verifyStatic();
    RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(USERNAME), eq(PASSWORD), eq(mobiConfiguration));
    verify(tokenManager).generateAuthToken(USERNAME);
    verify(engineManager).getUserRoles(USERNAME);
    Map<String, NewCookie> cookies = response.getCookies();
    assertTrue(cookies.containsKey(TOKEN_NAME));
    assertEquals(USERNAME, cookies.get(TOKEN_NAME).getValue());
    try {
        JSONObject result = JSONObject.fromObject(response.readEntity(String.class));
        assertEquals(removeWhitespace(VALID_USER), removeWhitespace(result.toString()));
    } catch (Exception e) {
        fail("Expected no exception, but got: " + e.getMessage());
    }
}
 
Example 6
@Test
public void loginCredValidTest() throws Exception {
    Response response = target().path("session").queryParam("username", USERNAME).queryParam("password", PASSWORD).request().post(Entity.json(""));
    assertEquals(response.getStatus(), 200);
    verifyStatic();
    RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(USERNAME), eq(PASSWORD), eq(mobiConfiguration));
    verify(tokenManager).generateAuthToken(USERNAME);
    verify(engineManager).getUserRoles(USERNAME);
    Map<String, NewCookie> cookies = response.getCookies();
    assertTrue(cookies.containsKey(TOKEN_NAME));
    assertEquals(USERNAME, cookies.get(TOKEN_NAME).getValue());
    try {
        JSONObject result = JSONObject.fromObject(response.readEntity(String.class));
        assertEquals(removeWhitespace(VALID_USER), removeWhitespace(result.toString()));
    } catch (Exception e) {
        fail("Expected no exception, but got: " + e.getMessage());
    }
}
 
Example 7
@Test
public void loginNoCredsNoAuthTest() throws Exception {
    Response response = target().path("session").request().post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 8
@Test
public void loginAuthNoUsernameTest() throws Exception {
    // Setup:
    String authorization = ":" + PASSWORD;

    Response response = target().path("session").request()
            .header("Authorization", "Basic " + Base64.encode(authorization.getBytes())).post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verify(tokenManager, never()).generateAuthToken(anyString());
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 9
@Test
public void loginAuthNoPasswordTest() throws Exception {
    // Setup:
    String authorization = USERNAME + ":";

    Response response = target().path("session").request()
            .header("Authorization", "Basic " + Base64.encode(authorization.getBytes())).post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verify(tokenManager, never()).generateAuthToken(anyString());
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 10
@Test
public void loginAuthInvalidTest() throws Exception {
    // Setup:
    String authorization = ANON + ":" + ERROR;

    Response response = target().path("session").request()
            .header("Authorization", "Basic " + Base64.encode(authorization.getBytes())).post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verifyStatic();
    RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(ANON), eq(ERROR), eq(mobiConfiguration));
    verify(tokenManager, never()).generateAuthToken(anyString());
    verify(engineManager, times(0)).getUserRoles(anyString());
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 11
@Test
public void loginCredNoUsernameTest() throws Exception {
    // Setup:
    String authorization = ":" + PASSWORD;

    Response response = target().path("session").queryParam("password", PASSWORD).request().post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verify(tokenManager, never()).generateAuthToken(anyString());
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 12
@Test
public void loginCredNoPasswordTest() throws Exception {
    Response response = target().path("session").queryParam("username", USERNAME).request().post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verify(tokenManager, never()).generateAuthToken(anyString());
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 13
@Test
public void loginCredInvalidTest() throws Exception {
    Response response = target().path("session").queryParam("username", ANON).queryParam("password", ERROR).request().post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verifyStatic();
    RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(ANON), eq(ERROR), eq(mobiConfiguration));
    verify(tokenManager, never()).generateAuthToken(anyString());
    verify(engineManager, times(0)).getUserRoles(anyString());
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 14
@Test
public void loginCredValidNoPrincipalsTest() throws Exception {
    // Setup:
    when(RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(USERNAME), eq(PASSWORD), eq(mobiConfiguration))).thenReturn(true);

    Response response = target().path("session").queryParam("username", USERNAME).queryParam("password", PASSWORD).request().post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verifyStatic();
    RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(USERNAME), eq(PASSWORD), eq(mobiConfiguration));
    verify(tokenManager, never()).generateAuthToken(anyString());
    verify(engineManager, times(0)).getUserRoles(anyString());
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 15
@Test
public void loginCredValidNoRequiredRoleTest() throws Exception {
    // Setup:
    when(engineManager.getUserRoles(USERNAME)).thenReturn(Collections.singleton(otherRole));

    Response response = target().path("session").queryParam("username", USERNAME).queryParam("password", PASSWORD).request().post(Entity.json(""));
    assertEquals(response.getStatus(), 401);
    verifyStatic();
    RestSecurityUtils.authenticateUser(eq("mobi"), any(Subject.class), eq(USERNAME), eq(PASSWORD), eq(mobiConfiguration));
    verify(tokenManager, never()).generateAuthToken(anyString());
    verify(engineManager).getUserRoles(USERNAME);
    Map<String, NewCookie> cookies = response.getCookies();
    assertEquals(0, cookies.size());
}
 
Example 16
@Test
public void logoutTest() throws Exception {
    Response response = target().path("session").request().delete();
    assertEquals(response.getStatus(), 200);
    verify(tokenManager).generateUnauthToken();
    Map<String, NewCookie> cookies = response.getCookies();
    assertTrue(cookies.containsKey(TOKEN_NAME));
    assertEquals(ANON, cookies.get(TOKEN_NAME).getValue());
    try {
        JSONObject result = JSONObject.fromObject(response.readEntity(String.class));
        assertEquals(removeWhitespace(ANON_USER), removeWhitespace(result.toString()));
    } catch (Exception e) {
        fail("Expected no exception, but got: " + e.getMessage());
    }
}
 
Example 17
@Test
public void goodCredentialsSetsCookie() throws Exception {
  User user = User.named("goodUser");
  when(ldapAuthenticator.authenticate(goodCredentials)).thenReturn(Optional.of(user));

  Response response = sessionLoginResource.login(LoginRequest.from("good", "credentials".toCharArray()));
  assertThat(response.getStatus()).isEqualTo(200);

  Map<String, NewCookie> responseCookies = response.getCookies();
  assertThat(responseCookies).hasSize(1).containsOnlyKeys("session");

  User authUser = cookieAuthenticator.authenticate(responseCookies.get("session"))
      .orElseThrow(RuntimeException::new);
  assertThat(authUser).isEqualTo(user);
}