通过 HTTPS 的 JerseyTest
JerseyTest over HTTPS
我正在尝试使用 Jersey 测试框架测试 Jersey 过滤器,我需要它通过 HTTPS 完成。
我知道如何在客户端上配置 ssl 上下文,但我似乎无法找到有关如何通过 HTTPS 运行 Grizzly 服务器的信息。
测试:
@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依赖:
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.25</version>
<scope>test</scope>
</dependency>
这个问题也在此处提出和回答:Configure JettyTestContainer with SSL for JerseyTest从 2.33 版开始,可以使用您的自定义 ssl 配置来配置 JerseyTest。示例见此处:
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);
}
}
我正在尝试使用 Jersey 测试框架测试 Jersey 过滤器,我需要它通过 HTTPS 完成。
我知道如何在客户端上配置 ssl 上下文,但我似乎无法找到有关如何通过 HTTPS 运行 Grizzly 服务器的信息。
测试:
@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依赖:
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.25</version>
<scope>test</scope>
</dependency>
这个问题也在此处提出和回答:Configure JettyTestContainer with SSL for JerseyTest从 2.33 版开始,可以使用您的自定义 ssl 配置来配置 JerseyTest。示例见此处:
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);
}
}