Created
January 8, 2016 12:12
-
-
Save psamsotha/b23f1e57ad7bc8fbde08 to your computer and use it in GitHub Desktop.
Stack Overflow question http://stackoverflow.com/q/34671186/2587435
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URI; | |
import java.util.ArrayList; | |
import java.util.logging.Logger; | |
import javax.ws.rs.BeanParam; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.FormParam; | |
import javax.ws.rs.HeaderParam; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.client.WebTarget; | |
import javax.ws.rs.core.Cookie; | |
import javax.ws.rs.core.Form; | |
import javax.ws.rs.core.MultivaluedHashMap; | |
import javax.ws.rs.core.MultivaluedMap; | |
import static junit.framework.Assert.assertEquals; | |
import org.glassfish.jersey.client.proxy.WebResourceFactory; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
/** | |
* Run it like any other JUnit test. Required dependencies: | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* <version>2.22.1</version> | |
* <scope>test</scope> | |
* </dependency> | |
* <dependency> | |
* <groupId>org.glassfish.jersey.ext</groupId> | |
* <artifactId>jersey-proxy-client</artifactId> | |
* <version>2.22.1</version> | |
* <scope>test</scope> | |
* </dependency> | |
*/ | |
public class ClientProxyTest extends JerseyTest { | |
public static class Bean { | |
@HeaderParam("X-Header") | |
public String header; | |
@FormParam("form-param") | |
public String form; | |
} | |
public static interface IResource { | |
@POST | |
@Path("{id}") | |
@Consumes("application/x-www-form-urlencoded") | |
public String post(@BeanParam Bean bean, @PathParam("id") String id); | |
} | |
@Path("resource") | |
public static class Resource implements IResource { | |
@Override | |
public String post(Bean bean, String id) { | |
return bean.form + ":" + bean.header + ":" + id; | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig(Resource.class) | |
.register(new LoggingFilter(Logger.getAnonymousLogger(), true)); | |
} | |
@Override | |
public URI getBaseUri() { | |
return URI.create(BASE_PATH); | |
} | |
private static final String BASE_PATH = "http://localhost:8080"; | |
@Test | |
public void doit() { | |
Client client = ClientBuilder.newClient(); | |
WebTarget target = client.target(BASE_PATH).path("resource"); | |
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); | |
headers.putSingle("X-Header", "headerValue"); | |
Form form = new Form(); | |
form.param("form-param", "formValue"); | |
IResource resource = WebResourceFactory.newResource( | |
IResource.class, target, true, headers, | |
new ArrayList<Cookie>(), form); | |
String response = resource.post(null, "idString"); | |
assertEquals("formValue:headerValue:idString", response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment