private Map<String, Cookie> getCookies(final Headers headers) { if (headers.containsHeader(Headers.HEADER_COOKIE)) { try { return cookieParser.parse(headers.getHeader(Headers.HEADER_COOKIE)); } catch (MalformedInputException e) { // Returns an empty map } } return new HashMap<>(); }
@Test public void shouldReturnZeroSizeForInvalidValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse("name"); assertThat(cookies.size(), is(0)); }
@Test public void shouldParseEmptyValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(""); assertThat(cookies.size(), is(0)); }
@Test public void shouldReturnZeroSizeForInvalidKey() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(" = value"); assertThat(cookies.size(), is(0)); }
@Test public void shouldAssignNoCookieOnMalformedCookieString() throws Exception { headers.setHeader(Headers.HEADER_COOKIE, "ANYTHING"); when(cookieParser.parse(any(String.class))).thenThrow(new MalformedInputException("ANY")); HttpServletRequestImpl request = factory.createFromSocket(socket); assertThat(request.getCookies().length, is(0)); verify(cookieParser, times(1)).parse(any(String.class)); }
@Test public void shouldParseMalformedEmptyValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(" ; "); assertThat(cookies.size(), is(0)); } }
@Test(expected = ProtocolException.class) public void shouldThrowProtocolExceptionOnMalformedHeaders() throws Exception { when(headersParser.parse(any(String.class))).thenThrow(new MalformedInputException("ANY")); factory.createFromSocket(socket); }
@Test public void shouldParseFormDataText() throws MalformedInputException { MultipartHeadersPart headers = multipartHeadersPartParser.parse("Content-Disposition: form-data; name=\"text\""); assertThat(headers.getFileName(), is(nullValue())); assertThat(headers.getName(), is("text")); assertThat(headers.getContentType(), is(nullValue())); }
@Test public void shouldParseColonValue() throws MalformedInputException { Headers headers = headersParser.parse("Cookie: ABCD:XYZ"); assertThat(headers.containsHeader("Cookie"), is(true)); assertThat(headers.getHeader("Cookie"), is("ABCD:XYZ")); }
@Test public void shouldTrimCookieNameValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(" name ="); assertThat(cookies, hasKey("name")); }
@Test public void shouldParseValidAttachmentHeader() throws MalformedInputException { MultipartHeadersPart headers = multipartHeadersPartParser.parse("Content-Disposition: attachment; name=\"FIELDNAME\"; filename=\"FILE.PDF\"\nContent-type: application/pdf"); assertThat(headers.getFileName(), is("FILE.PDF")); assertThat(headers.getName(), is("FIELDNAME")); assertThat(headers.getContentType(), is("application/pdf")); }
@Test public void shouldParseCookieHavingUrlEncodedValue() throws MalformedInputException { String value = "&<>some value"; Map<String, Cookie> cookies = cookieParser.parse("name=" + StringUtilities.urlEncode(value)); assertThat(cookies, hasKey("name")); assertThat(cookies.get("name").getValue(), is(value)); }
@Test public void shouldParseHeadersWithMissingValues() throws MalformedInputException { Headers headers = headersParser.parse("Cookie\r\nTest\r\nServer: Pepis"); assertThat(headers.containsHeader("Cookie"), is(false)); assertThat(headers.containsHeader("Server"), is(true)); assertThat(headers.getHeader("Server"), is("Pepis")); }
@Test public void shouldParseMultilineHeaderWithSingleLeadingTab() throws MalformedInputException { Headers headers = headersParser.parse("Word-Of-The-Day: The Fox Jumps Over\r\n\tthe\r\n\t brown dog.\r\nCookie: ABCD"); assertThat(headers.containsHeader("Word-Of-The-Day"), is(true)); assertThat(headers.getHeader("Word-Of-The-Day"), is("The Fox Jumps Over the brown dog.")); assertThat(headers.containsHeader("Cookie"), is(true)); assertThat(headers.getHeader("Cookie"), is("ABCD")); }
@Test public void shouldParseAndLtrimHeaders() throws MalformedInputException { Headers headers = headersParser.parse("Cookie: ABCD\r\nTest: XYZ\r\nServer: 1"); assertThat(headers.containsHeader("Cookie"), is(true)); assertThat(headers.containsHeader("Test"), is(true)); assertThat(headers.containsHeader("Server"), is(true)); assertThat(headers.getHeader("Cookie"), is("ABCD")); assertThat(headers.getHeader("Test"), is("XYZ")); assertThat(headers.getHeader("Server"), is("1")); assertThat(headers.getHeader("Non-existent"), is(nullValue())); }
@Test public void shouldIgnoreTrailingCharacters() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); RequestStatus requestStatus = requestStatusParser.parse("GET /home?param1=ABC¶m2=123 HTTP/1.1\r\n"); assertThat(requestStatus.getMethod(), is("GET")); assertThat(requestStatus.getQueryString(), is("param1=ABC¶m2=123")); assertThat(requestStatus.getUri(), is("/home")); assertThat(requestStatus.getProtocol(), is("HTTP/1.1")); }
@Test public void shouldParseWithoutLeadingSpace() throws MalformedInputException { Headers headers = headersParser.parse("COOKIE:ABCD\r\nTEST:XYZ\r\nSERVER:1"); assertThat(headers.containsHeader("Cookie"), is(true)); assertThat(headers.containsHeader("Test"), is(true)); assertThat(headers.containsHeader("Server"), is(true)); assertThat(headers.getHeader("Cookie"), is("ABCD")); assertThat(headers.getHeader("Test"), is("XYZ")); assertThat(headers.getHeader("Server"), is("1")); }
@Test public void shouldParseMultilineHeaderWithSingleLeadingSpace() throws MalformedInputException { Headers headers = headersParser.parse("Word-Of-The-Day: The Fox Jumps Over\r\n the\r\n brown dog.\r\nAnother: Another\r\n multiline\r\n header\r\nCookie: ABCD"); assertThat(headers.containsHeader("Word-Of-The-Day"), is(true)); assertThat(headers.containsHeader("Another"), is(true)); assertThat(headers.getHeader("Word-Of-The-Day"), is("The Fox Jumps Over the brown dog.")); assertThat(headers.getHeader("Another"), is("Another multiline header")); assertThat(headers.containsHeader("Cookie"), is(true)); assertThat(headers.getHeader("Cookie"), is("ABCD")); }
@Test public void shouldParseStatusString() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); RequestStatus requestStatus = requestStatusParser.parse("GET /home?param1=ABC¶m2=123 HTTP/1.1"); assertThat(requestStatus.getMethod(), is("GET")); assertThat(requestStatus.getQueryString(), is("param1=ABC¶m2=123")); assertThat(requestStatus.getUri(), is("/home")); assertThat(requestStatus.getProtocol(), is("HTTP/1.1")); }
@Test public void shouldIgnoreMultipleEmptyNewlines() throws MalformedInputException { Headers headers = headersParser.parse("Cookie: ABCD\r\n\r\n\r\nTest: XYZ\r\nServer: 1\r\n\r\n\r\n\r\n"); assertThat(headers.containsHeader("Cookie"), is(true)); assertThat(headers.containsHeader("Test"), is(true)); assertThat(headers.containsHeader("Server"), is(true)); assertThat(headers.getHeader("Cookie"), is("ABCD")); assertThat(headers.getHeader("Test"), is("XYZ")); assertThat(headers.getHeader("Server"), is("1")); }