HttpServletRequestImplFactory { public HttpServletRequestImpl createFromSocket(final Socket socket) throws IOException, ProtocolException { HttpServletRequestImpl.Builder builder = HttpServletRequestImpl.createNewBuilder(); InputStream in = socket.getInputStream(); RequestStatus status; try { status = statusParser.parse(getStatusLine(in)); } catch (MalformedInputException e) { throw new MalformedStatusLineException("Malformed status line " + e.getMessage()); } int uriLengthExceededWith = status.getUri().length() - URI_MAX_LENGTH; if (uriLengthExceededWith > 0) { throw new UriTooLongProtocolException("Uri length exceeded max length with" + uriLengthExceededWith + " characters"); } if (!isValidProtocol(status.getProtocol())) { throw new UnsupportedProtocolException("Protocol " + status.getProtocol() + " is not supported"); } assignSocketMetadata(socket, builder); try { builder.withGetParameters(queryStringParser.parse(status.getQueryString())); } catch (MalformedInputException e) { } Headers headers = computeHeaders(in); builder.withServletContext(getImplicitServletContext()) .withInputStream(in) .withStatus(status) .withPathTranslated(status.getUri()) .withPathInfo("") .withRemoteUser(null) .withPrincipal(null) .withHeaders(headers) .withCookies(getCookies(headers)); if (status.getMethod().equalsIgnoreCase(HttpServletRequest.METHOD_POST)) { try { handlePostRequest(builder, in, headers); } catch (MalformedInputException e) { throw new ProtocolException("Malformed post input"); } } return builder.build(); } HttpServletRequestImplFactory(final Parser<Headers> headersParser,
                                         final Parser<Map<String, String>> queryStringParser,
                                         final Parser<RequestStatus> statusParser,
                                         final Parser<Map<String, Cookie>> cookieParser,
                                         final Parser<MultipartHeadersPart> multipartHeadersPartParser,
                                         final String tempPath); HttpServletRequestImpl createFromSocket(final Socket socket);  }
@Test(expected = ProtocolException.class) public void shouldThrowProtocolExceptionOnMalformedHeaders() throws Exception { when(headersParser.parse(any(String.class))).thenThrow(new MalformedInputException("ANY")); factory.createFromSocket(socket); }
@Test(expected = UnsupportedProtocolException.class) public void shouldThrowUnsuportedProtocolExceptionIllegalProtocol() throws Exception { when(socket.getInputStream()) .thenReturn(new ByteArrayInputStream("GET / MALF/1.0\r\nHeader1: someValue\r\n\r\n".getBytes())); factory.createFromSocket(socket); }