Skip to content

Commit 54104b4

Browse files
committed
More unit tests and final fixes
Changed the ContainerConfig property to be static
1 parent 0c7e579 commit 54104b4

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/LambdaContainerHandler.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public abstract class LambdaContainerHandler<RequestType, ResponseType, Containe
3737

3838
public static final String SERVER_INFO = "aws-serverless-java-container";
3939

40+
4041
//-------------------------------------------------------------
4142
// Variables - Private
4243
//-------------------------------------------------------------
@@ -48,7 +49,12 @@ public abstract class LambdaContainerHandler<RequestType, ResponseType, Containe
4849

4950
protected Context lambdaContext;
5051

51-
private ContainerConfig config = ContainerConfig.defaultConfig();
52+
53+
//-------------------------------------------------------------
54+
// Variables - Private - Static
55+
//-------------------------------------------------------------
56+
57+
private static ContainerConfig config = ContainerConfig.defaultConfig();
5258

5359

5460
//-------------------------------------------------------------
@@ -81,11 +87,19 @@ protected abstract void handleRequest(ContainerRequestType containerRequest, Con
8187
// Methods - Public
8288
//-------------------------------------------------------------
8389

90+
/**
91+
* Configures the library to strip a base path from incoming requests before passing them on to the wrapped
92+
* framework. This was added in response to issue #34 (https://github.com/awslabs/aws-serverless-java-container/issues/34).
93+
* When creating a base path mapping for custom domain names in API Gateway we want to be able to strip the base path
94+
* from the request - the underlying service may not recognize this path.
95+
* @param basePath The base path to be stripped from the request
96+
*/
8497
public void stripBasePath(String basePath) {
8598
config.setStripBasePath(true);
8699
config.setServiceBasePath(basePath);
87100
}
88101

102+
89103
/**
90104
* Proxies requests to the underlying container given the incoming Lambda request. This method returns a populated
91105
* return object for the Lambda function.
@@ -114,4 +128,17 @@ public ResponseType proxy(RequestType request, Context context) {
114128
return exceptionHandler.handle(e);
115129
}
116130
}
131+
132+
133+
//-------------------------------------------------------------
134+
// Methods - Getter/Setter
135+
//-------------------------------------------------------------
136+
137+
/**
138+
* Returns the current container configuration object.
139+
* @return
140+
*/
141+
public static ContainerConfig getContainerConfig() {
142+
return config;
143+
}
117144
}

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/RequestReaderTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,25 @@ public void requestReader_stripBasePath() {
5353
assertNotNull(finalPath);
5454
assertEquals(ORDERS_URL, finalPath);
5555
}
56+
57+
@Test
58+
public void requestReader_doubleBasePath() {
59+
ContainerConfig config = ContainerConfig.defaultConfig();
60+
config.setStripBasePath(true);
61+
config.setServiceBasePath(BASE_PATH_MAPPING);
62+
63+
String finalPath = requestReader.stripBasePath("/" + BASE_PATH_MAPPING + "/" + BASE_PATH_MAPPING, config);
64+
assertNotNull(finalPath);
65+
assertEquals("/" + BASE_PATH_MAPPING, finalPath);
66+
67+
finalPath = requestReader.stripBasePath("/custom/" + BASE_PATH_MAPPING, config);
68+
assertNotNull(finalPath);
69+
assertEquals("/custom/" + BASE_PATH_MAPPING, finalPath);
70+
71+
finalPath = requestReader.stripBasePath(BASE_PATH_MAPPING, config);
72+
assertNotNull(finalPath);
73+
// the request path does not start with a "/", the comparison in the method should fail
74+
// and nothing should get replaced
75+
assertEquals(BASE_PATH_MAPPING, finalPath);
76+
}
5677
}

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletRequestReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class AwsProxyHttpServletRequestReaderTest {
2323

2424
@Test
2525
public void readRequest_reflection_returnType() throws NoSuchMethodException {
26-
Method readRequestMethod = AwsProxyHttpServletRequestReader.class.getMethod("readRequest", AwsProxyRequest.class, SecurityContext.class, Context.class);
26+
Method readRequestMethod = AwsProxyHttpServletRequestReader.class.getMethod("readRequest", AwsProxyRequest.class, SecurityContext.class, Context.class, ContainerConfig.class);
2727

2828
assertTrue(readRequestMethod.getReturnType() == AwsProxyHttpServletRequest.class);
2929
}

aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/factory/AwsProxyServletRequestFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515

1616
import com.amazonaws.serverless.exceptions.InvalidRequestEventException;
1717
import com.amazonaws.serverless.proxy.internal.AwsProxySecurityContextWriter;
18+
import com.amazonaws.serverless.proxy.internal.model.ContainerConfig;
1819
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest;
1920
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader;
2021
import com.amazonaws.serverless.proxy.internal.servlet.AwsServletContext;
2122
import com.amazonaws.serverless.proxy.jersey.JerseyAwsProxyRequestReader;
23+
import com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler;
2224

2325
import org.glassfish.hk2.api.Factory;
2426

@@ -64,8 +66,9 @@ public void dispose(HttpServletRequest httpServletRequest) {
6466
public static HttpServletRequest getRequest() {
6567
try {
6668
AwsProxyHttpServletRequest req = requestReader.readRequest(JerseyAwsProxyRequestReader.getCurrentRequest(),
67-
AwsProxySecurityContextWriter.getCurrentContext(),
68-
JerseyAwsProxyRequestReader.getCurrentLambdaContext());
69+
AwsProxySecurityContextWriter.getCurrentContext(),
70+
JerseyAwsProxyRequestReader.getCurrentLambdaContext(),
71+
JerseyLambdaContainerHandler.getContainerConfig());
6972
req.setServletContext(new AwsServletContext(JerseyAwsProxyRequestReader.getCurrentLambdaContext(), null));
7073
return req;
7174
} catch (InvalidRequestEventException e) {

0 commit comments

Comments
 (0)