Search in sources :

Example 46 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testSmallBlockWithGZIPTrailerChunked.

@Test
public void testSmallBlockWithGZIPTrailerChunked() throws Exception {
    String data = "0";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes = baos.toByteArray();
    // The trailer is 4+4 bytes, chunk the last 3 bytes
    byte[] bytes1 = new byte[bytes.length - 3];
    System.arraycopy(bytes, 0, bytes1, 0, bytes1.length);
    byte[] bytes2 = new byte[bytes.length - bytes1.length];
    System.arraycopy(bytes, bytes1.length, bytes2, 0, bytes2.length);
    GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048);
    ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1));
    assertEquals(0, decoded.capacity());
    decoder.release(decoded);
    decoded = decoder.decode(ByteBuffer.wrap(bytes2));
    assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString());
    decoder.release(decoded);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 47 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testBigBlockOneByteAtATime.

@Test
public void testBigBlockOneByteAtATime() throws Exception {
    String data = "0123456789ABCDEF";
    for (int i = 0; i < 10; ++i) data += data;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes = baos.toByteArray();
    String result = "";
    GZIPContentDecoder decoder = new GZIPContentDecoder(64);
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    while (buffer.hasRemaining()) {
        ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(new byte[] { buffer.get() }));
        if (decoded.hasRemaining())
            result += StandardCharsets.UTF_8.decode(decoded).toString();
        decoder.release(decoded);
    }
    assertEquals(data, result);
    assertTrue(decoder.isFinished());
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 48 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class HttpGeneratorServerTest method testResponseWithKnownContentLengthFromMetaData.

