通过HTTPS进行JerseyTest [英] JerseyTest over HTTPS

查看:100
本文介绍了通过HTTPS进行JerseyTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jersey测试框架测试Jersey过滤器,并且需要通过HTTPS来完成.

I'm trying to test a Jersey filter with the Jersey Test Framework and I need it to be done over HTTPS.

我知道如何在客户端上配置ssl上下文,但是我似乎找不到有关如何通过HTTPS运行Grizzly服务器的信息.

I know how to configure the ssl context on the Client but I can't seem to find info on how to run the Grizzly server over HTTPS.

测试:

@Test
public void testPeerTokenOK() {
    SSLContext sslContext = getSslContext();
    Client client = ClientBuilder.newBuilder().hostnameVerifier((s, session) -> true).sslContext(sslContext).build();

    WebTarget target = target().path(URI);

    Response response = client.target(target.getUri())
            .request(MediaType.APPLICATION_JSON)
            .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON + "; charset=" + StandardCharsets.UTF_8.name())

    assertEquals(Status.OK.getStatusCode(), response.getStatus());
}

资源:

@Path(URI)
public static class TestResource {

    @GET
    @Singleton
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response get(EntityPayLoad payload) throws Exception {
        if (payload != null && payload instanceof EntityPayLoad) {
            return Response.ok(payload).build();
        } else {
            return Response.status(Status.BAD_REQUEST.getStatusCode()).build();
        }
    }

}

构造函数:

@Override
protected Application configure() {
    ResourceConfig rc = new ResourceConfig();
    rc.register(SpringLifecycleListener.class);
    rc.register(RequestContextFilter.class);
    rc.register(new JacksonFeature());
    rc.register(new ObjectMapperContextResolver());

    rc.registerClasses(TestResource.class);
    rc.register(AccessTokenFilter.class);
    rc.register(PeerTokenFilter.class);

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("filterContext.xml");
    rc.property("contextConfig", applicationContext);
    return rc;
}

相关的Maven依赖项:

The relevant maven dependency:

<dependency>
   <groupId>org.glassfish.jersey.test-framework.providers</groupId>
   <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
   <version>2.25</version>
   <scope>test</scope>
</dependency>

推荐答案

在此处也提出并回答了这个问题:使用SSL配置JettyTestContainer for JerseyTest 从2.33版开始,可以使用自定义ssl配置来配置JerseyTest.参见此处的示例:

This question was also asked and answered here: Configure JettyTestContainer with SSL for JerseyTest From version 2.33 onwards it is possible to configure the JerseyTest with your custom ssl configuration. See here for an example:

public class SecuredJerseyTest extends JerseyTest {

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new GrizzlyTestContainerFactory();
    }

    @Path("hello")
    public static class TestResource {
        @GET
        public String hello() {
            return "hello";
        }
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(TestResource.class);
    }

    @Override
    protected URI getBaseUri() {
        return UriBuilder
                .fromUri("https://localhost")
                .port(getPort())
                .build();
    }

    @Override
    protected Optional<SSLContext> getSslContext() {
        SSLContext sslContext = ... // your initialised server sslContext 
        return Optional.of(sslContext);
    }

    @Override
    protected Optional<SSLParameters> getSslParameters() {
        serverSslParameters = new SSLParameters();
        serverSslParameters.setNeedClientAuth(false);
        return Optional.of(serverSslParameters);
    }

    @Test
    public void testHello() {
        SSLContext sslContext = ... // your initialised client sslContext 

        Client client = ClientBuilder.newBuilder()
                .sslContext(sslContext)
                .build();

        WebTarget target = client.target(getBaseUri()).path("hello");

        String s = target.request().get(String.class);
        Assert.assertEquals("hello", s);
    }
}

这篇关于通过HTTPS进行JerseyTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