我正在尝试使用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>
答案 0 :(得分:1)
这里也有人提出并回答了这个问题: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);
}
}