Proxy extends HttpObject { @Override public Response get(Request req) { return proxyRequest(req, new GetMethod()); } Proxy(final String localPath, final String base, final String me);  Proxy(final String base, final String me); void setBase(String base); String getBase(); @Override Response get(Request req); @Override Response delete(Request req); @Override Response put(Request req); @Override Response patch(Request req); @Override Response options(Request req); @Override Response post(Request req);  }
@Test public void relaysTheOriginalHostField() { HttpObject subject = new Proxy("http: Response output = subject.get(new MockRequest(subject, "/headerEcho", new Query("?name=beforeTab%09afterTab"), new GenericHeaderField("Host", "dummy-remote-host-value"))); String representation = HttpObjectUtil.toAscii(output.representation()); System.out.println("representation = " + representation); assertThat(representation, containsString("X-Forwarded-Host=dummy-remote-host-value")); }
@Test public void handlesTargetsWithSlashes() { HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/frog"); Response output = subject.get(input); responseCodeOf(output).assertIsOK_200(); assertTrue(contentTypeOf(output).isPlainTextWithEncoding("utf-8")); assertTrue(bodyOf(output).equals("Kermit")); }
@Test public void handlesRedirects() { HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/notme"); Response output = subject.get(input); assertEquals("", bodyOf(output).asString()); responseCodeOf(output).assertIs(ResponseCode.SEE_OTHER); locationHeaderOf(output).assertIs("/me"); }
@Test public void proxyShouldRelayTheOriginSourceIPAddress(){ HttpObject subject = new Proxy("http: Request input = new MockRequest( new ConnectionInfo("dummy-local-ip-address",8080,"dummy-remote-ip-address",10001), subject, "/headerEcho", new Query(""), HttpObject.Text("does not matter")); Response output = subject.get(input); String representation = HttpObjectUtil.toAscii(output.representation()); System.out.println("representation = " + representation); assertThat(representation, containsString("X-Forwarded-For=dummy-remote-ip-address")); }
@Test public void handlesRootRequests(){ HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/"); Response output = subject.get(input); String representation = HttpObjectUtil.toAscii(output.representation()); System.out.println("representation = " + representation); assertEquals(representation, "this is the root page"); }
@Test public void doesntDoubleEncodeTheUrl() { HttpObject subject = new Proxy("http: Response output = subject.get(new MockRequest(subject, "/queryStringEcho", new Query("?name=beforeTab%09afterTab"))); assertEquals("?name=beforeTab%09afterTab", bodyOf(output).asString()); }
@Test public void sendsNonTextContentTypes() throws Exception { HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/echo", utf8Bytes("hi")); Response output = subject.get(input); responseCodeOf(output).assertIs(ResponseCode.OK); contentTypeOf(output).assertIs("image/png"); }
@Test public void doesntSendBlankContentTypes() { HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/contentTypeEcho"); Response output = subject.get(input); assertEquals("null", bodyOf(output).asString()); }
@Test public void sendsCustomHeaders() { HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/requirescustomheader") { @Override public RequestHeader header() { return new RequestHeader(new GenericHeaderField("xyz123", "yes!")); } }; Response output = subject.get(input); responseCodeOf(output).assertIs(ResponseCode.OK); }
@Test public void proxiesSetCookieHeaders() { HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/setcookies"); Response output = subject.get(input); responseCodeOf(output).assertIs(ResponseCode.OK); cookiesIn(output).assertContains("id", "1234"); HttpObjectAssert.ContentType contentType = contentTypeOf(output); assertTrue(contentType.isPlainTextWithEncoding("utf-8")); assertEquals("Here is a tasty cookie", bodyOf(output).asString()); }
@Test public void proxiesOKGets() { HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/frog"); Response output = subject.get(input); responseCodeOf(output).assertIs(ResponseCode.OK); assertTrue(contentTypeOf(output).isPlainTextWithEncoding("utf-8")); assertEquals("Kermit", bodyOf(output).asString()); }
@Test public void proxiesQueryStrings() { HttpObject subject = new Proxy("http: Request input = new MockRequest(subject, "/frog", new Query("?name=kermit&property=value")); Response output = subject.get(input); assertTrue(responseCodeOf(output).isOK_200()); assertTrue(contentTypeOf(output).isPlainTextWithEncoding("utf-8")); assertEquals("Kermit?name=kermit&property=value", bodyOf(output).asString()); }