@Test public void testBasics() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new IdentityOutputStream(outbuffer, outputStream); final byte[] tmp = new byte[10]; out.write(tmp, 0, 10); out.write(tmp); out.write(1); out.flush(); out.close(); final byte[] data = outputStream.toByteArray(); Assert.assertEquals(21, data.length); }
@Test public void testBasics() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new ContentLengthOutputStream(outbuffer, outputStream, 15L); final byte[] tmp = new byte[10]; out.write(tmp, 0, 10); out.write(1); out.write(tmp, 0, 10); out.write(tmp, 0, 10); out.write(tmp); out.write(1); out.write(2); out.flush(); out.close(); final byte[] data = outputStream.toByteArray(); Assert.assertEquals(15, data.length); }
@Test(expected=CharacterCodingException.class) public void testUnmappableInputActionReport() throws Exception { final String s = "This text contains a circumflex \u0302 !!!"; final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder(); encoder.onMalformedInput(CodingErrorAction.IGNORE); encoder.onUnmappableCharacter(CodingErrorAction.REPORT); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final CharArrayBuffer chbuffer = new CharArrayBuffer(32); chbuffer.append(s); outbuffer.writeLine(chbuffer, outputStream); }
@Test public void testClose() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new ContentLengthOutputStream(outbuffer, outputStream, 15L); out.close(); out.close(); final byte[] tmp = new byte[10]; try { out.write(tmp); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } try { out.write(1); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } }
@Test public void testClosedCondition() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new IdentityOutputStream(outbuffer, outputStream); out.close(); out.close(); try { final byte[] tmp = new byte[2]; out.write(tmp, 0, tmp.length); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } try { out.write('a'); Assert.fail("StreamClosedException expected"); } catch (final IOException expected) { } }
@Test public void testUnmappableInputActionReplace() throws Exception { final String s = "This text contains a circumflex \u0302 !!!"; final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder(); encoder.onMalformedInput(CodingErrorAction.IGNORE); encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final CharArrayBuffer chbuffer = new CharArrayBuffer(32); chbuffer.append(s); outbuffer.writeLine(chbuffer, outputStream); outbuffer.flush(outputStream); final String result = new String(outputStream.toByteArray(), "ISO-8859-1"); Assert.assertEquals("This text contains a circumflex ? !!!\r\n", result); }
@Test public void testBasicWrite() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new IdentityOutputStream(outbuffer, outputStream); out.write(new byte[] {'a', 'b'}, 0, 2); out.write('c'); out.flush(); final byte[] input = outputStream.toByteArray(); Assert.assertNotNull(input); final byte[] expected = new byte[] {'a', 'b', 'c'}; Assert.assertEquals(expected.length, input.length); for (int i = 0; i < expected.length; i++) { Assert.assertEquals(expected[i], input[i]); } out.close(); }
@Test public void testUnmappableInputActionIgnore() throws Exception { final String s = "This text contains a circumflex \u0302 !!!"; final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder(); encoder.onMalformedInput(CodingErrorAction.IGNORE); encoder.onUnmappableCharacter(CodingErrorAction.IGNORE); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final CharArrayBuffer chbuffer = new CharArrayBuffer(32); chbuffer.append(s); outbuffer.writeLine(chbuffer, outputStream); outbuffer.flush(outputStream); final String result = new String(outputStream.toByteArray(), "ISO-8859-1"); Assert.assertEquals("This text contains a circumflex !!!\r\n", result); }
@Test public void testClose() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new IdentityOutputStream(outbuffer, outputStream); out.close(); out.close(); final byte[] tmp = new byte[10]; try { out.write(tmp); Assert.fail("IOException should have been thrown"); } catch (final IOException ex) { // expected } try { out.write(1); Assert.fail("IOException should have been thrown"); } catch (final IOException ex) { // expected } }
@Test public void testWriteSmallFragmentNoBuffering() throws Exception { final ByteArrayOutputStream outputStream = Mockito.spy(new ByteArrayOutputStream()); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(new BasicHttpTransportMetrics(), 16, 0, null); outbuffer.write(1, outputStream); outbuffer.write(2, outputStream); outbuffer.write(new byte[] {1, 2}, outputStream); outbuffer.write(new byte[]{3, 4}, outputStream); Mockito.verify(outputStream, Mockito.times(2)).write(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); Mockito.verify(outputStream, Mockito.times(2)).write(ArgumentMatchers.anyInt()); }
@Test public void testWriteSmallFragmentBuffering() throws Exception { final ByteArrayOutputStream outputStream = Mockito.spy(new ByteArrayOutputStream()); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(new BasicHttpTransportMetrics(), 16, 16, null); outbuffer.write(1, outputStream); outbuffer.write(2, outputStream); outbuffer.write(new byte[] {1, 2}, outputStream); outbuffer.write(new byte[]{3, 4}, outputStream); outbuffer.flush(outputStream); Mockito.verify(outputStream, Mockito.times(1)).write(ArgumentMatchers.<byte[]>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); Mockito.verify(outputStream, Mockito.never()).write(ArgumentMatchers.anyInt()); }
@Test public void testChunkedOutputStreamSmallChunk() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2); out.write('1'); out.finish(); out.close(); final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII); Assert.assertEquals("1\r\n1\r\n0\r\n\r\n", content); }
BHttpConnectionBase( final H1Config h1Config, final CharsetDecoder charDecoder, final CharsetEncoder charEncoder) { this.h1Config = h1Config != null ? h1Config : H1Config.DEFAULT; final BasicHttpTransportMetrics inTransportMetrics = new BasicHttpTransportMetrics(); final BasicHttpTransportMetrics outTransportMetrics = new BasicHttpTransportMetrics(); this.inBuffer = new SessionInputBufferImpl(inTransportMetrics, this.h1Config.getBufferSize(), -1, this.h1Config.getMaxLineLength(), charDecoder); this.outbuffer = new SessionOutputBufferImpl(outTransportMetrics, this.h1Config.getBufferSize(), this.h1Config.getChunkSizeHint(), charEncoder); this.connMetrics = new BasicHttpConnectionMetrics(inTransportMetrics, outTransportMetrics); this.socketHolderRef = new AtomicReference<>(); }
BHttpConnectionBase( final H1Config h1Config, final CharsetDecoder charDecoder, final CharsetEncoder charEncoder) { this.h1Config = h1Config != null ? h1Config : H1Config.DEFAULT; final BasicHttpTransportMetrics inTransportMetrics = new BasicHttpTransportMetrics(); final BasicHttpTransportMetrics outTransportMetrics = new BasicHttpTransportMetrics(); this.inBuffer = new SessionInputBufferImpl(inTransportMetrics, this.h1Config.getBufferSize(), -1, this.h1Config.getMaxLineLength(), charDecoder); this.outbuffer = new SessionOutputBufferImpl(outTransportMetrics, this.h1Config.getBufferSize(), this.h1Config.getChunkSizeHint(), charEncoder); this.connMetrics = new BasicHttpConnectionMetrics(inTransportMetrics, outTransportMetrics); this.socketHolderRef = new AtomicReference<>(); }
@Test public void testChunkedOutputStreamLargeChunk() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2); out.write(new byte[] {'1', '2', '3', '4'}); out.finish(); out.close(); final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII); Assert.assertEquals("4\r\n1234\r\n0\r\n\r\n", content); }
@Test public void testChunkedOutputStreamWithTrailers() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2, new Supplier<List<? extends Header>>() { @Override public List<? extends Header> get() { return Arrays.asList( new BasicHeader("E", ""), new BasicHeader("Y", "Z")); } } ); out.write('x'); out.finish(); out.close(); final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII); Assert.assertEquals("1\r\nx\r\n0\r\nE: \r\nY: Z\r\n\r\n", content); }
@Test public void testMultibyteCodedReadWriteLongLine() throws Exception { final String s1 = constructString(SWISS_GERMAN_HELLO); final String s2 = constructString(RUSSIAN_HELLO); final String s3 = "Like hello and stuff"; final StringBuilder buf = new StringBuilder(); for (int i = 0; i < 1024; i++) { buf.append(s1).append(s2).append(s3); } final String s = buf.toString(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.UTF_8.newEncoder()); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final CharArrayBuffer chbuffer = new CharArrayBuffer(16); chbuffer.append(s); outbuffer.writeLine(chbuffer, outputStream); outbuffer.flush(outputStream); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder()); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); chbuffer.clear(); inBuffer.readLine(chbuffer, inputStream); Assert.assertEquals(s, chbuffer.toString()); }
@Test public void testChunkedOutputStream() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2); out.write('1'); out.write('2'); out.write('3'); out.write('4'); out.finish(); out.close(); final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII); Assert.assertEquals("2\r\n12\r\n2\r\n34\r\n0\r\n\r\n", content); }
@Test public void testChunkedOutputStreamClose() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048); out.close(); out.close(); try { out.write(new byte[] {1,2,3}); Assert.fail("IOException should have been thrown"); } catch (final IOException ex) { // expected } try { out.write(1); Assert.fail("IOException should have been thrown"); } catch (final IOException ex) { // expected } }
@Test public void testBasicBufferProperties() throws Exception { final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1, 2 , 3}); Assert.assertEquals(16, inBuffer.capacity()); Assert.assertEquals(16, inBuffer.available()); Assert.assertEquals(0, inBuffer.length()); inBuffer.read(inputStream); Assert.assertEquals(14, inBuffer.available()); Assert.assertEquals(2, inBuffer.length()); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Assert.assertEquals(16, outbuffer.capacity()); Assert.assertEquals(16, outbuffer.available()); Assert.assertEquals(0, outbuffer.length()); outbuffer.write(new byte[] {1, 2, 3}, outputStream); Assert.assertEquals(13, outbuffer.available()); Assert.assertEquals(3, outbuffer.length()); }