コード例 #1
0
  @Override
  public void logout() {
    final HttpUriRequest logoutGet = RequestBuilder.get().setUri(NYT_LOGOUT_URL).build();

    try (final CloseableHttpResponse getResponse = this.getHttpClient().execute(logoutGet)) {

      // successful NYT logout should give 200 status
      final int responseStatus = getResponse.getStatusLine().getStatusCode();
      if (responseStatus != 200) {
        final String errorMessage =
            String.format("did not detect expected 200, got %d instead", responseStatus);
        throw new LogoutException(errorMessage);
      }

      // successful NYT logout should delete a few cookies like this:
      // Set-Cookie: NYT-S=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/;
      // domain=.nytimes.com

      final Header[] cookies = getResponse.getHeaders("Set-Cookie");

      if (cookies.length < 1) {
        throw new LogoutException("no cookie deletions detected, logout might have failed");
      }

      final Stream<Header> cookieStream = Arrays.stream(cookies);
      final Predicate<Header> deletedCheck = c -> c.getValue().contains("deleted");
      if (!cookieStream.allMatch(deletedCheck)) {
        final List<Header> unexpectedCookies =
            cookieStream.filter(deletedCheck).collect(Collectors.toList());
        LOG.error("unexpected cookies={}", unexpectedCookies);
        throw new LogoutException("unexpected cookie(s) set, loguout might have failed");
      }

      LOG.info("successfully logged out of nyt");
    } catch (IOException | LogoutException e) {
      LOG.error("error while logging out of nyt, e={}", e);
    }
  }
コード例 #2
0
  @Test
  public void _03_map() {
    Stream<String> lowercaseWords = words.stream().map(String::toLowerCase);

    assertFalse("대문자를 포함하고 있으면 안됨.", lowercaseWords.allMatch(w -> w.matches("[A-Z]+")));
  }