我的任务是为我的团队的代码设置集成测试。这些测试需要向 Jersey 2.27 中实现的 REST 端点执行 HTTPS 请求。在我寻找如何进行此类测试的过程中,我偶然发现这篇文章来自Baeldung https://www.baeldung.com/jersey-test这让我了解了 Jersey 测试框架以及他们为此目的实现的提供程序容器。我选择了 Jetty 容器,因为我们的代码使用“自己部署”的 Jetty 实例来完成这一切。我开始实施我们的测试,但在为 JettyTestContainer 配置 SSL 来提供其余请求时遇到了困难。我开始收到 IllegalArgumentException,指出“不使用 SSL 时,URI 方案应为 http”。我不知道如何继续。
这是我的测试课:
public class ProjectTest extends JerseyTest {
private SSLContext sslCtx;
@Override
protected Application configure() {
ResourceConfig conf = new ResourceConfig(Sets.newHashSet(RestSuite1.class));
SslConfigurator sslConfig = SslConfigurator.newInstance()
.trustStoreFile()
.trustStorePassword()
.keyStoreFile()
.keyStorePassword()
sslCtx = sslConfig.createSSLContext();
}
@Override
public URI getBaseUri() {
return URI.create("https://localhost:1234")
}
@Override
public Client getClient() {
return ClientBuilder.newBuilder().sslContext(sslCtx).build();
}
@Test
public void test1() throws Exception {
String testString = "HelloWorld";
OurObject oo = target("/restSuite1")
.request()
.post(Entity.entity(testString, MediaType.APPLICATION_JSON), String.class);
}
}
这是一个 RestSuite1:
@Path("restSuite1")
@Produces("application/json")
public class RestSuite1 {
@POST
@Consumes("application/json")
@Produces("application/json")
public String doSomething(String payload) {
// Do Something
}
}
这是我的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>my-application1</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>my war</name>
<url>http://some-url.com</url>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>2.27</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<version>2.27</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
我应该注意到,在尝试 SSL 配置之前,我能够成功到达 POST /restSuite1 端点。
I found 这个问题 https://stackoverflow.com/questions/33831480/how-to-setup-basic-jersey-grizzly-2-21-ssl-startup-configuration,但它看起来不像人们在网络中使用的 Grizzly SSL 配置
泽西岛测试场景。
I found 这个问题 https://stackoverflow.com/questions/22067657/configuring-jersey-test-framework-with-security,但它看起来像是设置用户角色而不是实际的 SSL 配置。
然后我发现这个问题 https://stackoverflow.com/questions/54463147/jerseytest-over-https,问这个问题的人似乎和我有同样的问题,但没有人回答这个问题。
请帮忙!