使用SSL配置JettyTestContainer进行JerseyTest

问题描述

我的任务是为我的团队的代码设置集成测试。这些测试将要求对Jersey 2.27中实现的REST端点执行HTTPS请求。在寻找如何执行这种测试的过程中,我偶然发现this article from Baeldung指出了泽西测试框架及其为此目的而实现的提供者容器。我选择了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端点。

我发现了this question,但它看起来不像是在服务器上使用的Grizzly SSL配置 JerseyTest方案。

我发现了this question,但看起来就像设置用户角色,而不是实际的SSL配置。

然后我找到了this question,问这个问题的人似乎也遇到了同样的问题,但是没有人回答这个问题。

请帮助!

解决方法

似乎此选项在JerseyTest中不可用。我创建了一个拉取请求以添加此选项。请在这里查看:https://github.com/eclipse-ee4j/jersey/pull/4573我不太确定他们批准和发布它的速度如何...

我知道这不是您要寻找的答案...但是,如果您仍想使用ssl配置服务器并使用https测试您的应用程序,而不必等到请求请求获得批准,您可以看看我的github项目,其中有一个在没有JerseyTest的情况下使用grizzly测试球衣服务器的基本示例:ApplicationIntegrationTest