Search in sources :

Example 81 with Test

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

the class HttpParserTest method testParseRequest.

@Test
public void testParseRequest() throws Exception {
    ByteBuffer buffer = BufferUtil.toBuffer("GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Header1: value1\r\n" + "Connection: close\r\n" + "Accept-Encoding: gzip, deflated\r\n" + "Accept: unknown\r\n" + "\r\n");
    HttpParser.RequestHandler handler = new Handler();
    HttpParser parser = new HttpParser(handler);
    parser.parseNext(buffer);
    Assert.assertEquals("GET", _methodOrVersion);
    Assert.assertEquals("/", _uriOrStatus);
    Assert.assertEquals("HTTP/1.1", _versionOrReason);
    Assert.assertEquals("Host", _hdr[0]);
    Assert.assertEquals("localhost", _val[0]);
    Assert.assertEquals("Connection", _hdr[2]);
    Assert.assertEquals("close", _val[2]);
    Assert.assertEquals("Accept-Encoding", _hdr[3]);
    Assert.assertEquals("gzip, deflated", _val[3]);
    Assert.assertEquals("Accept", _hdr[4]);
    Assert.assertEquals("unknown", _val[4]);
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 82 with Test

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

the class HttpParserTest method testHostPort.

@Test
public void testHostPort() throws Exception {
    ByteBuffer buffer = BufferUtil.toBuffer("GET / HTTP/1.1\r\n" + "Host: myhost:8888\r\n" + "Connection: close\r\n" + "\r\n");
    HttpParser.RequestHandler handler = new Handler();
    HttpParser parser = new HttpParser(handler);
    parser.parseNext(buffer);
    Assert.assertEquals("myhost", _host);
    Assert.assertEquals(8888, _port);
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 83 with Test

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

the class HttpParserTest method testBadRequestVersion.

@Test
public void testBadRequestVersion() throws Exception {
    ByteBuffer buffer = BufferUtil.toBuffer("GET / HPPT/7.7\r\n" + "Content-Length: 0\r\n" + "Connection: close\r\n" + "\r\n");
    HttpParser.RequestHandler handler = new Handler();
    HttpParser parser = new HttpParser(handler);
    parser.parseNext(buffer);
    Assert.assertEquals(null, _methodOrVersion);
    Assert.assertEquals("Unknown Version", _bad);
    Assert.assertFalse(buffer.hasRemaining());
    Assert.assertEquals(HttpParser.State.CLOSE, parser.getState());
    parser.atEOF();
    parser.parseNext(BufferUtil.EMPTY_BUFFER);
    Assert.assertEquals(HttpParser.State.CLOSED, parser.getState());
    buffer = BufferUtil.toBuffer("GET / HTTP/1.01\r\n" + "Content-Length: 0\r\n" + "Connection: close\r\n" + "\r\n");
    handler = new Handler();
    parser = new HttpParser(handler);
    parser.parseNext(buffer);
    Assert.assertEquals(null, _methodOrVersion);
    Assert.assertEquals("Unknown Version", _bad);
    Assert.assertFalse(buffer.hasRemaining());
    Assert.assertEquals(HttpParser.State.CLOSE, parser.getState());
    parser.atEOF();
    parser.parseNext(BufferUtil.EMPTY_BUFFER);
    Assert.assertEquals(HttpParser.State.CLOSED, parser.getState());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 84 with Test

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

the class HTTP2CServerTest method testHTTP_2_0_DirectWithoutH2C.

@Test
public void testHTTP_2_0_DirectWithoutH2C() throws Exception {
    AtomicLong fills = new AtomicLong();
    // Remove "h2c", leaving only "http/1.1".
    connector.clearConnectionFactories();
    HttpConnectionFactory connectionFactory = new HttpConnectionFactory() {

        @Override
        public Connection newConnection(Connector connector, EndPoint endPoint) {
            HttpConnection connection = new HttpConnection(getHttpConfiguration(), connector, endPoint, getHttpCompliance(), isRecordHttpComplianceViolations()) {

                @Override
                public void onFillable() {
                    fills.incrementAndGet();
                    super.onFillable();
                }
            };
            return configure(connection, connector, endPoint);
        }
    };
    connector.addConnectionFactory(connectionFactory);
    connector.setDefaultProtocol(connectionFactory.getProtocol());
    // Now send a HTTP/2 direct request, which
    // will have the PRI * HTTP/2.0 preface.
    byteBufferPool = new MappedByteBufferPool();
    generator = new Generator(byteBufferPool);
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        // We sent a HTTP/2 preface, but the server has no "h2c" connection
        // factory so it does not know how to handle this request.
        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        String responseLine = reader.readLine();
        Assert.assertThat(responseLine, Matchers.containsString(" 426 "));
        while (true) {
            if (reader.read() < 0)
                break;
        }
    }
    // Make sure we did not spin.
    Thread.sleep(1000);
    Assert.assertThat(fills.get(), Matchers.lessThan(5L));
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) InputStreamReader(java.io.InputStreamReader) HttpConnection(org.eclipse.jetty.server.HttpConnection) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) EndPoint(org.eclipse.jetty.io.EndPoint) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) AtomicLong(java.util.concurrent.atomic.AtomicLong) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 85 with Test

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

the class HTTP2CServerTest method testHTTP_1_1_Simple.

@Test
public void testHTTP_1_1_Simple() throws Exception {
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        client.getOutputStream().write("GET /one HTTP/1.1\r\nHost: localhost\r\n\r\n".getBytes(StandardCharsets.ISO_8859_1));
        client.getOutputStream().write("GET /two HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".getBytes(StandardCharsets.ISO_8859_1));
        client.getOutputStream().flush();
        String response = IO.toString(client.getInputStream());
        assertThat(response, containsString("HTTP/1.1 200 OK"));
        assertThat(response, containsString("Hello from Jetty using HTTP/1.1"));
        assertThat(response, containsString("uri=/one"));
        assertThat(response, containsString("uri=/two"));
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) 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