Search in sources :

Example 11 with Matchers.containsString

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

the class HttpServerTestBase method testFullHeader.

/*
     * Feed a full header method
     */
@Test
public void testFullHeader() throws Exception {
    configureServer(new HelloWorldHandler());
    try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
        StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
        Log.getLogger(HttpConnection.class).info("expect header 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] = '/';
        buffer[5] = ' ';
        buffer[6] = 'H';
        buffer[7] = 'T';
        buffer[8] = 'T';
        buffer[9] = 'P';
        buffer[10] = '/';
        buffer[11] = '1';
        buffer[12] = '.';
        buffer[13] = '0';
        buffer[14] = '\n';
        buffer[15] = 'H';
        buffer[16] = ':';
        Arrays.fill(buffer, 17, 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 431 "));
    }
}
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) Test(org.junit.Test)

Example 12 with Matchers.containsString

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

the class SniSslConnectionFactoryTest method testSocketCustomization.

@Test
public void testSocketCustomization() throws Exception {
    final Queue<String> history = new LinkedBlockingQueue<>();
    _connector.addBean(new SocketCustomizationListener() {

        @Override
        protected void customize(Socket socket, Class<? extends Connection> connection, boolean ssl) {
            history.add("customize connector " + connection + "," + ssl);
        }
    });
    _connector.getBean(SslConnectionFactory.class).addBean(new SocketCustomizationListener() {

        @Override
        protected void customize(Socket socket, Class<? extends Connection> connection, boolean ssl) {
            history.add("customize ssl " + connection + "," + ssl);
        }
    });
    _connector.getBean(HttpConnectionFactory.class).addBean(new SocketCustomizationListener() {

        @Override
        protected void customize(Socket socket, Class<? extends Connection> connection, boolean ssl) {
            history.add("customize http " + connection + "," + ssl);
        }
    });
    String response = getResponse("127.0.0.1", null);
    Assert.assertThat(response, Matchers.containsString("X-HOST: 127.0.0.1"));
    Assert.assertEquals("customize connector class org.eclipse.jetty.io.ssl.SslConnection,false", history.poll());
    Assert.assertEquals("customize ssl class org.eclipse.jetty.io.ssl.SslConnection,false", history.poll());
    Assert.assertEquals("customize connector class org.eclipse.jetty.server.HttpConnection,true", history.poll());
    Assert.assertEquals("customize http class org.eclipse.jetty.server.HttpConnection,true", history.poll());
    Assert.assertEquals(0, history.size());
}
Also used : HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) SocketCustomizationListener(org.eclipse.jetty.server.SocketCustomizationListener) Matchers.containsString(org.hamcrest.Matchers.containsString) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) Test(org.junit.Test)

Example 13 with Matchers.containsString

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

the class SniSslConnectionFactoryTest method testConnect.

@Test
public void testConnect() throws Exception {
    String response = getResponse("127.0.0.1", null);
    Assert.assertThat(response, Matchers.containsString("X-HOST: 127.0.0.1"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 14 with Matchers.containsString

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

the class SniSslConnectionFactoryTest method testSameConnectionRequestsForManyWildDomains.

@Test
public void testSameConnectionRequestsForManyWildDomains() throws Exception {
    SslContextFactory clientContextFactory = new SslContextFactory(true);
    clientContextFactory.start();
    SSLSocketFactory factory = clientContextFactory.getSslContext().getSocketFactory();
    try (SSLSocket sslSocket = (SSLSocket) factory.createSocket("127.0.0.1", _port)) {
        SNIHostName serverName = new SNIHostName("www.domain.com");
        SSLParameters params = sslSocket.getSSLParameters();
        params.setServerNames(Collections.singletonList(serverName));
        sslSocket.setSSLParameters(params);
        sslSocket.startHandshake();
        String request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: www.domain.com\r\n" + "\r\n";
        OutputStream output = sslSocket.getOutputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = sslSocket.getInputStream();
        String response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 200 "));
        // Now, on the same socket, send a request for a different valid domain.
        request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: assets.domain.com\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 200 "));
        // Now make a request for an invalid domain for this connection.
        request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 400 "));
        Assert.assertThat(response, Matchers.containsString("Host does not match SNI"));
    } finally {
        clientContextFactory.stop();
    }
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SSLParameters(javax.net.ssl.SSLParameters) SNIHostName(javax.net.ssl.SNIHostName) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 15 with Matchers.containsString

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

the class SniSslConnectionFactoryTest method testSNIConnect.

@Test
public void testSNIConnect() throws Exception {
    String response = getResponse("jetty.eclipse.org", "jetty.eclipse.org");
    Assert.assertThat(response, Matchers.containsString("X-HOST: jetty.eclipse.org"));
    response = getResponse("www.example.com", "www.example.com");
    Assert.assertThat(response, Matchers.containsString("X-HOST: www.example.com"));
    response = getResponse("foo.domain.com", "*.domain.com");
    Assert.assertThat(response, Matchers.containsString("X-HOST: foo.domain.com"));
    response = getResponse("m.san.com", "san example");
    Assert.assertThat(response, Matchers.containsString("X-HOST: m.san.com"));
    response = getResponse("www.san.com", "san example");
    Assert.assertThat(response, Matchers.containsString("X-HOST: www.san.com"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Matchers.containsString (org.hamcrest.Matchers.containsString)26 Test (org.junit.Test)24 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 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)4 InputStream (java.io.InputStream)3 ByteBuffer (java.nio.ByteBuffer)3 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)3 AbstractLogger (org.eclipse.jetty.util.log.AbstractLogger)3 SSLSocket (javax.net.ssl.SSLSocket)2 ServletInputStream (javax.servlet.ServletInputStream)2 ServerConnector (org.eclipse.jetty.server.ServerConnector)2 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)2