-
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.
Added unit test to validate the proposed solution for #111 and warnin…
…g log messages in request object when a framework tries to access the session
- Loading branch information
Showing
6 changed files
with
196 additions
and
0 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
43 changes: 43 additions & 0 deletions
43
...ntainer-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringBootAppTest.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,43 @@ | ||
package com.amazonaws.serverless.proxy.spring; | ||
|
||
|
||
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; | ||
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyRequest; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse; | ||
import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel; | ||
import com.amazonaws.serverless.proxy.spring.springbootapp.LambdaHandler; | ||
import com.amazonaws.serverless.proxy.spring.springbootapp.TestController; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
|
||
public class SpringBootAppTest { | ||
private LambdaHandler handler = new LambdaHandler(); | ||
private MockLambdaContext context = new MockLambdaContext(); | ||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Test | ||
public void testMethod_springSecurity_doesNotThrowException() { | ||
AwsProxyRequest req = new AwsProxyRequestBuilder("/test", "GET").build(); | ||
AwsProxyResponse resp = handler.handleRequest(req, context); | ||
assertNotNull(resp); | ||
validateSingleValueModel(resp, TestController.TEST_VALUE); | ||
} | ||
|
||
private void validateSingleValueModel(AwsProxyResponse output, String value) { | ||
try { | ||
SingleValueModel response = mapper.readValue(output.getBody(), SingleValueModel.class); | ||
assertNotNull(response.getValue()); | ||
assertEquals(value, response.getValue()); | ||
} catch (IOException e) { | ||
fail("Exception while parsing response body: " + e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ring/src/test/java/com/amazonaws/serverless/proxy/spring/springbootapp/LambdaHandler.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,37 @@ | ||
package com.amazonaws.serverless.proxy.spring.springbootapp; | ||
|
||
|
||
import com.amazonaws.serverless.exceptions.ContainerInitializationException; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyRequest; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse; | ||
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler; | ||
import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler; | ||
import com.amazonaws.serverless.proxy.spring.springbootapp.TestApplication; | ||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
|
||
import org.springframework.web.context.support.XmlWebApplicationContext; | ||
|
||
|
||
public class LambdaHandler | ||
implements RequestHandler<AwsProxyRequest, AwsProxyResponse> | ||
{ | ||
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler; | ||
boolean isinitialized = false; | ||
|
||
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) | ||
{ | ||
if (!isinitialized) { | ||
isinitialized = true; | ||
try { | ||
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(TestApplication.class); | ||
} catch (ContainerInitializationException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
AwsProxyResponse res = handler.proxy(awsProxyRequest, context); | ||
return res; | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
...ng/src/test/java/com/amazonaws/serverless/proxy/spring/springbootapp/TestApplication.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,12 @@ | ||
package com.amazonaws.serverless.proxy.spring.springbootapp; | ||
|
||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.web.support.SpringBootServletInitializer; | ||
import org.springframework.context.annotation.ComponentScan; | ||
|
||
|
||
@SpringBootApplication | ||
@ComponentScan(basePackages = "com.amazonaws.serverless.proxy.spring.springbootapp") | ||
public class TestApplication extends SpringBootServletInitializer { | ||
} |
30 changes: 30 additions & 0 deletions
30
...ing/src/test/java/com/amazonaws/serverless/proxy/spring/springbootapp/TestController.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,30 @@ | ||
package com.amazonaws.serverless.proxy.spring.springbootapp; | ||
|
||
|
||
import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel; | ||
|
||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@RestController | ||
@EnableWebSecurity | ||
public class TestController extends WebSecurityConfigurerAdapter{ | ||
public static final String TEST_VALUE = "test"; | ||
|
||
@RequestMapping(path = "/test", method = { RequestMethod.GET }) | ||
public SingleValueModel testGet() { | ||
SingleValueModel value = new SingleValueModel(); | ||
value.setValue(TEST_VALUE); | ||
return value; | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http.sessionManagement().disable(); | ||
} | ||
} |