|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 AsyncHttpClient Project. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, |
| 10 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 13 | + */ |
| 14 | +package com.ning.http.client.async; |
| 15 | + |
| 16 | +import static com.ning.http.client.async.BasicHttpsTest.createSSLContext; |
| 17 | + |
| 18 | +import com.ning.http.client.AsyncHttpClient; |
| 19 | +import com.ning.http.client.AsyncHttpClientConfig; |
| 20 | +import com.ning.http.client.AsyncHttpClientConfig.Builder; |
| 21 | +import com.ning.http.client.Request; |
| 22 | +import com.ning.http.client.RequestBuilder; |
| 23 | +import com.ning.http.client.Response; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.URL; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | +import java.util.concurrent.Future; |
| 30 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 31 | + |
| 32 | +import javax.servlet.ServletException; |
| 33 | +import javax.servlet.http.HttpServletRequest; |
| 34 | +import javax.servlet.http.HttpServletResponse; |
| 35 | + |
| 36 | +import org.eclipse.jetty.server.Server; |
| 37 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 38 | +import org.eclipse.jetty.server.ssl.SslSocketConnector; |
| 39 | +import org.testng.Assert; |
| 40 | +import org.testng.annotations.AfterClass; |
| 41 | +import org.testng.annotations.BeforeClass; |
| 42 | +import org.testng.annotations.Test; |
| 43 | + |
| 44 | +public abstract class ConnectionCloseTest extends AbstractBasicTest { |
| 45 | + |
| 46 | + @BeforeClass(alwaysRun = true) |
| 47 | + public void setUpGlobal() throws Exception { |
| 48 | + server = new Server(); |
| 49 | + port1 = findFreePort(); |
| 50 | + SslSocketConnector connector = new SslSocketConnector(); |
| 51 | + connector.setHost("127.0.0.1"); |
| 52 | + connector.setPort(port1); |
| 53 | + |
| 54 | + ClassLoader cl = getClass().getClassLoader(); |
| 55 | + |
| 56 | + URL cacertsUrl = cl.getResource("ssltest-cacerts.jks"); |
| 57 | + String trustStoreFile = new File(cacertsUrl.toURI()).getAbsolutePath(); |
| 58 | + connector.setTruststore(trustStoreFile); |
| 59 | + connector.setTrustPassword("changeit"); |
| 60 | + connector.setTruststoreType("JKS"); |
| 61 | + |
| 62 | + URL keystoreUrl = cl.getResource("ssltest-keystore.jks"); |
| 63 | + String keyStoreFile = new File(keystoreUrl.toURI()).getAbsolutePath(); |
| 64 | + connector.setKeystore(keyStoreFile); |
| 65 | + connector.setKeyPassword("changeit"); |
| 66 | + connector.setKeystoreType("JKS"); |
| 67 | + |
| 68 | + server.addConnector(connector); |
| 69 | + |
| 70 | + server.setHandler(configureHandler()); |
| 71 | + server.start(); |
| 72 | + log.info("Local HTTP server started successfully"); |
| 73 | + } |
| 74 | + |
| 75 | + @AfterClass(alwaysRun = true) |
| 76 | + public void tearDownGlobal() throws Exception { |
| 77 | + server.stop(); |
| 78 | + } |
| 79 | + |
| 80 | + public static class CloseConnectionHandler extends AbstractHandler { |
| 81 | + |
| 82 | + @Override |
| 83 | + public void handle(String pathInContext, org.eclipse.jetty.server.Request request, HttpServletRequest httpRequest, |
| 84 | + HttpServletResponse httpResponse) throws IOException, ServletException { |
| 85 | + httpResponse.setHeader("Connection", "close"); |
| 86 | + httpResponse.setStatus(200); |
| 87 | + httpResponse.getOutputStream().flush(); |
| 88 | + httpResponse.getOutputStream().close(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public AbstractHandler configureHandler() throws Exception { |
| 94 | + return new CloseConnectionHandler(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void handlesCloseResponse() throws IOException, InterruptedException, ExecutionException { |
| 99 | + |
| 100 | + AsyncHttpClientConfig config = new Builder().setSSLContext(createSSLContext(new AtomicBoolean(true))).build(); |
| 101 | + |
| 102 | + try (AsyncHttpClient client = getAsyncHttpClient(config)) { |
| 103 | + Request request = new RequestBuilder("GET").setHeader("Connection", "keep-alive").setUrl(getTargetUrl()).build(); |
| 104 | + Future<Response> responseFuture = client.executeRequest(request); |
| 105 | + int status = responseFuture.get().getStatusCode(); |
| 106 | + Assert.assertEquals(status, 200); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + protected String getTargetUrl() { |
| 111 | + return String.format("https://127.0.0.1:%d/foo/test", port1); |
| 112 | + } |
| 113 | + |
| 114 | +} |
| 115 | + |
0 commit comments