Skip to content

Commit

Permalink
expose a new method to stop a "handler" (SimpleRequestHandler#stop) a…
Browse files Browse the repository at this point in the history
…nd do so on test tear down
  • Loading branch information
bbilger committed Nov 17, 2017
1 parent e2ddf81 commit 197b7b1
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.glassfish.jersey.internal.util.collection.Ref;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.spi.RequestScopedInitializer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -82,6 +83,11 @@ public void setup() {
gatewayHandler.start();
}

@After
public void tearDown() {
gatewayHandler.stop();
}

@SuppressWarnings("unchecked")
@Test
public void delegateRequest_ValidRequestAndReferencesGiven_ShouldSetReferencesOnRequestInitialization() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.filter.EncodingFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -107,6 +108,11 @@ public void setup() {
handler = createAndStartHandler(new ResourceConfig(), testService);
}

@After
public void tearDown() {
handler.stop();
}

private GatewayRequestObjectHandlerImpl createAndStartHandler(ResourceConfig config, TestService testService) {
config.register(GatewayFeature.class);
Binder binder = new InstanceBinder.Builder().addInstance(testService, TestService.class).build();
Expand Down Expand Up @@ -172,11 +178,16 @@ public void testGatewayRequestMemberInjection() {

@Test
public void testContainerFailureCreates500() {
GatewayRequestObjectHandlerImpl throwingHandler = spy(createAndStartHandler(new ResourceConfig(), testService));
DefaultGatewayRequest request = new DefaultGatewayRequest();
doThrow(new RuntimeException()).when(throwingHandler).createContainerRequest(any());
GatewayResponse response = throwingHandler.handleRequest(request, context);
assertEquals(new GatewayResponse(null, new HashMap<>(), Status.INTERNAL_SERVER_ERROR, false), response);
GatewayRequestObjectHandlerImpl throwingHandler = null;
try {
throwingHandler = spy(createAndStartHandler(new ResourceConfig(), testService));
DefaultGatewayRequest request = new DefaultGatewayRequest();
doThrow(new RuntimeException()).when(throwingHandler).createContainerRequest(any());
GatewayResponse response = throwingHandler.handleRequest(request, context);
assertEquals(new GatewayResponse(null, new HashMap<>(), Status.INTERNAL_SERVER_ERROR, false), response);
} finally {
throwingHandler.stop();
}
}

@Test
Expand Down Expand Up @@ -383,29 +394,37 @@ public void testBaseUriWithHost() {

@Test
public void testAppPathWithoutHost() {
GatewayRequestObjectHandlerImpl handlerWithAppPath = createAndStartHandler(new ApiResourceConfig(),
testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/api/uris") // = path
.build();
handlerWithAppPath.handleRequest(request, context);
verify(testService).baseUri(URI.create("/api/"));
verify(testService).requestUri(URI.create("/api/uris"));
GatewayRequestObjectHandlerImpl handlerWithAppPath = null;
try {
handlerWithAppPath = createAndStartHandler(new ApiResourceConfig(), testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/api/uris") // = path
.build();
handlerWithAppPath.handleRequest(request, context);
verify(testService).baseUri(URI.create("/api/"));
verify(testService).requestUri(URI.create("/api/uris"));
} finally {
handlerWithAppPath.stop();
}
}

@Test
public void testAppPathWithHost() {
GatewayRequestObjectHandlerImpl handlerWithAppPath = createAndStartHandler(new ApiResourceConfig(),
testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/api/uris") // = path
.domain("api.example.com")
.build();
handlerWithAppPath.handleRequest(request, context);
verify(testService).baseUri(URI.create("https://api.example.com/api/"));
verify(testService).requestUri(URI.create("https://api.example.com/api/uris"));
GatewayRequestObjectHandlerImpl handlerWithAppPath = null;
try {
handlerWithAppPath = createAndStartHandler(new ApiResourceConfig(), testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/api/uris") // = path
.domain("api.example.com")
.build();
handlerWithAppPath.handleRequest(request, context);
verify(testService).baseUri(URI.create("https://api.example.com/api/"));
verify(testService).requestUri(URI.create("https://api.example.com/api/uris"));
} finally {
handlerWithAppPath.stop();
}
}

@Test
Expand All @@ -425,51 +444,63 @@ public void testBasePathWithBasePathWithHost() {
public void testProxyBasePathingWithoutDomainWithoutPathBasePath() {
ResourceConfig config = new ResourceConfig();
config.register(DynamicProxyBasePathFilter.class);
GatewayRequestObjectHandlerImpl handlerWithProxyFilter = createAndStartHandler(config,
testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/a/{proxy+}")
.pathParams(Collections.singletonMap("proxy", "uris"))
.build();
handlerWithProxyFilter.handleRequest(request, context);
verify(testService).baseUri(URI.create("/a/"));
verify(testService).requestUri(URI.create("/a/uris"));
GatewayRequestObjectHandlerImpl handlerWithProxyFilter = null;
try {
handlerWithProxyFilter = createAndStartHandler(config, testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/a/{proxy+}")
.pathParams(Collections.singletonMap("proxy", "uris"))
.build();
handlerWithProxyFilter.handleRequest(request, context);
verify(testService).baseUri(URI.create("/a/"));
verify(testService).requestUri(URI.create("/a/uris"));
} finally {
handlerWithProxyFilter.stop();
}
}

@Test
public void testProxyBasePathingWithDomainWithoutPathBasePath() {
ResourceConfig config = new ResourceConfig();
config.register(DynamicProxyBasePathFilter.class);
GatewayRequestObjectHandlerImpl handlerWithProxyFilter = createAndStartHandler(config,
testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/a/{proxy+}")
.pathParams(Collections.singletonMap("proxy", "uris"))
.domain("api.example.com")
.build();
handlerWithProxyFilter.handleRequest(request, context);
verify(testService).baseUri(URI.create("https://api.example.com/a/"));
verify(testService).requestUri(URI.create("https://api.example.com/a/uris"));
GatewayRequestObjectHandlerImpl handlerWithProxyFilter = null;
try {
handlerWithProxyFilter = createAndStartHandler(config, testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/a/{proxy+}")
.pathParams(Collections.singletonMap("proxy", "uris"))
.domain("api.example.com")
.build();
handlerWithProxyFilter.handleRequest(request, context);
verify(testService).baseUri(URI.create("https://api.example.com/a/"));
verify(testService).requestUri(URI.create("https://api.example.com/a/uris"));
} finally {
handlerWithProxyFilter.stop();
}
}

@Test
public void testProxyBasePathingWithDomainWithPathBasePath() {
ResourceConfig config = new ResourceConfig();
config.register(DynamicProxyBasePathFilter.class);
GatewayRequestObjectHandlerImpl handlerWithProxyFilter = createAndStartHandler(config,
testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/p/{proxy+}")
.pathParams(Collections.singletonMap("proxy", "uris"))
.path("/a/p/uris")
.domain("api.example.com")
.build();
handlerWithProxyFilter.handleRequest(request, context);
verify(testService).baseUri(URI.create("https://api.example.com/a/p/"));
verify(testService).requestUri(URI.create("https://api.example.com/a/p/uris"));
GatewayRequestObjectHandlerImpl handlerWithProxyFilter = null;
try {
handlerWithProxyFilter = createAndStartHandler(config, testService);
DefaultGatewayRequest request = new DefaultGatewayRequestBuilder()
.httpMethod("GET")
.resource("/p/{proxy+}")
.pathParams(Collections.singletonMap("proxy", "uris"))
.path("/a/p/uris")
.domain("api.example.com")
.build();
handlerWithProxyFilter.handleRequest(request, context);
verify(testService).baseUri(URI.create("https://api.example.com/a/p/"));
verify(testService).requestUri(URI.create("https://api.example.com/a/p/uris"));
} finally {
handlerWithProxyFilter.stop();
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.glassfish.jersey.internal.util.collection.Ref;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.spi.RequestScopedInitializer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -55,6 +56,11 @@ public void setup() {
serviceHandler.start();
}

@After
public void tearDown() {
serviceHandler.stop();
}

@SuppressWarnings("unchecked")
@Test
public void delegateRequest_ValidRequestAndReferencesGiven_ShouldSetReferencesOnRequestInitialization() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.glassfish.jersey.internal.inject.Binder;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -64,6 +65,11 @@ public void setup() {
handler.start();
}

@After
public void tearDown() {
handler.stop();
}

@Test
public void testLambdaContextInjection() {
DefaultServiceRequest request = new DefaultServiceRequest(null, new HashMap<>(), URI.create("/"), "DELETE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.glassfish.jersey.internal.inject.Binder;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
Expand Down Expand Up @@ -49,6 +50,11 @@ public void setup() {
handler.start();
}

@After
public void tearDown() {
handler.stop();
}

@Test
public void testLambdaContextInjection() {
SNSEvent snsEvent = createSnsEvent("inject-lambda-context");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.glassfish.jersey.internal.util.collection.Ref;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.spi.RequestScopedInitializer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -60,6 +61,11 @@ public void setup() {
snsHandler.start();
}

@After
public void tearDown() {
snsHandler.stop();
}

@SuppressWarnings("unchecked")
@Test
public void delegateRequest_ValidRequestAndReferencesGiven_ShouldSetReferencesOnRequestInitialization() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ public final void start() {
started = true;
}

/**
* Stops the container.
* <p>
* May be called once, only.
* <p>
* {@link #start()} must be called, first.
*/
public final void stop() {
checkState(started, "container has already been stopped");
container.onShutdown();
started = false;
}

/**
* Handles the request by passing it to the container and so Jersey.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import org.glassfish.jersey.internal.inject.Binder;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -84,6 +85,11 @@ public void setup() {
handler.start();
}

@After
public void tearDown() {
handler.stop();
}

private JRestlessContainerRequest createRequest(String requestUri, String httpMethod, String body, Map<String, List<String>> headers) {
InputStream is = EMPTY_ENTITY_STREAM;
if (body != null) {
Expand Down
Loading

0 comments on commit 197b7b1

Please sign in to comment.