问题
I've been tasked with setting up integration tests for my team's code. These tests will require performing HTTPS requests to REST endpoints implemented in Jersey 2.27. In my search for how to perform this kind of test, I stumbled upon this article from Baeldung that pointed me to the Jersey Test Framework and the provider containers they've implemented for this purpose. I chose the Jetty container, as our code was using a 'roll your own' Jetty instance to do all this. I began the implementation of our tests, and ran into difficulty configuring SSL for the JettyTestContainer that would serve up the rest requests. I began receiving an IllegalArgumentException stating that "The URI scheme should be http when not using SSL." I'm at a loss for how to proceed.
Here's my test class:
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);
}
}
Here's a RestSuite1:
@Path("restSuite1")
@Produces("application/json")
public class RestSuite1 {
@POST
@Consumes("application/json")
@Produces("application/json")
public String doSomething(String payload) {
// Do Something
}
}
Here's my 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>
I should note that prior to attempting the SSL configuration, I was able to reach the POST /restSuite1 end point successfully.
I found this question, but it doesn't look like a Grizzly SSL configuration one would use in a JerseyTest scenario.
I found this question, but it looks like setting user roles and not an actual SSL configuration.
I then found this question, and the person that asked it seems to have the same problem I do, however no one ever answered the question.
Please help!
回答1:
It looks like this option is not available within JerseyTest. I created a pull request to add this option. Please have a look here: https://github.com/eclipse-ee4j/jersey/pull/4573 I am not quite sure how fast they will approve and publish it...
I know it was not the answer you are looking for... However if you still want to configure your server with ssl and test your application with https without waiting till the pull request got approved, you can have a look at my github project which has a basic example of testing a jersey server with grizzly without JerseyTest: Application & IntegrationTest
=============== UPDATE #1 20-12-2020 ===============
The pull request has been approved, merged and released. It is now available starting from version 2.33. It is now possible to provide a sslcontext with sslparameters to configure your jersey test. An example setup would be:
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://stackoverflow.com/questions/63924572/configure-jettytestcontainer-with-ssl-for-jerseytest