Search code examples
javahttpsjava-8jerseyjersey-test-framework

JerseyTest over HTTPS


I'm trying to test a Jersey filter with the Jersey Test Framework and I need it to be done over HTTPS.

I know how to configure the ssl context on the Client but I can't seem to find info on how to run the Grizzly server over HTTPS.

The test:

@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());
}

The resource:

@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();
        }
    }

}

The constructor:

@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;
}

The relevant maven dependency:

<dependency>
   <groupId>org.glassfish.jersey.test-framework.providers</groupId>
   <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
   <version>2.25</version>
   <scope>test</scope>
</dependency>

Solution

  • This question was also asked and answered here: Configure JettyTestContainer with SSL for JerseyTest From version 2.33 onwards it is possible to configure the JerseyTest with your custom ssl configuration. See here for an example:

    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);
        }
    }