Skip to content
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

Backport CONVERT_TO_TEXT for OPTIONS #432

Merged
merged 2 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ private static ObjectNode createPreflightIntegration(Map<CorsHeader, String> hea
}

MockIntegrationTrait.Builder integration = MockIntegrationTrait.builder()
// See https://forums.aws.amazon.com/thread.jspa?threadID=256140
.contentHandling("CONVERT_TO_TEXT")
.passThroughBehavior("when_no_match")
.putResponse("default", responseBuilder.build())
.putRequestTemplate(API_GATEWAY_DEFAULT_ACCEPT_VALUE, PREFLIGHT_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
"security": [],
"x-amazon-apigateway-integration": {
"passThroughBehavior": "when_no_match",
"contentHandling": "CONVERT_TO_TEXT",
"requestTemplates": {
"application/json": "{\"statusCode\":200}",
"application/octet-stream": "{\"statusCode\":200}"
Expand Down Expand Up @@ -340,6 +341,7 @@
"security": [],
"x-amazon-apigateway-integration": {
"passThroughBehavior": "when_no_match",
"contentHandling": "CONVERT_TO_TEXT",
"requestTemplates": {
"application/json": "{\"statusCode\":200}"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ protected ObjectNode createNode() {

@Override
public Builder toBuilder() {
Builder builder = builder()
return builder()
.type(type)
.uri(uri)
.credentials(credentials)
Expand All @@ -419,12 +419,11 @@ public Builder toBuilder() {
.timeoutInMillis(timeoutInMillis)
.connectionId(connectionId)
.connectionType(connectionType)
.cacheNamespace(cacheNamespace);
cacheKeyParameters.forEach(builder::addCacheKeyParameter);
requestParameters.forEach(builder::putRequestParameter);
requestTemplates.forEach(builder::putRequestTemplate);
responses.forEach(builder::putResponse);
return builder;
.cacheNamespace(cacheNamespace)
.requestParameters(requestParameters)
.requestTemplates(requestTemplates)
.responses(responses)
.cacheKeyParameters(cacheKeyParameters);
}

public static final class Builder extends AbstractTraitBuilder<IntegrationTrait, Builder> {
Expand Down Expand Up @@ -515,7 +514,7 @@ public Builder passThroughBehavior(String passThroughBehavior) {
* utf-8-encoded string or passing through the text payload natively
* without modification</li>
* <li>CONVERT_TO_BINARY, for converting a text payload into
* Base64-decoded blobor passing through a binary payload natively
* Base64-decoded blob or passing through a binary payload natively
* without modification.</li>
* </ul>
*
Expand Down Expand Up @@ -585,6 +584,18 @@ public Builder addCacheKeyParameter(String cacheKeyParameter) {
return this;
}

/**
* Sets a list of request parameters whose values are to be cached.
*
* @param cacheKeyParameters Parameters to use in the cache.
* @return Returns the builder.
*/
public Builder cacheKeyParameters(List<String> cacheKeyParameters) {
this.cacheKeyParameters.clear();
this.cacheKeyParameters.addAll(cacheKeyParameters);
return this;
}

/**
* Removes a specific cache key parameter.
*
Expand All @@ -607,7 +618,7 @@ public Builder clearCacheKeyParameters() {
}

/**
* Adds a request template.
* Adds a request parameter.
*
* @param input Input request expression.
* @param output Output request expression.
Expand All @@ -619,6 +630,19 @@ public Builder putRequestParameter(String input, String output) {
return this;
}

/**
* Sets request parameters.
*
* @param requestParameters Map of parameters to add.
* @return Returns the builder.
* @see IntegrationTrait#getAllRequestParameters()
*/
public Builder requestParameters(Map<String, String> requestParameters) {
this.requestParameters.clear();
this.requestParameters.putAll(requestParameters);
return this;
}

/**
* Remove a request parameter by expression.
*
Expand All @@ -643,6 +667,19 @@ public Builder putRequestTemplate(String mimeType, String template) {
return this;
}

/**
* Sets request templates.
*
* @param requestTemplates Map of MIME types to the corresponding template.
* @return Returns the builder.
* @see IntegrationTrait#getAllRequestTemplates()
*/
public Builder requestTemplates(Map<String, String> requestTemplates) {
this.requestTemplates.clear();
this.requestTemplates.putAll(requestTemplates);
return this;
}

/**
* Removes a request template by MIME type.
*
Expand All @@ -667,6 +704,19 @@ public Builder putResponse(String statusCodeRegex, IntegrationResponse integrati
return this;
}

/**
* Sets responses for the given response regular expressions.
*
* @param responses Map of regular expressions to responses.
* @return Returns the builder.
* @see IntegrationTrait#getAllResponses()
*/
public Builder responses(Map<String, IntegrationResponse> responses) {
this.responses.clear();
this.responses.putAll(responses);
return this;
}

/**
* Removes a response by status code regex.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,23 @@ public final class MockIntegrationTrait extends AbstractTrait implements ToSmith
public static final ShapeId ID = ShapeId.from("aws.apigateway#mockIntegration");

private static final String PASS_THROUGH_BEHAVIOR_KEY = "passThroughBehavior";
private static final String CONTENT_HANDLING_KEY = "contentHandling";
private static final String REQUEST_PARAMETERS_KEY = "requestParameters";
private static final String REQUEST_TEMPLATES_KEY = "requestTemplates";
private static final String RESPONSES_KEY = "responses";
private static final Set<String> KEYS = SetUtils.of(
PASS_THROUGH_BEHAVIOR_KEY, REQUEST_PARAMETERS_KEY, REQUEST_TEMPLATES_KEY, RESPONSES_KEY);

private final String passThroughBehavior;
private final String contentHandling;
private final Map<String, String> requestParameters;
private final Map<String, String> requestTemplates;
private final Map<String, IntegrationResponse> responses;

private MockIntegrationTrait(Builder builder) {
super(ID, builder.getSourceLocation());
passThroughBehavior = builder.passThroughBehavior;
contentHandling = builder.contentHandling;
requestParameters = MapUtils.copyOf(builder.requestParameters);
requestTemplates = MapUtils.copyOf(builder.requestTemplates);
responses = MapUtils.copyOf(builder.responses);
Expand All @@ -70,6 +73,9 @@ public Trait createTrait(ShapeId target, Node value) {
node.getStringMember(PASS_THROUGH_BEHAVIOR_KEY)
.map(StringNode::getValue)
.ifPresent(builder::passThroughBehavior);
node.getStringMember(CONTENT_HANDLING_KEY)
.map(StringNode::getValue)
.ifPresent(builder::contentHandling);
node.getObjectMember(REQUEST_PARAMETERS_KEY)
.map(ObjectNode::getMembers)
.ifPresent(members -> members.forEach(
Expand Down Expand Up @@ -169,10 +175,20 @@ public Optional<IntegrationResponse> getResponse(String statusCode) {
return Optional.ofNullable(responses.get(statusCode));
}

/**
* Gets the contentHandling property of the integration.
*
* @return Returns the contentHandling property.
*/
public Optional<String> getContentHandling() {
return Optional.ofNullable(contentHandling);
}

@Override
protected ObjectNode createNode() {
return Node.objectNode()
.withOptionalMember(PASS_THROUGH_BEHAVIOR_KEY, getPassThroughBehavior().map(Node::from))
.withOptionalMember(CONTENT_HANDLING_KEY, getContentHandling().map(Node::from))
.withOptionalMember(REQUEST_PARAMETERS_KEY, requestParameters.size() > 0
? Optional.of(ObjectNode.fromStringMap(requestParameters))
: Optional.empty())
Expand All @@ -187,15 +203,17 @@ protected ObjectNode createNode() {

@Override
public Builder toBuilder() {
Builder builder = builder().passThroughBehavior(passThroughBehavior);
requestParameters.forEach(builder::putRequestParameter);
requestTemplates.forEach(builder::putRequestTemplate);
responses.forEach(builder::putResponse);
return builder;
return builder()
.passThroughBehavior(passThroughBehavior)
.contentHandling(contentHandling)
.requestParameters(requestParameters)
.requestTemplates(requestTemplates)
.responses(responses);
}

public static final class Builder extends AbstractTraitBuilder<MockIntegrationTrait, Builder> {
private String passThroughBehavior;
private String contentHandling;
private final Map<String, String> requestParameters = new HashMap<>();
private final Map<String, String> requestTemplates = new HashMap<>();
private final Map<String, IntegrationResponse> responses = new HashMap<>();
Expand All @@ -218,7 +236,30 @@ public Builder passThroughBehavior(String passThroughBehavior) {
}

/**
* Adds a request template.
* Set the Request payload encoding conversion types.
*
* <p>Valid values are:
*
* <ul>
* <li>CONVERT_TO_TEXT, for converting a binary payload into a
* Base64-encoded string or converting a text payload into a
* utf-8-encoded string or passing through the text payload natively
* without modification</li>
* <li>CONVERT_TO_BINARY, for converting a text payload into
* Base64-decoded blob or passing through a binary payload natively
* without modification.</li>
* </ul>
*
* @param contentHandling Content handling property.
* @return Returns the builder.
*/
public Builder contentHandling(String contentHandling) {
this.contentHandling = contentHandling;
return this;
}

/**
* Adds a request parameters.
*
* @param input Input request expression.
* @param output Output request expression.
Expand All @@ -230,6 +271,19 @@ public Builder putRequestParameter(String input, String output) {
return this;
}

/**
* Sets request parameters.
*
* @param requestParameters Map of parameters to add.
* @return Returns the builder.
* @see IntegrationTrait#getAllRequestParameters()
*/
public Builder requestParameters(Map<String, String> requestParameters) {
this.requestParameters.clear();
this.requestParameters.putAll(requestParameters);
return this;
}

/**
* Remove a request parameter by expression.
*
Expand All @@ -254,6 +308,19 @@ public Builder putRequestTemplate(String mimeType, String template) {
return this;
}

/**
* Sets request templates.
*
* @param requestTemplates Map of MIME types to the corresponding template.
* @return Returns the builder.
* @see IntegrationTrait#getAllRequestTemplates()
*/
public Builder requestTemplates(Map<String, String> requestTemplates) {
this.requestTemplates.clear();
this.requestTemplates.putAll(requestTemplates);
return this;
}

/**
* Removes a request template by MIME type.
*
Expand All @@ -278,6 +345,19 @@ public Builder putResponse(String statusCodeRegex, IntegrationResponse integrati
return this;
}

/**
* Sets responses for the given response regular expressions.
*
* @param responses Map of regular expressions to responses.
* @return Returns the builder.
* @see IntegrationTrait#getAllResponses()
*/
public Builder responses(Map<String, IntegrationResponse> responses) {
this.responses.clear();
this.responses.putAll(responses);
return this;
}

/**
* Removes a response by status code regex.
*
Expand Down