getCookies()源码实例Demo

javax.ws.rs.core.Response#getCookies()源码实例Demo

下面列出了javax.ws.rs.core.Response#getCookies() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: mobi   文件: AuthRestTest.java
@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());
    }
}
 
源代码2 项目: mobi   文件: AuthRestTest.java
@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());
    }
}
 
源代码3 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码4 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码5 项目: mobi   文件: AuthRestTest.java
@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());
    }
}
 
源代码6 项目: mobi   文件: AuthRestTest.java
@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());
    }
}
 
源代码7 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码8 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码9 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码10 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码11 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码12 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码13 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码14 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码15 项目: mobi   文件: AuthRestTest.java
@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());
}
 
源代码16 项目: mobi   文件: AuthRestTest.java
@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());
    }
}
 
源代码17 项目: keywhiz   文件: SessionLoginResourceTest.java
@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);
}