@Test
public void testResponseWithKnownContentLengthFromMetaData() throws Exception {
    ByteBuffer header = BufferUtil.allocate(4096);
    ByteBuffer content0 = BufferUtil.toBuffer("Hello World! ");
    ByteBuffer content1 = BufferUtil.toBuffer("The quick brown fox jumped over the lazy dog. ");
    HttpGenerator gen = new HttpGenerator();
    HttpGenerator.Result result = gen.generateResponse(null, false, null, null, content0, false);
    assertEquals(HttpGenerator.Result.NEED_INFO, result);
    assertEquals(HttpGenerator.State.START, gen.getState());
    MetaData.Response info = new MetaData.Response(HttpVersion.HTTP_1_1, 200, null, new HttpFields(), 59);
    info.getFields().add("Last-Modified", DateGenerator.__01Jan1970);
    result = gen.generateResponse(info, false, null, null, content0, false);
    assertEquals(HttpGenerator.Result.NEED_HEADER, result);
    assertEquals(HttpGenerator.State.START, gen.getState());
    result = gen.generateResponse(info, false, header, null, content0, false);
    assertEquals(HttpGenerator.Result.FLUSH, result);
    assertEquals(HttpGenerator.State.COMMITTED, gen.getState());
    String out = BufferUtil.toString(header);
    BufferUtil.clear(header);
    out += BufferUtil.toString(content0);
    BufferUtil.clear(content0);
    result = gen.generateResponse(null, false, null, null, content1, false);
    assertEquals(HttpGenerator.Result.FLUSH, result);
    assertEquals(HttpGenerator.State.COMMITTED, gen.getState());
    out += BufferUtil.toString(content1);
    BufferUtil.clear(content1);
    result = gen.generateResponse(null, false, null, null, null, true);
    assertEquals(HttpGenerator.Result.CONTINUE, result);
    assertEquals(HttpGenerator.State.COMPLETING, gen.getState());
    result = gen.generateResponse(null, false, null, null, null, true);
    assertEquals(HttpGenerator.Result.DONE, result);
    assertEquals(HttpGenerator.State.END, gen.getState());
    assertThat(out, containsString("HTTP/1.1 200 OK"));
    assertThat(out, containsString("Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT"));
    assertThat(out, not(containsString("chunked")));
    assertThat(out, containsString("Content-Length: 59"));
    assertThat(out, containsString("\r\n\r\nHello World! The quick brown fox jumped over the lazy dog. "));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 49 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class HttpGeneratorServerTest method testSendServerXPoweredBy.

@Test
public void testSendServerXPoweredBy() throws Exception {
    ByteBuffer header = BufferUtil.allocate(8096);
    MetaData.Response info = new MetaData.Response(HttpVersion.HTTP_1_1, 200, null, new HttpFields(), -1);
    HttpFields fields = new HttpFields();
    fields.add(HttpHeader.SERVER, "SomeServer");
    fields.add(HttpHeader.X_POWERED_BY, "SomePower");
    MetaData.Response infoF = new MetaData.Response(HttpVersion.HTTP_1_1, 200, null, fields, -1);
    String head;
    HttpGenerator gen = new HttpGenerator(true, true);
    gen.generateResponse(info, false, header, null, null, true);
    head = BufferUtil.toString(header);
    BufferUtil.clear(header);
    assertThat(head, containsString("HTTP/1.1 200 OK"));
    assertThat(head, containsString("Server: Jetty(9.x.x)"));
    assertThat(head, containsString("X-Powered-By: Jetty(9.x.x)"));
    gen.reset();
    gen.generateResponse(infoF, false, header, null, null, true);
    head = BufferUtil.toString(header);
    BufferUtil.clear(header);
    assertThat(head, containsString("HTTP/1.1 200 OK"));
    assertThat(head, not(containsString("Server: Jetty(9.x.x)")));
    assertThat(head, containsString("Server: SomeServer"));
    assertThat(head, containsString("X-Powered-By: Jetty(9.x.x)"));
    assertThat(head, containsString("X-Powered-By: SomePower"));
    gen.reset();
    gen = new HttpGenerator(false, false);
    gen.generateResponse(info, false, header, null, null, true);
    head = BufferUtil.toString(header);
    BufferUtil.clear(header);
    assertThat(head, containsString("HTTP/1.1 200 OK"));
    assertThat(head, not(containsString("Server: Jetty(9.x.x)")));
    assertThat(head, not(containsString("X-Powered-By: Jetty(9.x.x)")));
    gen.reset();
    gen.generateResponse(infoF, false, header, null, null, true);
    head = BufferUtil.toString(header);
    BufferUtil.clear(header);
    assertThat(head, containsString("HTTP/1.1 200 OK"));
    assertThat(head, not(containsString("Server: Jetty(9.x.x)")));
    assertThat(head, containsString("Server: SomeServer"));
    assertThat(head, not(containsString("X-Powered-By: Jetty(9.x.x)")));
    assertThat(head, containsString("X-Powered-By: SomePower"));
    gen.reset();
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 50 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class HttpGeneratorServerTest method testResponseWithKnownContentLengthFromHeader.

@Test
public void testResponseWithKnownContentLengthFromHeader() throws Exception {
    ByteBuffer header = BufferUtil.allocate(4096);
    ByteBuffer content0 = BufferUtil.toBuffer("Hello World! ");
    ByteBuffer content1 = BufferUtil.toBuffer("The quick brown fox jumped over the lazy dog. ");
    HttpGenerator gen = new HttpGenerator();
    HttpGenerator.Result result = gen.generateResponse(null, false, null, null, content0, false);
    assertEquals(HttpGenerator.Result.NEED_INFO, result);
    assertEquals(HttpGenerator.State.START, gen.getState());
    MetaData.Response info = new MetaData.Response(HttpVersion.HTTP_1_1, 200, null, new HttpFields(), -1);
    info.getFields().add("Last-Modified", DateGenerator.__01Jan1970);
    info.getFields().add("Content-Length", "" + (content0.remaining() + content1.remaining()));
    result = gen.generateResponse(info, false, null, null, content0, false);
    assertEquals(HttpGenerator.Result.NEED_HEADER, result);
    assertEquals(HttpGenerator.State.START, gen.getState());
    result = gen.generateResponse(info, false, header, null, content0, false);
    assertEquals(HttpGenerator.Result.FLUSH, result);
    assertEquals(HttpGenerator.State.COMMITTED, gen.getState());
    String out = BufferUtil.toString(header);
    BufferUtil.clear(header);
    out += BufferUtil.toString(content0);
    BufferUtil.clear(content0);
    result = gen.generateResponse(null, false, null, null, content1, false);
    assertEquals(HttpGenerator.Result.FLUSH, result);
    assertEquals(HttpGenerator.State.COMMITTED, gen.getState());
    out += BufferUtil.toString(content1);
    BufferUtil.clear(content1);
    result = gen.generateResponse(null, false, null, null, null, true);
    assertEquals(HttpGenerator.Result.CONTINUE, result);
    assertEquals(HttpGenerator.State.COMPLETING, gen.getState());
    result = gen.generateResponse(null, false, null, null, null, true);
    assertEquals(HttpGenerator.Result.DONE, result);
    assertEquals(HttpGenerator.State.END, gen.getState());
    assertThat(out, containsString("HTTP/1.1 200 OK"));
    assertThat(out, containsString("Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT"));
    assertThat(out, not(containsString("chunked")));
    assertThat(out, containsString("Content-Length: 59"));
    assertThat(out, containsString("\r\n\r\nHello World! The quick brown fox jumped over the lazy dog. "));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)597324 ArrayList (java.util.ArrayList)32672 QuickTest (com.hazelcast.test.annotation.QuickTest)27910 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)25464 File (java.io.File)24462 HashMap (java.util.HashMap)21030 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)19416 List (java.util.List)16717 IOException (java.io.IOException)13352 Map (java.util.Map)9238 Date (java.util.Date)9014 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8368 InputStream (java.io.InputStream)7753 Properties (java.util.Properties)7445 CountDownLatch (java.util.concurrent.CountDownLatch)7403 ByteArrayInputStream (java.io.ByteArrayInputStream)7401 HashSet (java.util.HashSet)7165 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6707 Response (javax.ws.rs.core.Response)6154 Ignore (org.junit.Ignore)5936