ApacheCommons4xHttpClient implements HttpClient { @Override public RemoteObject resource(final String uri) { return new RemoteObject() { @Override public Response trace(Representation r, String query, HeaderField ... fields) { return doit(query, r, fields, "trace", uri); } @Override public Response put(Representation r, String query, HeaderField ... fields) { return doit(query, r, fields, "put", uri); } @Override public Response post(Representation r, String query, HeaderField ... fields) { return doit(query, r, fields, "post", uri); } @Override public Response patch(Representation r, String query, HeaderField ... fields) { return doit(query, r, fields, "patch", uri); } @Override public Response options(Representation r, String query, HeaderField ... fields) { return doit(query, r, fields, "options", uri); } @Override public Response head(Representation r, String query, HeaderField ... fields) { return doit(query, r, fields, "head", uri); } @Override public Response get(Representation r, String query, HeaderField ... fields) { return doit(query, r, fields, "get", uri); } @Override public Response delete(Representation r, String query, HeaderField ... fields) { return doit(query, r, fields, "delete", uri); } }; } ApacheCommons4xHttpClient();  ApacheCommons4xHttpClient(org.apache.http.client.HttpClient client); @Override RemoteObject resource(final String uri);  }
@Test public void sendsRequests() throws Exception { final Channel server = serve(port, new Echoer("/echo")); try { final HttpClient testSubject = new ApacheCommons4xHttpClient(); final Response response = testSubject .resource("http: .post(DSL.Text("this is my content\nsee it?"), new GenericHeaderField("echo-header-A", "alpha"), new GenericHeaderField("echo-header-B", "beta")); assertEquals( "POST /echo\n" + "echo-header-A=alpha\n" + "echo-header-B=beta\n" + "this is my content\n" + "see it?", HttpObjectAssert.bodyOf(response).asString()); } finally { server.close(); } }
@Test public void returnsResponses() throws Exception { final Channel server = serve(port, new HttpObject("/some/resource/with/headers") { @Override public Response get(Request req) { return OK(Text("You GET it"), new GenericHeaderField("a-custom-header-name", "a-custom-header-value")); } }); try { final HttpClient testSubject = new ApacheCommons4xHttpClient(); final Response response = testSubject .resource("http: .get(); assertEquals(ResponseCode.OK, response.code()); assertEquals("You GET it", HttpObjectAssert.bodyOf(response).asString()); assertEquals("text/plain; charset=utf-8", response.representation().contentType().toLowerCase()); assertEquals( "a-custom-header-value", findByName("a-custom-header-name", response.header()).value()); } finally { server.close(); } }
@Test public void supportsAllTheMethods() throws Exception { final Channel server = serve(port, new MethodEchoer("/i-have-all-the-methods")); try { for (Method method : Method.values()) { final HttpClient testSubject = new ApacheCommons4xHttpClient(); final java.lang.reflect.Method m = RemoteObject.class.getMethod(method.name().toLowerCase(), String.class, HeaderField[].class); final RemoteObject o = testSubject.resource("http: final Response response = (Response) m.invoke(o, "?foo=bar", (Object) new HeaderField[]{new GenericHeaderField("echo-foo", "bar")}); assertEquals(ResponseCode.OK, response.code()); assertEquals(method.name().toLowerCase(), findByName("method-name", response.header()).value()); } } finally { server.close(); } }
@Test public void supportsAllTheMethodsWithTheQueryOnlyConvenienceVersion() throws Exception { final Channel server = serve(port, new Echoer("/i-have-all-the-methods")); try { for (Method method : Method.values()) { final HttpClient testSubject = new ApacheCommons4xHttpClient(); final java.lang.reflect.Method m = RemoteObject.class.getMethod(method.name().toLowerCase(), String.class, HeaderField[].class); final RemoteObject o = testSubject.resource("http: final Response response = (Response) m.invoke(o, "?foo=bar", (Object) new HeaderField[]{new GenericHeaderField("echo-foo", "bar")}); assertEquals(ResponseCode.OK, response.code()); assertEquals(method.name().toUpperCase() + " /i-have-all-the-methods?foo=bar\necho-foo=bar\n", bodyInHeader(response)); } } finally { server.close(); } }
@Test public void supportsAllTheMethodsWithTheRepresentationOnlyConvenienceVersion() throws Exception { final Channel server = serve(port, new Echoer("/i-have-all-the-methods")); try { for (Method method : Method.values()) { final HttpClient testSubject = new ApacheCommons4xHttpClient(); final java.lang.reflect.Method m = RemoteObject.class.getMethod(method.name().toLowerCase(), Representation.class, HeaderField[].class); final RemoteObject o = testSubject.resource("http: final Response response = (Response) m.invoke(o, Text("yo"), (Object) new HeaderField[]{new GenericHeaderField("echo-foo", "bar")}); assertEquals(ResponseCode.OK, response.code()); assertEquals(method.name().toUpperCase() + " /i-have-all-the-methods\necho-foo=bar\nyo", bodyInHeader(response)); } } finally { server.close(); } }
@Test public void supportsAllTheMethodsWithTheNoArgsConvenienceVersion() throws Exception { final Channel server = serve(port, new Echoer("/i-have-all-the-methods")); try { for (Method method : Method.values()) { final HttpClient testSubject = new ApacheCommons4xHttpClient(); final java.lang.reflect.Method m = RemoteObject.class.getMethod(method.name().toLowerCase(), HeaderField[].class); final RemoteObject o = testSubject.resource("http: final Response response = (Response) m.invoke(o, (Object) new HeaderField[]{new GenericHeaderField("echo-foo", "bar")}); assertEquals(ResponseCode.OK, response.code()); assertEquals(method.name().toUpperCase() + " /i-have-all-the-methods\necho-foo=bar\n", bodyInHeader(response)); } } catch (Exception e) { throw new RuntimeException(e); } finally { server.close(); } }