|
| 1 | +/* |
| 2 | + * Copyright 2010 Ning, Inc. |
| 3 | + * |
| 4 | + * Ning licenses this file to you under the Apache License, version 2.0 |
| 5 | + * (the "License"); you may not use this file except in compliance with the |
| 6 | + * License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package com.ning.http.client.async; |
| 17 | + |
| 18 | +import com.ning.http.client.AsyncHttpClient; |
| 19 | +import com.ning.http.client.AsyncHttpClientConfig; |
| 20 | +import com.ning.http.client.ListenableFuture; |
| 21 | +import com.ning.http.client.RequestBuilder; |
| 22 | +import com.ning.http.client.Response; |
| 23 | +import com.ning.http.client.providers.netty.NettyAsyncHttpProviderConfig; |
| 24 | +import org.eclipse.jetty.server.Connector; |
| 25 | +import org.eclipse.jetty.server.Server; |
| 26 | +import org.eclipse.jetty.server.nio.SelectChannelConnector; |
| 27 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 28 | +import org.eclipse.jetty.servlet.ServletHolder; |
| 29 | +import org.testng.annotations.AfterClass; |
| 30 | +import org.testng.annotations.BeforeClass; |
| 31 | +import org.testng.annotations.Test; |
| 32 | + |
| 33 | +import javax.servlet.ServletException; |
| 34 | +import javax.servlet.http.HttpServlet; |
| 35 | +import javax.servlet.http.HttpServletRequest; |
| 36 | +import javax.servlet.http.HttpServletResponse; |
| 37 | +import java.io.IOException; |
| 38 | +import java.io.OutputStream; |
| 39 | +import java.net.ServerSocket; |
| 40 | +import java.util.Date; |
| 41 | + |
| 42 | +import static org.testng.Assert.assertEquals; |
| 43 | +import static org.testng.Assert.assertNotNull; |
| 44 | +import static org.testng.FileAssert.fail; |
| 45 | + |
| 46 | +/** |
| 47 | + * Test for multithreaded url fetcher calls that use two separate |
| 48 | + * sets of ssl certificates. This then tests that the certificate |
| 49 | + * settings do not clash (override each other), resulting in the |
| 50 | + * peer not authenticated exception |
| 51 | + * |
| 52 | + * @author dominict |
| 53 | + */ |
| 54 | +public class RedirectConnectionUsageTest { |
| 55 | + private String BASE_URL; |
| 56 | + |
| 57 | + private String servletEndpointRedirectUrl; |
| 58 | + |
| 59 | + private Server server; |
| 60 | + |
| 61 | + private int port1; |
| 62 | + |
| 63 | + public static int findFreePort() throws IOException { |
| 64 | + ServerSocket socket = null; |
| 65 | + |
| 66 | + try { |
| 67 | + // 0 is open a socket on any free port |
| 68 | + socket = new ServerSocket(0); |
| 69 | + return socket.getLocalPort(); |
| 70 | + } finally { |
| 71 | + if (socket != null) { |
| 72 | + socket.close(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @BeforeClass |
| 79 | + public void setUp() throws Exception { |
| 80 | + server = new Server(); |
| 81 | + |
| 82 | + port1 = findFreePort(); |
| 83 | + |
| 84 | + Connector listener = new SelectChannelConnector(); |
| 85 | + listener.setHost("localhost"); |
| 86 | + listener.setPort(port1); |
| 87 | + |
| 88 | + server.addConnector(listener); |
| 89 | + |
| 90 | + |
| 91 | + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); |
| 92 | + |
| 93 | + context.setContextPath("/"); |
| 94 | + server.setHandler(context); |
| 95 | + |
| 96 | + context.addServlet(new ServletHolder(new MockRedirectHttpServlet()), "/redirect/*"); |
| 97 | + context.addServlet(new ServletHolder(new MockFullResponseHttpServlet()), "/*"); |
| 98 | + |
| 99 | + server.start(); |
| 100 | + |
| 101 | + BASE_URL = "http://localhost" + ":" + port1; |
| 102 | + servletEndpointRedirectUrl = BASE_URL + "/redirect"; |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + @AfterClass |
| 107 | + public void tearDown() { |
| 108 | + try { |
| 109 | + if (server != null) { |
| 110 | + server.stop(); |
| 111 | + } |
| 112 | + |
| 113 | + } catch (Exception e) { |
| 114 | + System.err.print("Error stopping servlet tester"); |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Tests that after a redirect the final url in the response |
| 121 | + * reflect the redirect |
| 122 | + */ |
| 123 | + @Test |
| 124 | + public void testGetRedirectFinalUrl() { |
| 125 | + |
| 126 | + AsyncHttpClient c = null; |
| 127 | + try { |
| 128 | + AsyncHttpClientConfig.Builder bc = |
| 129 | + new AsyncHttpClientConfig.Builder(); |
| 130 | + |
| 131 | + bc.setAllowPoolingConnection(true); |
| 132 | + bc.setMaximumConnectionsPerHost(1); |
| 133 | + bc.setMaximumConnectionsTotal(1); |
| 134 | + bc.setConnectionTimeoutInMs(1000); |
| 135 | + bc.setRequestTimeoutInMs(1000); |
| 136 | + bc.setFollowRedirects(true); |
| 137 | + |
| 138 | + NettyAsyncHttpProviderConfig config = new NettyAsyncHttpProviderConfig(); |
| 139 | + if (System.getProperty("blockingio") != null) |
| 140 | + config.addProperty(NettyAsyncHttpProviderConfig.USE_BLOCKING_IO, "true"); |
| 141 | + //config.addProperty(NettyAsyncHttpProviderConfig.REUSE_ADDRESS, "true"); |
| 142 | + bc.setAsyncHttpClientProviderConfig(config); |
| 143 | + c = new AsyncHttpClient(bc.build()); |
| 144 | + |
| 145 | + RequestBuilder builder = new RequestBuilder("GET"); |
| 146 | + builder.setUrl(servletEndpointRedirectUrl); |
| 147 | + |
| 148 | + com.ning.http.client.Request r = builder.build(); |
| 149 | + |
| 150 | + try { |
| 151 | + ListenableFuture<Response> response = c.executeRequest(r); |
| 152 | + Response res = null; |
| 153 | + res = response.get(); |
| 154 | + assertNotNull(res.getResponseBody()); |
| 155 | + assertEquals(BASE_URL + "/overthere", BASE_URL + "/overthere", res.getUri().toString()); |
| 156 | + |
| 157 | + } catch (Exception e) { |
| 158 | + System.err.print("============"); |
| 159 | + e.printStackTrace(); |
| 160 | + System.err.print("============"); |
| 161 | + System.err.flush(); |
| 162 | + fail("Should not get here, The request threw an exception"); |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + } |
| 167 | + finally { |
| 168 | + // can hang here |
| 169 | + if (c != null) c.close(); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + class MockRedirectHttpServlet extends HttpServlet { |
| 177 | + public void service(HttpServletRequest req, HttpServletResponse res) |
| 178 | + throws ServletException, IOException { |
| 179 | + res.sendRedirect("/overthere"); |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + class MockFullResponseHttpServlet extends HttpServlet { |
| 185 | + |
| 186 | + private static final String contentType = "text/xml"; |
| 187 | + private static final String xml = "<?xml version=\"1.0\"?><hello date=\"%s\"></hello>"; |
| 188 | + |
| 189 | + public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { |
| 190 | + String xmlToReturn = String.format(xml, new Object[]{new Date().toString()}); |
| 191 | + |
| 192 | + res.setStatus(200, "Complete, XML Being Returned"); |
| 193 | + res.addHeader("Content-Type", contentType); |
| 194 | + res.addHeader("X-Method", req.getMethod()); |
| 195 | + res.addHeader("MultiValue", "1"); |
| 196 | + res.addHeader("MultiValue", "2"); |
| 197 | + res.addHeader("MultiValue", "3"); |
| 198 | + |
| 199 | + OutputStream os = res.getOutputStream(); |
| 200 | + |
| 201 | + byte[] retVal = xmlToReturn.getBytes(); |
| 202 | + res.setContentLength(retVal.length); |
| 203 | + os.write(retVal); |
| 204 | + os.close(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | +} |
0 commit comments