在 JerseyTest 下为 grizzly 打开 gzip 压缩

发布于 2024-10-08 19:25:54 字数 1355 浏览 4 评论 0原文

我有网络服务的球衣实现。根据要求的响应必须经过 gzip 压缩。

客户端包含以下引导代码来打开 gzip:

Client retval = Client.create();
retval.addFilter(
    new com.sun.jersey.api.client.filter.GZIPContentEncodingFilter());

对于 Tomcat web.xml gzip 配置如下

 <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>

一切正常!

但我需要编写调用我的服务的单元测试。我使用 JerseyTest 作为基础,在实践中表明,在没有明确声明的情况下,grizzly 无法正确处理 gzip。我找到了如何在类似问题上切换它的代码片段,但我不知道如何将它与 JerseyTest 结合起来。

先感谢您

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你穿错了嫁妆 2024-10-15 19:25:54

如果您使用球衣测试框架,这里是一个示例测试用例:

@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());
}

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());
}
别念他 2024-10-15 19:25:54

由于当前 Jersey 版本中的客户端 API 发生了变化,这是一个适用于 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"));
    }
}

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"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文