-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fixes and new tests for spark implementation - partly addressing #40
- Loading branch information
Buliani
committed
Jul 3, 2017
1 parent
dfa6689
commit 0e762c6
Showing
13 changed files
with
208 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ntainer-spark/src/test/java/com/amazonaws/serverless/proxy/spark/HelloWorldSparkTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.amazonaws.serverless.proxy.spark; | ||
|
||
|
||
import com.amazonaws.serverless.exceptions.ContainerInitializationException; | ||
import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest; | ||
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse; | ||
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; | ||
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
import static spark.Spark.get; | ||
|
||
|
||
public class HelloWorldSparkTest { | ||
private static final String CUSTOM_HEADER_KEY = "X-Custom-Header"; | ||
private static final String CUSTOM_HEADER_VALUE = "My Header Value"; | ||
private static final String BODY_TEXT_RESPONSE = "Hello World"; | ||
|
||
private static SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler; | ||
|
||
@Test | ||
public void basicServer_initialize() { | ||
try { | ||
handler = SparkLambdaContainerHandler.getAwsProxyHandler(); | ||
|
||
configureRoutes(); | ||
|
||
} catch (RuntimeException | ContainerInitializationException e) { | ||
e.printStackTrace(); | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void basicServer_handleRequest_emptyFilters() { | ||
AwsProxyRequest req = new AwsProxyRequestBuilder().method("GET").path("/hello").build(); | ||
AwsProxyResponse response = handler.proxy(req, new MockLambdaContext()); | ||
|
||
assertEquals(200, response.getStatusCode()); | ||
assertTrue(response.getHeaders().containsKey(CUSTOM_HEADER_KEY)); | ||
assertEquals(CUSTOM_HEADER_VALUE, response.getHeaders().get(CUSTOM_HEADER_KEY)); | ||
assertEquals(BODY_TEXT_RESPONSE, response.getBody()); | ||
} | ||
|
||
private void configureRoutes() { | ||
get("/hello", (req, res) -> { | ||
res.status(200); | ||
res.header(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE); | ||
return BODY_TEXT_RESPONSE; | ||
}); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...k/src/test/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.amazonaws.serverless.proxy.spark; | ||
|
||
|
||
import com.amazonaws.serverless.exceptions.ContainerInitializationException; | ||
import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest; | ||
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse; | ||
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; | ||
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; | ||
import com.amazonaws.serverless.proxy.spark.filter.CustomHeaderFilter; | ||
|
||
import org.junit.Test; | ||
|
||
import javax.servlet.DispatcherType; | ||
import javax.servlet.FilterRegistration; | ||
|
||
import java.util.EnumSet; | ||
|
||
import static org.junit.Assert.*; | ||
import static spark.Spark.get; | ||
|
||
|
||
public class SparkLambdaContainerHandlerTest { | ||
private static final String RESPONSE_BODY_TEXT = "hello"; | ||
|
||
@Test | ||
public void filters_onStartupMethod_executeFilters() { | ||
|
||
SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = null; | ||
try { | ||
handler = SparkLambdaContainerHandler.getAwsProxyHandler(); | ||
} catch (ContainerInitializationException e) { | ||
e.printStackTrace(); | ||
fail(); | ||
} | ||
|
||
handler.onStartup(c -> { | ||
if (c == null) { | ||
System.out.println("Null servlet context"); | ||
fail(); | ||
} | ||
FilterRegistration.Dynamic registration = c.addFilter("CustomHeaderFilter", CustomHeaderFilter.class); | ||
// update the registration to map to a path | ||
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); | ||
// servlet name mappings are disabled and will throw an exception | ||
}); | ||
|
||
configureRoutes(); | ||
|
||
AwsProxyRequest req = new AwsProxyRequestBuilder().method("GET").path("/header-filter").build(); | ||
AwsProxyResponse response = handler.proxy(req, new MockLambdaContext()); | ||
|
||
assertNotNull(response); | ||
assertEquals(200, response.getStatusCode()); | ||
assertTrue(response.getHeaders().containsKey(CustomHeaderFilter.HEADER_NAME)); | ||
assertEquals(CustomHeaderFilter.HEADER_VALUE, response.getHeaders().get(CustomHeaderFilter.HEADER_NAME)); | ||
assertEquals(RESPONSE_BODY_TEXT, response.getBody()); | ||
|
||
} | ||
|
||
private void configureRoutes() { | ||
get("/header-filter", (req, res) -> { | ||
res.status(200); | ||
return RESPONSE_BODY_TEXT; | ||
}); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...st/java/com/amazonaws/serverless/proxy/spark/embeddedserver/LambdaEmbeddedServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.amazonaws.serverless.proxy.spark.embeddedserver; | ||
|
||
|
||
import org.junit.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
|
||
public class LambdaEmbeddedServerTest { | ||
private static LambdaEmbeddedServer server = new LambdaEmbeddedServer(null, null, false); | ||
|
||
@Test | ||
public void webSocket_configureWebSocket_noException() { | ||
try { | ||
server.configureWebSockets(null, Optional.of(0)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
fail(); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...r-spark/src/test/java/com/amazonaws/serverless/proxy/spark/filter/CustomHeaderFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.amazonaws.serverless.proxy.spark.filter; | ||
|
||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
public class CustomHeaderFilter implements Filter { | ||
public static final String HEADER_NAME = "X-Filter-Header"; | ||
public static final String HEADER_VALUE = "CustomHeaderFilter"; | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
System.out.println("Called init on filter"); | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
System.out.println("Called doFilter"); | ||
HttpServletResponse resp = (HttpServletResponse)servletResponse; | ||
resp.addHeader(HEADER_NAME, HEADER_VALUE); | ||
|
||
filterChain.doFilter(servletRequest, servletResponse); | ||
} | ||
|
||
|
||
@Override | ||
public void destroy() { | ||
System.out.println("Called destroy"); | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
...er-spark/src/test/java/com/amazonaws/serverless/proxy/test/spark/HelloWorldSparkTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters