RequestStatusParser implements Parser<RequestStatus> { @Override public RequestStatus parse(final String input) throws MalformedInputException { RequestStatus status = new RequestStatus(); status.setQueryString(""); String uri; String[] statusArray = input.split(STATUS_SEPARATOR, NUMBER_OF_CHUNKS); if (statusArray.length < NUMBER_OF_CHUNKS) { throw new MalformedInputException("Input status string should be composed out of " + NUMBER_OF_CHUNKS + " chunks. Received " + input); } status.setMethod(statusArray[0].toUpperCase()); uri = statusArray[1]; status.setProtocol(statusArray[2].trim()); int questionMarkPosition = uri.indexOf(QUERY_STRING_START); if (questionMarkPosition > -1) { status.setQueryString(uri.substring(questionMarkPosition + 1)); uri = uri.substring(0, questionMarkPosition); } status.setUri(uri); return status; }  @Override RequestStatus parse(final String input);  }
@Test public void shouldParseStatusString() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); RequestStatus requestStatus = requestStatusParser.parse("GET /home?param1=ABC&param2=123 HTTP/1.1"); assertThat(requestStatus.getMethod(), is("GET")); assertThat(requestStatus.getQueryString(), is("param1=ABC&param2=123")); assertThat(requestStatus.getUri(), is("/home")); assertThat(requestStatus.getProtocol(), is("HTTP/1.1")); }
@Test public void shouldIgnoreTrailingCharacters() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); RequestStatus requestStatus = requestStatusParser.parse("GET /home?param1=ABC&param2=123 HTTP/1.1\r\n"); assertThat(requestStatus.getMethod(), is("GET")); assertThat(requestStatus.getQueryString(), is("param1=ABC&param2=123")); assertThat(requestStatus.getUri(), is("/home")); assertThat(requestStatus.getProtocol(), is("HTTP/1.1")); }
@Test(expected = MalformedInputException.class) public void shouldThrowMalformedInputExceptionOnInvalidStatus() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); requestStatusParser.parse("GET HTTP/1.1"); }