Search in sources :

Example 96 with Test

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

the class HttpClientTransportOverHTTP2Test method testExternalServer.

@Ignore
@Test
public void testExternalServer() throws Exception {
    HTTP2Client http2Client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory();
    HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
    Executor executor = new QueuedThreadPool();
    httpClient.setExecutor(executor);
    httpClient.start();
    //        ContentResponse response = httpClient.GET("https://http2.akamai.com/");
    ContentResponse response = httpClient.GET("https://webtide.com/");
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    httpClient.stop();
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Executor(java.util.concurrent.Executor) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 97 with Test

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

the class HttpClientTransportOverHTTP2Test method testRequestHasHTTP2Version.

@Test
public void testRequestHasHTTP2Version() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            HttpVersion version = HttpVersion.fromString(request.getProtocol());
            response.setStatus(version == HttpVersion.HTTP_2 ? HttpStatus.OK_200 : HttpStatus.INTERNAL_SERVER_ERROR_500);
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).onRequestBegin(request -> {
        if (request.getVersion() != HttpVersion.HTTP_2)
            request.abort(new Exception("Not a HTTP/2 request"));
    }).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) ServerSocket(java.net.ServerSocket) HttpClient(org.eclipse.jetty.client.HttpClient) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetaData(org.eclipse.jetty.http.MetaData) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) HttpStatus(org.eclipse.jetty.http.HttpStatus) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Callback(org.eclipse.jetty.util.Callback) HttpDestination(org.eclipse.jetty.client.HttpDestination) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Session(org.eclipse.jetty.http2.api.Session) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) BufferUtil(org.eclipse.jetty.util.BufferUtil) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) ErrorCode(org.eclipse.jetty.http2.ErrorCode) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SocketTimeoutException(java.net.SocketTimeoutException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpFields(org.eclipse.jetty.http.HttpFields) OutputStream(java.io.OutputStream) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Executor(java.util.concurrent.Executor) RawHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Generator(org.eclipse.jetty.http2.generator.Generator) HttpMethod(org.eclipse.jetty.http.HttpMethod) Ignore(org.junit.Ignore) Assert(org.junit.Assert) InputStream(java.io.InputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpVersion(org.eclipse.jetty.http.HttpVersion) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 98 with Test

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

the class HttpClientTransportOverHTTP2Test method testRequestViaForwardHttpProxy.

@Test
public void testRequestViaForwardHttpProxy() throws Exception {
    String path = "/path";
    String query = "a=b";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Assert.assertEquals(path, request.getRequestURI());
            Assert.assertEquals(query, request.getQueryString());
        }
    });
    int proxyPort = connector.getLocalPort();
    client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", proxyPort));
    // Any port will do, just not the same as the proxy.
    int serverPort = proxyPort + 1;
    ContentResponse response = client.newRequest("localhost", serverPort).path(path + "?" + query).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpProxy(org.eclipse.jetty.client.HttpProxy) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 99 with Test

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

the class HttpClientTransportOverHTTP2Test method testResponseAbortSendsResetFrame.

@Test
public void testResponseAbortSendsResetFrame() throws Exception {
    CountDownLatch resetLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), metaData, null, false), new Callback() {

                @Override
                public void succeeded() {
                    ByteBuffer data = ByteBuffer.allocate(1024);
                    stream.data(new DataFrame(stream.getId(), data, false), NOOP);
                }
            });
            return new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    resetLatch.countDown();
                }
            };
        }
    });
    try {
        client.newRequest("localhost", connector.getLocalPort()).onResponseContent((response, buffer) -> response.abort(new Exception("explicitly_aborted_by_test"))).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    }
}
Also used : Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) ServerSocket(java.net.ServerSocket) HttpClient(org.eclipse.jetty.client.HttpClient) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetaData(org.eclipse.jetty.http.MetaData) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) HttpStatus(org.eclipse.jetty.http.HttpStatus) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Callback(org.eclipse.jetty.util.Callback) HttpDestination(org.eclipse.jetty.client.HttpDestination) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Session(org.eclipse.jetty.http2.api.Session) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) BufferUtil(org.eclipse.jetty.util.BufferUtil) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) ErrorCode(org.eclipse.jetty.http2.ErrorCode) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SocketTimeoutException(java.net.SocketTimeoutException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpFields(org.eclipse.jetty.http.HttpFields) OutputStream(java.io.OutputStream) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Executor(java.util.concurrent.Executor) RawHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Generator(org.eclipse.jetty.http2.generator.Generator) HttpMethod(org.eclipse.jetty.http.HttpMethod) Ignore(org.junit.Ignore) Assert(org.junit.Assert) InputStream(java.io.InputStream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) InputStream(java.io.InputStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ExecutionException(java.util.concurrent.ExecutionException) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 100 with Test

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

the class CloseTest method testClientAbruptlyClosesConnection.

@Test
public void testClientAbruptlyClosesConnection() throws Exception {
    final CountDownLatch closeLatch = new CountDownLatch(1);
    final AtomicReference<Session> sessionRef = new AtomicReference<>();
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            try {
                sessionRef.set(stream.getSession());
                MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
                // Reply with HEADERS.
                stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
                closeLatch.await(5, TimeUnit.SECONDS);
                return null;
            } catch (InterruptedException x) {
                return null;
            }
        }
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                try {
                    // Close the connection just after
                    // receiving the response headers.
                    client.close();
                    closeLatch.countDown();
                } catch (IOException x) {
                    throw new RuntimeIOException(x);
                }
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        // We need to give some time to the server to receive and process the TCP FIN.
        Thread.sleep(1000);
        Session session = sessionRef.get();
        Assert.assertTrue(session.isClosed());
        Assert.assertTrue(((HTTP2Session) session).isDisconnected());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)591606 ArrayList (java.util.ArrayList)32306 QuickTest (com.hazelcast.test.annotation.QuickTest)27910 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)25464 File (java.io.File)24371 HashMap (java.util.HashMap)20598 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)19416 List (java.util.List)16209 IOException (java.io.IOException)13083 Map (java.util.Map)9056 Date (java.util.Date)8811 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8305 InputStream (java.io.InputStream)7624 Properties (java.util.Properties)7365 CountDownLatch (java.util.concurrent.CountDownLatch)7322 ByteArrayInputStream (java.io.ByteArrayInputStream)7230 HashSet (java.util.HashSet)7019 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6379 Response (javax.ws.rs.core.Response)5810 Ignore (org.junit.Ignore)5729