-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Populate AWSProxyResponse headers with multi #1338
base: 5.0.x
Are you sure you want to change the base?
Changes from 3 commits
47cfaa4
6d35a76
50e072c
a771d57
b46f2ae
0f624cb
52b852e
9f7ff53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import com.amazonaws.serverless.proxy.internal.testutils.Timer; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyRequest; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse; | ||
import com.amazonaws.serverless.proxy.model.Headers; | ||
import com.amazonaws.services.lambda.runtime.Context; | ||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.io.Writable; | ||
|
@@ -38,6 +39,7 @@ | |
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
@@ -68,6 +70,7 @@ public AwsProxyResponse writeResponse( | |
Context lambdaContext) throws InvalidResponseObjectException { | ||
Timer.start(TIMER_NAME); | ||
AwsProxyResponse awsProxyResponse = containerResponse.getAwsResponse(); | ||
|
||
final Map<String, Cookie> cookies = containerResponse.getAllCookies(); | ||
if (CollectionUtils.isNotEmpty(cookies)) { | ||
final io.netty.handler.codec.http.cookie.Cookie[] nettyCookies = cookies.values().stream().filter(c -> c instanceof NettyCookie).map(c -> ((NettyCookie) c).getNettyCookie()).toArray(io.netty.handler.codec.http.cookie.Cookie[]::new); | ||
|
@@ -112,8 +115,20 @@ public AwsProxyResponse writeResponse( | |
status + " " + | ||
Response.Status.fromStatusCode(status.getCode()).getReasonPhrase()); | ||
} | ||
|
||
awsProxyResponse.setHeaders(getHeaders(awsProxyResponse)); | ||
Timer.stop(TIMER_NAME); | ||
return awsProxyResponse; | ||
} | ||
|
||
private static Map<String, String> getHeaders(AwsProxyResponse awsProxyResponse) { | ||
Headers multiValueHeaders = awsProxyResponse.getMultiValueHeaders(); | ||
Map<String, String> headers = new HashMap<>(); | ||
for (Map.Entry<String, List<String>> entry : multiValueHeaders.entrySet()) { | ||
List<String> headerValues = entry.getValue(); | ||
if (headerValues != null && headerValues.size() == 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check: No headers will be added if the suggestion: Try this instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resolved via 52b852e |
||
headers.put(entry.getKey(), headerValues.get(0)); | ||
} | ||
} | ||
return headers; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.micronaut.function.aws.proxy | ||
|
||
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder | ||
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext | ||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse | ||
import com.amazonaws.services.lambda.runtime.Context | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpHeaders | ||
import io.micronaut.http.HttpMethod | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Body | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Post | ||
import io.micronaut.http.annotation.Status | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Issue | ||
import spock.lang.PendingFeature | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class ContentTypeSpec extends Specification { | ||
@Shared | ||
@AutoCleanup | ||
MicronautLambdaContainerHandler handler = new MicronautLambdaContainerHandler( | ||
ApplicationContext.builder().properties([ | ||
'spec.name' : 'ContentTypeSpec', | ||
'micronaut.security.enabled': false, | ||
]) | ||
) | ||
@Shared | ||
Context lambdaContext = new MockLambdaContext() | ||
|
||
void "verify controllers return json by default and mulitvalue headers are populated"() { | ||
given: | ||
AwsProxyRequestBuilder builder = new AwsProxyRequestBuilder('/json/bydefault', HttpMethod.GET.toString()) | ||
builder.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) | ||
|
||
when: | ||
AwsProxyResponse response = handler.proxy(builder.build(), lambdaContext) | ||
|
||
then: | ||
response.statusCode == 200 | ||
response.body == '{"msg":"Hello world"}' | ||
|
||
and: 'works with multivalue headers' | ||
response.multiValueHeaders | ||
["application/json"] == response.getMultiValueHeaders().get("Content-Type") | ||
} | ||
|
||
@Issue("https://github.com/micronaut-projects/micronaut-aws/issues/1330") | ||
void "verify controllers return json by default and headers are populated"() { | ||
given: | ||
AwsProxyRequestBuilder builder = new AwsProxyRequestBuilder('/json/bydefault', HttpMethod.GET.toString()) | ||
builder.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) | ||
|
||
when: | ||
AwsProxyResponse response = handler.proxy(builder.build(), lambdaContext) | ||
|
||
then: | ||
response.statusCode == 200 | ||
response.body == '{"msg":"Hello world"}' | ||
|
||
response.headers | ||
"application/json" == response.getHeaders().get("Content-Type") | ||
} | ||
|
||
@Controller('/json') | ||
@Requires(property = 'spec.name', value = 'ContentTypeSpec') | ||
static class BodyController { | ||
|
||
@Get("/bydefault") | ||
@Status(HttpStatus.OK) | ||
Map<String, Object> index() { | ||
[msg: "Hello world"] | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use a linked hash map to retain order
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved via 0f624cb