Reputation: 23624
I have jersey implementation of web service. The response per requirements must be gzip-ed.
Client side contains following bootstrap code to switch gzip on:
Client retval = Client.create();
retval.addFilter(
new com.sun.jersey.api.client.filter.GZIPContentEncodingFilter());
For Tomcat web.xml gzip is configured as follow
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.GZIPContentEncodingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.GZIPContentEncodingFilter</param-value>
</init-param>
And everything works fine!
But I need write unit test that invokes my service. I'm using JerseyTest as base and in practice way it was shown that grizzly is not correctly handles gzip without explicit declaration. I have found code snippet how to switch it on similar problem, but I have no idea how to combine it with JerseyTest.
Thank you in advance
Upvotes: 1
Views: 2481
Reputation: 3320
AS the client API changed in the current Jersey versions, this is a sample test which works with Jersey 2.6:
public class WebServicesCompressionTest extends JerseyTest {
@Path("/")
public static class HelloResource {
@GET
public String getHello() {
return "Hello World!";
}
}
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
return new ResourceConfig(
HelloResource.class,
EncodingFilter.class,
GZipEncoder.class,
DeflateEncoder.class
);
}
@Test
public void testGzip() {
Response response = target().request().acceptEncoding("gzip").get(Response.class);
assertThat(response.getStatus(), is(200));
assertThat(response.getHeaderString("Content-encoding"), is("gzip"));
}
}
Upvotes: 0
Reputation: 7665
Here is a sample test case if you're using the jersey test Framwork:
@Test
public void testGet(){
WebResource webResource = resource();
ClientResponse result = webResource
.path("pathToResource")
.header("Accept-Encoding", "gzip")
.head();
assertEquals(
"response header must contain gzip encoding",
"[gzip]",
result.getHeaders().get("Content-Encoding").toString());
}
Upvotes: 1