|
| 1 | +package gate.util; |
| 2 | + |
| 3 | +import gate.Utils; |
| 4 | +import junit.framework.TestCase; |
| 5 | +import org.apache.http.HttpException; |
| 6 | +import org.apache.http.HttpRequest; |
| 7 | +import org.apache.http.HttpResponse; |
| 8 | +import org.apache.http.impl.bootstrap.HttpServer; |
| 9 | +import org.apache.http.impl.bootstrap.ServerBootstrap; |
| 10 | +import org.apache.http.protocol.HttpContext; |
| 11 | +import org.apache.http.protocol.HttpRequestHandler; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.net.*; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +public class TestResolveUrl extends TestCase { |
| 18 | + |
| 19 | + static class RedirectingHandler implements HttpRequestHandler { |
| 20 | + |
| 21 | + InetAddress myAddress; |
| 22 | + int port; |
| 23 | + |
| 24 | + @Override |
| 25 | + public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { |
| 26 | + response.setStatusCode(303); |
| 27 | + try { |
| 28 | + String path = request.getRequestLine().getUri(); |
| 29 | + if(path.matches("/loop/[0-3]")) { |
| 30 | + // simulates a redirection loop |
| 31 | + int nextNum = (path.charAt(6) - '0' + 1) % 4; |
| 32 | + String nextPath = "/loop/" + nextNum; |
| 33 | + System.err.println("Redirecting to " + nextPath); |
| 34 | + response.addHeader("Location", new URI("http", null, myAddress.getHostAddress(), port, nextPath, null, null).toString()); |
| 35 | + } else if(path.startsWith("/infinite/")) { |
| 36 | + // simulates a redirection loop |
| 37 | + String nextPath = path + "/x"; |
| 38 | + System.err.println("Redirecting " + path + " to " + nextPath); |
| 39 | + response.addHeader("Location", new URI("http", null, myAddress.getHostAddress(), port, nextPath, null, null).toString()); |
| 40 | + } else if(path.equals("/redirect-to-file")) { |
| 41 | + response.addHeader("Location", new URI("http", null, myAddress.getHostAddress(), port, "/file", null, null).toString()); |
| 42 | + } else if(path.equals("/file")) { |
| 43 | + System.err.println("Attempting to redirect to a file: URL"); |
| 44 | + response.addHeader("Location", "file:/etc/hosts"); |
| 45 | + } |
| 46 | + } catch(URISyntaxException e) { |
| 47 | + throw new IOException(e); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + HttpServer server; |
| 53 | + URL baseUrl; |
| 54 | + |
| 55 | + public void setUp() throws Exception { |
| 56 | + RedirectingHandler handler = new RedirectingHandler(); |
| 57 | + server = ServerBootstrap.bootstrap() |
| 58 | + .setLocalAddress(InetAddress.getLoopbackAddress()) |
| 59 | + .registerHandler("*", handler).create(); |
| 60 | + server.start(); |
| 61 | + handler.myAddress = server.getInetAddress(); |
| 62 | + handler.port = server.getLocalPort(); |
| 63 | + baseUrl = new URL("http", server.getInetAddress().getHostAddress(), server.getLocalPort(), ""); |
| 64 | + System.err.println("Started server at " + baseUrl); |
| 65 | + } |
| 66 | + |
| 67 | + public void testRedirectLoop() throws Exception { |
| 68 | + URL url = new URL(baseUrl, "/loop/0"); |
| 69 | + try { |
| 70 | + URL newUrl = Utils.resolveURL(url); |
| 71 | + fail("resolveURL should have failed due to redirect loop"); |
| 72 | + } catch(IOException e) { |
| 73 | + // exception expected |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public void testTooManyRedirects() throws Exception { |
| 78 | + URL url = new URL(baseUrl, "/infinite/x"); |
| 79 | + try { |
| 80 | + URL newUrl = Utils.resolveURL(url); |
| 81 | + fail("resolveURL should have failed due to too many redirects"); |
| 82 | + } catch(IOException e) { |
| 83 | + // exception expected |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public void testRedirectToFile() throws Exception { |
| 88 | + URL url = new URL(baseUrl, "/redirect-to-file"); |
| 89 | + URL newUrl = Utils.resolveURL(url); |
| 90 | + assertEquals("Redirection should have stopped at http://.../file", new URL(baseUrl, "/file"), newUrl); |
| 91 | + } |
| 92 | + |
| 93 | + public void tearDown() throws Exception { |
| 94 | + server.stop(); |
| 95 | + server.awaitTermination(20, TimeUnit.SECONDS); |
| 96 | + } |
| 97 | +} |
0 commit comments