String s = "HTTP/1.1 200 OK\r\n" + "Content-Length: 100\r\n" + "Content-Type: text/plain\r\n" + "Server: some-server\r\n" + "\r\n"; SessionInputBufferImpl sessionInputBuffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 2048); sessionInputBuffer.bind(new ByteArrayInputStream(s.getBytes(Consts.ASCII))); DefaultHttpResponseParser responseParser = new DefaultHttpResponseParser(sessionInputBuffer); HttpResponse response = responseParser.parse(); System.out.println(response);
@Test(expected = NoHttpResponseException.class) public void testConnectionClosedException() throws Exception { final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {}); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(); parser.parse(inBuffer, inputStream); }
@Test(expected = MessageConstraintException.class) public void testBasicMessageParsingTooManyLeadingEmptyLines() throws Exception { final String s = "\r\n" + "\r\n" + "\r\n" + "HTTP/1.1 200 OK\r\n" + "Server: whatever\r\n" + "\r\n"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); final DefaultHttpResponseParser parser = new DefaultHttpResponseParser( H1Config.custom().setMaxEmptyLineCount(3).build()); parser.parse(inBuffer, inputStream); }
@Test public void testBasicMessageParsing() throws Exception { final String s = "HTTP/1.1 200 OK\r\n" + "Server: whatever\r\n" + "Date: some date\r\n" + "Set-Cookie: c1=stuff\r\n" + "\r\n"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(); final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream); Assert.assertEquals(200, httpresponse.getCode()); Assert.assertEquals("OK", httpresponse.getReasonPhrase()); final Header[] headers = httpresponse.getHeaders(); Assert.assertEquals(3, headers.length); }
@Test public void testBasicMessageParsingLeadingEmptyLines() throws Exception { final String s = "\r\n" + "\r\n" + "HTTP/1.1 200 OK\r\n" + "Server: whatever\r\n" + "\r\n"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); final DefaultHttpResponseParser parser = new DefaultHttpResponseParser( H1Config.custom().setMaxEmptyLineCount(3).build()); final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream); Assert.assertEquals(200, httpresponse.getCode()); Assert.assertEquals("OK", httpresponse.getReasonPhrase()); final Header[] headers = httpresponse.getHeaders(); Assert.assertEquals(1, headers.length); }
@Test public void testMessageParsingTimeout() throws Exception { final String s = "HTTP\000/1.1 200\000 OK\r\n" + "Server: wha\000tever\r\n" + "Date: some date\r\n" + "Set-Coo\000kie: c1=stuff\r\n" + "\000\r\n"; final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(); int timeoutCount = 0; ClassicHttpResponse httpresponse = null; for (int i = 0; i < 10; i++) { try { httpresponse = parser.parse(inBuffer, inputStream); break; } catch (final InterruptedIOException ex) { timeoutCount++; } } Assert.assertNotNull(httpresponse); Assert.assertEquals(5, timeoutCount); Assert.assertEquals(200, httpresponse.getCode()); Assert.assertEquals("OK", httpresponse.getReasonPhrase()); final Header[] headers = httpresponse.getHeaders(); Assert.assertEquals(3, headers.length); }