简体   繁体   English

用码头测试过滤器

[英]Testing filters with jetty

I've configured my servlets/filters using guice-servlet. 我已经使用guice-servlet配置了我的servlet /过滤器。 With bindings like 与像

 serve("/foo").with(HelloServlet.class);

Now, I want to test that mapping. 现在,我要测试该映射。 I've used jetty-testing 我用过码头测试

private ServletTester tester;
private HttpTester request;
private HttpTester response;

@BeforeMethod
public void setUp() {
    this.tester = new ServletTester();
    this.tester.setContextPath("/");
    this.tester.addEventListener(new Config()); //my guice servlet config goes there.

    this.tester.addFilter(GuiceFilter.class, "/*", 0);
    this.tester.addServlet(FakeServlet.class, "/*");        <-----!!!!

    this.tester.start();
    this.request = new HttpTester();
    this.response = new HttpTester();
    this.request.setMethod("GET");
    this.request.setHeader("Host", "tester");
    this.request.setVersion("HTTP/1.0");
}

@Test
public void test() {
    this.request.setURI("/foo");
    this.response.parse(tester.getResponses(request.generate()));
    assertEquals(this.response.getContent(), "Hello World");
}

It works. 有用。 But it made me to add some fake servlet that should bewer be invoked. 但这使我添加了一些应该被调用的假servlet。 How I can test it without adding such servlet? 如何在不添加此类servlet的情况下进行测试?

The servlet spec requires a servlet for the doFilter chain to make sense. Servlet规范需要一个servlet来使doFilter链有意义。

If you don't want to create your own Servlet in the test cases, just use the org.eclipse.jetty.servlet.DefaultServlet.class instead (found in the jetty-servlet.jar ). 如果您不想在测试用例中创建自己的Servlet,只需改用org.eclipse.jetty.servlet.DefaultServlet.class (位于jetty-servlet.jar中 )即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM