RequestParser { void parseCookies(RequestImpl request, HttpServerExchange exchange) { HeaderValues cookieHeaders = exchange.getRequestHeaders().get(Headers.COOKIE); if (cookieHeaders != null) { try { request.cookies = decodeCookies(exchange.requestCookies()); } catch (IllegalArgumentException | IllegalStateException e) { logger.debug("[request:header] {}={}", Headers.COOKIE, new HeaderLogParam(Headers.COOKIE, cookieHeaders)); throw new BadRequestException("invalid cookie", "INVALID_COOKIE", e); } } }  void parse(RequestImpl request, HttpServerExchange exchange, ActionLog actionLog); final ClientIPParser clientIPParser; public boolean logSiteHeaders; }
@Test void parseCookies() { Map<String, String> cookies = parser.decodeCookies(Set.of(new CookieImpl("name", "value"))); assertThat(cookies).containsEntry("name", "value"); }
@Test void parseInvalidCookie() { var headers = new HeaderMap(); headers.put(Headers.COOKIE, "value"); var exchange = mock(HttpServerExchange.class); when(exchange.getRequestHeaders()).thenReturn(headers); when(exchange.requestCookies()) .thenThrow(new IllegalStateException("UT000046: The number of cookies sent exceeded the maximum of 200")) .thenThrow(new IllegalArgumentException("UT000069: Could not parse set cookie header value")); assertThatThrownBy(() -> parser.parseCookies(null, exchange)) .isInstanceOf(BadRequestException.class) .satisfies(e -> assertThat(((BadRequestException) e).errorCode()).isEqualTo("INVALID_COOKIE")); assertThatThrownBy(() -> parser.parseCookies(null, exchange)) .isInstanceOf(BadRequestException.class) .satisfies(e -> assertThat(((BadRequestException) e).errorCode()).isEqualTo("INVALID_COOKIE")); }