|
| 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 | +packagecom.ning.http.client.async; |
| 15 | + |
| 16 | +importstaticcom.ning.http.client.async.BasicHttpsTest.createSSLContext; |
| 17 | + |
| 18 | +importcom.ning.http.client.AsyncHttpClient; |
| 19 | +importcom.ning.http.client.AsyncHttpClientConfig; |
| 20 | +importcom.ning.http.client.AsyncHttpClientConfig.Builder; |
| 21 | +importcom.ning.http.client.Request; |
| 22 | +importcom.ning.http.client.RequestBuilder; |
| 23 | +importcom.ning.http.client.Response; |
| 24 | + |
| 25 | +importjava.io.File; |
| 26 | +importjava.io.IOException; |
| 27 | +importjava.net.URL; |
| 28 | +importjava.util.concurrent.ExecutionException; |
| 29 | +importjava.util.concurrent.Future; |
| 30 | +importjava.util.concurrent.atomic.AtomicBoolean; |
| 31 | + |
| 32 | +importjavax.servlet.ServletException; |
| 33 | +importjavax.servlet.http.HttpServletRequest; |
| 34 | +importjavax.servlet.http.HttpServletResponse; |
| 35 | + |
| 36 | +importorg.eclipse.jetty.server.Server; |
| 37 | +importorg.eclipse.jetty.server.handler.AbstractHandler; |
| 38 | +importorg.eclipse.jetty.server.ssl.SslSocketConnector; |
| 39 | +importorg.testng.Assert; |
| 40 | +importorg.testng.annotations.AfterClass; |
| 41 | +importorg.testng.annotations.BeforeClass; |
| 42 | +importorg.testng.annotations.Test; |
| 43 | + |
| 44 | +publicabstractclassConnectionCloseTestextendsAbstractBasicTest { |
| 45 | + |
| 46 | +@BeforeClass(alwaysRun =true) |
| 47 | +publicvoidsetUpGlobal()throwsException { |
| 48 | +server =newServer(); |
| 49 | +port1 =findFreePort(); |
| 50 | +SslSocketConnectorconnector =newSslSocketConnector(); |
| 51 | +connector.setHost("127.0.0.1"); |
| 52 | +connector.setPort(port1); |
| 53 | + |
| 54 | +ClassLoadercl =getClass().getClassLoader(); |
| 55 | + |
| 56 | +URLcacertsUrl =cl.getResource("ssltest-cacerts.jks"); |
| 57 | +StringtrustStoreFile =newFile(cacertsUrl.toURI()).getAbsolutePath(); |
| 58 | +connector.setTruststore(trustStoreFile); |
| 59 | +connector.setTrustPassword("changeit"); |
| 60 | +connector.setTruststoreType("JKS"); |
| 61 | + |
| 62 | +URLkeystoreUrl =cl.getResource("ssltest-keystore.jks"); |
| 63 | +StringkeyStoreFile =newFile(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 | +publicvoidtearDownGlobal()throwsException { |
| 77 | +server.stop(); |
| 78 | + } |
| 79 | + |
| 80 | +publicstaticclassCloseConnectionHandlerextendsAbstractHandler { |
| 81 | + |
| 82 | +@Override |
| 83 | +publicvoidhandle(StringpathInContext,org.eclipse.jetty.server.Requestrequest,HttpServletRequesthttpRequest, |
| 84 | +HttpServletResponsehttpResponse)throwsIOException,ServletException { |
| 85 | +httpResponse.setHeader("Connection","close"); |
| 86 | +httpResponse.setStatus(200); |
| 87 | +httpResponse.getOutputStream().flush(); |
| 88 | +httpResponse.getOutputStream().close(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +@Override |
| 93 | +publicAbstractHandlerconfigureHandler()throwsException { |
| 94 | +returnnewCloseConnectionHandler(); |
| 95 | + } |
| 96 | + |
| 97 | +@Test |
| 98 | +publicvoidhandlesCloseResponse()throwsIOException,InterruptedException,ExecutionException { |
| 99 | + |
| 100 | +AsyncHttpClientConfigconfig =newBuilder().setSSLContext(createSSLContext(newAtomicBoolean(true))).build(); |
| 101 | + |
| 102 | +try (AsyncHttpClientclient =getAsyncHttpClient(config)) { |
| 103 | +Requestrequest =newRequestBuilder("GET").setHeader("Connection","keep-alive").setUrl(getTargetUrl()).build(); |
| 104 | +Future<Response>responseFuture =client.executeRequest(request); |
| 105 | +intstatus =responseFuture.get().getStatusCode(); |
| 106 | +Assert.assertEquals(status,200); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +protectedStringgetTargetUrl() { |
| 111 | +returnString.format("https://127.0.0.1:%d/foo/test",port1); |
| 112 | + } |
| 113 | + |
| 114 | +} |
| 115 | + |