Search in sources :

Example 1 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class HttpFieldsTest method testCachedPut.

@Test
public void testCachedPut() throws Exception {
    HttpFields header = new HttpFields();
    header.put("Connection", "Keep-Alive");
    header.put("tRansfer-EncOding", "CHUNKED");
    header.put("CONTENT-ENCODING", "gZIP");
    ByteBuffer buffer = BufferUtil.allocate(1024);
    BufferUtil.flipToFill(buffer);
    HttpGenerator.putTo(header, buffer);
    BufferUtil.flipToFlush(buffer, 0);
    String out = BufferUtil.toString(buffer).toLowerCase(Locale.ENGLISH);
    Assert.assertThat(out, Matchers.containsString((HttpHeader.CONNECTION + ": " + HttpHeaderValue.KEEP_ALIVE).toLowerCase(Locale.ENGLISH)));
    Assert.assertThat(out, Matchers.containsString((HttpHeader.TRANSFER_ENCODING + ": " + HttpHeaderValue.CHUNKED).toLowerCase(Locale.ENGLISH)));
    Assert.assertThat(out, Matchers.containsString((HttpHeader.CONTENT_ENCODING + ": " + HttpHeaderValue.GZIP).toLowerCase(Locale.ENGLISH)));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with Matchers.containsString

use of org.hamcrest.Matchers.containsString 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 3 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class HttpServerTestBase method testInterruptedRequest.

@Test
public void testInterruptedRequest() throws Exception {
    final AtomicBoolean fourBytesRead = new AtomicBoolean(false);
    final AtomicBoolean earlyEOFException = new AtomicBoolean(false);
    configureServer(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            int contentLength = request.getContentLength();
            ServletInputStream inputStream = request.getInputStream();
            for (int i = 0; i < contentLength; i++) {
                try {
                    inputStream.read();
                } catch (EofException e) {
                    earlyEOFException.set(true);
                    throw new QuietServletException(e);
                }
                if (i == 3)
                    fourBytesRead.set(true);
            }
        }
    });
    StringBuffer request = new StringBuffer("GET / HTTP/1.0\n");
    request.append("Host: localhost\n");
    request.append("Content-length: 6\n\n");
    request.append("foo");
    Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
    OutputStream os = client.getOutputStream();
    os.write(request.toString().getBytes());
    os.flush();
    client.shutdownOutput();
    String response = readResponse(client);
    client.close();
    assertThat("response contains 500", response, Matchers.containsString(" 500 "));
    assertThat("The 4th byte (-1) has not been passed to the handler", fourBytesRead.get(), is(false));
    assertThat("EofException has been caught", earlyEOFException.get(), is(true));
}
Also used : EofException(org.eclipse.jetty.io.EofException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletInputStream(javax.servlet.ServletInputStream) Socket(java.net.Socket) Test(org.junit.Test)

Example 4 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class HttpServerTestBase method testFullURI.

/*
    * Feed a full header method
    */
@Test
public void testFullURI() throws Exception {
    configureServer(new HelloWorldHandler());
    try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
        StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
        ((AbstractLogger) Log.getLogger(HttpConnection.class)).info("expect URI is too large, then ISE extra data ...");
        OutputStream os = client.getOutputStream();
        byte[] buffer = new byte[64 * 1024];
        buffer[0] = 'G';
        buffer[1] = 'E';
        buffer[2] = 'T';
        buffer[3] = ' ';
        buffer[4] = '/';
        Arrays.fill(buffer, 5, buffer.length - 1, (byte) 'A');
        os.write(buffer);
        os.flush();
        // Read the response.
        String response = readResponse(client);
        Assert.assertThat(response, Matchers.containsString("HTTP/1.1 414 "));
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) AbstractLogger(org.eclipse.jetty.util.log.AbstractLogger) Test(org.junit.Test)

Example 5 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class HttpServerTestBase method testExceptionThrownInHandler.

@Test
public void testExceptionThrownInHandler() throws Exception {
    configureServer(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            throw new QuietServletException("TEST handler exception");
        }
    });
    StringBuffer request = new StringBuffer("GET / HTTP/1.0\r\n");
    request.append("Host: localhost\r\n\r\n");
    Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
    OutputStream os = client.getOutputStream();
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        Log.getLogger(HttpChannel.class).info("Expecting ServletException: TEST handler exception...");
        os.write(request.toString().getBytes());
        os.flush();
        String response = readResponse(client);
        assertThat(response, Matchers.containsString(" 500 "));
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

Matchers.containsString (org.hamcrest.Matchers.containsString)35 Test (org.junit.Test)32 Socket (java.net.Socket)12 OutputStream (java.io.OutputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ServletOutputStream (javax.servlet.ServletOutputStream)9 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)4 File (java.io.File)3 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)3 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)3 AbstractLogger (org.eclipse.jetty.util.log.AbstractLogger)3 Matchers (org.hamcrest.Matchers)3 Assert.assertEquals (org.junit.Assert.assertEquals)3