private Range getRange(final String[] values) throws MalformedInputException { try { return new Range(Long.parseLong(values[0].trim()), Long.parseLong(values[1].trim())); } catch (NumberFormatException e) { throw new MalformedInputException("Invalid range value, unable to parse numeric values " + e.getMessage()); } } }
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(expected = MalformedInputException.class) public void shouldThrowMalformedInputExceptionOnInvalidStatus() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); requestStatusParser.parse("GET HTTP/1.1"); } }
@Test(expected = ProtocolException.class) public void shouldThrowProtocolExceptionOnMalformedHeaders() throws Exception { when(headersParser.parse(any(String.class))).thenThrow(new MalformedInputException("ANY")); factory.createFromSocket(socket); }
@Test(expected = MalformedInputException.class) public void shouldThrowMalformedInputExceptionForMissingClosing() throws MalformedInputException { multipartHeadersPartParser.parse("Content-Disposition: form-data; name=\"text"); } }
@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(expected = MalformedInputException.class) public void shouldThrowMalformedInputException() throws MalformedInputException { multipartHeadersPartParser.parse("Content-Disposition: form-data; name=\"text\" filename=\"text"); }
@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 shouldParseMalformedEmptyValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(" ; "); assertThat(cookies.size(), is(0)); } }
@Test public void shouldTrimCookieNameValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(" name ="); assertThat(cookies, hasKey("name")); }
@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 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 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 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")); }
@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")); }