mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sfn): encode HTTP Task object bodies as form data (#6010)
* feat(sfn): encode HTTP Task object bodies as form data * refactor(sfn): simplify HTTP Task payload encoding Reuse the shared RFC 3986 URI encoder and remove the unused form payload branch while preserving buffer and no-body requests.
This commit is contained in:
1 parent
87aa6f38a1
commit
f8ab55f319
5 files changed
+246
-14
No files matched your search
+64
-4
@@ -21,6 +21,9 @@ import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
@@ -146,7 +149,7 @@ class AslExecutorHttpInvokeTest {
|
||||
.endAndForget(Buffer.newInstance(JsonObject.of("ok", "false").toBuffer()))
|
||||
);
|
||||
server.requestHandler(router)
|
||||
.listenAndAwait(0);
|
||||
.listenAndAwait(0, "127.0.0.1");
|
||||
baseUrl = "http://127.0.0.1:" + server.actualPort();
|
||||
|
||||
executor = AslExecutorFixture.builder()
|
||||
@@ -428,7 +431,7 @@ class AslExecutorHttpInvokeTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void formTransformationIsUnsupported() {
|
||||
void unknownBodyTransformationFailsBeforeSending() {
|
||||
Execution execution = run("""
|
||||
{
|
||||
"StartAt": "CallHttp",
|
||||
@@ -444,7 +447,7 @@ class AslExecutorHttpInvokeTest {
|
||||
"ConnectionArn": "%s"
|
||||
},
|
||||
"Transform": {
|
||||
"RequestBodyEncoding": "URL_ENCODED",
|
||||
"RequestBodyEncoding": "INVALID",
|
||||
"RequestEncodingOptions": {
|
||||
"ArrayFormat": "COMMAS"
|
||||
}
|
||||
@@ -464,7 +467,64 @@ class AslExecutorHttpInvokeTest {
|
||||
|
||||
assertEquals("FAILED", execution.getStatus());
|
||||
assertEquals("States.TaskFailed", execution.getError());
|
||||
assertThat(execution.getCause(), startsWith("URL-encoded request bodies are not supported yet"));
|
||||
assertThat(execution.getCause(), startsWith("Unsupported body transformer: INVALID"));
|
||||
assertEquals(0, receivedRequests.size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
"INDICES | colors%5B0%5D=red%20blue&colors%5B1%5D=a%26b&colors%5B2%5D=caf%C3%A9",
|
||||
"REPEAT | colors=red%20blue&colors=a%26b&colors=caf%C3%A9",
|
||||
"COMMAS | colors=red%20blue,a%26b,caf%C3%A9",
|
||||
"BRACKETS | colors%5B%5D=red%20blue&colors%5B%5D=a%26b&colors%5B%5D=caf%C3%A9"
|
||||
})
|
||||
void formTransformationSendsTheDocumentedArrayFormats(String format, String expected) {
|
||||
Execution execution = runForm("{\"colors\":[\"red blue\",\"a&b\",\"café\"]}",
|
||||
"{\"ArrayFormat\":\"" + format + "\"}");
|
||||
|
||||
assertEquals("SUCCEEDED", execution.getStatus(), execution.getCause());
|
||||
RecordedRequest request = onlyRequest();
|
||||
assertEquals("POST", request.method());
|
||||
assertEquals("application/x-www-form-urlencoded", request.firstHeader("Content-Type"));
|
||||
assertEquals(expected, request.body());
|
||||
}
|
||||
|
||||
@Test
|
||||
void formTransformationDefaultsToIndicesAndEncodesNestedObjectsAndScalars() {
|
||||
Execution execution = runForm("""
|
||||
{"customer":"cust-123","metadata":{"order_details":"monthly report"},
|
||||
"symbols":"a&b=c+%","enabled":true,"count":3,"empty":"","nothing":null,
|
||||
"items":["one","two"]}
|
||||
""", "{}");
|
||||
|
||||
assertEquals("SUCCEEDED", execution.getStatus(), execution.getCause());
|
||||
assertEquals("customer=cust-123&metadata%5Border_details%5D=monthly%20report"
|
||||
+ "&symbols=a%26b%3Dc%2B%25&enabled=true&count=3&empty=¬hing="
|
||||
+ "&items%5B0%5D=one&items%5B1%5D=two", onlyRequest().body());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"{\"ArrayFormat\":\"INVALID\"}", "{\"ArrayFormat\":null}",
|
||||
"{\"ArrayFormat\":{}}", "[]"})
|
||||
void malformedFormOptionsFailBeforeSending(String options) {
|
||||
Execution execution = runForm("{\"value\":1}", options);
|
||||
|
||||
assertEquals("FAILED", execution.getStatus());
|
||||
assertEquals("States.TaskFailed", execution.getError());
|
||||
assertEquals(0, receivedRequests.size());
|
||||
}
|
||||
|
||||
private Execution runForm(String body, String options) {
|
||||
return run("""
|
||||
{"StartAt":"CallHttp","States":{"CallHttp":{"Type":"Task",
|
||||
"Resource":"arn:aws:states:::http:invoke","Parameters":{
|
||||
"ApiEndpoint":"%s/text","Method":"POST",
|
||||
"Authentication":{"ConnectionArn":"%s"},
|
||||
"Headers":{"Content-Type":"application/x-www-form-urlencoded"},
|
||||
"RequestBody.$":"$.body",
|
||||
"Transform":{"RequestBodyEncoding":"URL_ENCODED","RequestEncodingOptions":%s}
|
||||
},"End":true}}}
|
||||
""".formatted(baseUrl, CONNECTION_ARN, options), "{\"body\":" + body + "}");
|
||||
}
|
||||
|
||||
private Execution run(String definition, String input) {
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class HttpTaskFormEncoderTest {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void nestedArrayObjectsUseBracketPathsWithoutMutatingTheInput() throws Exception {
|
||||
JsonNode body = mapper.readTree("{\"items\":[{\"name\":\"café\",\"flags\":[true,false]}]}");
|
||||
JsonNode original = body.deepCopy();
|
||||
|
||||
assertEquals("items%5B0%5D%5Bname%5D=caf%C3%A9&items%5B0%5D%5Bflags%5D%5B0%5D=true"
|
||||
+ "&items%5B0%5D%5Bflags%5D%5B1%5D=false",
|
||||
HttpTaskFormEncoder.encode(body, MissingNode.getInstance()));
|
||||
assertEquals(original, body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void commaSeparatorsStayDistinctFromCommasInsideValues() throws Exception {
|
||||
JsonNode options = mapper.readTree("{\"ArrayFormat\":\"COMMAS\"}");
|
||||
assertEquals("v=a%2Cb,c", HttpTaskFormEncoder.encode(mapper.readTree("{\"v\":[\"a,b\",\"c\"]}"), options));
|
||||
assertEquals("v=a,b%2Cc", HttpTaskFormEncoder.encode(mapper.readTree("{\"v\":[\"a\",\"b,c\"]}"), options));
|
||||
}
|
||||
|
||||
@Test
|
||||
void namesAndValuesCannotInjectAdditionalFields() throws Exception {
|
||||
assertEquals("a%26b%3Dc=%2B%25%26%3D%20%2A~%0D%0A",
|
||||
HttpTaskFormEncoder.encode(mapper.readTree("{\"a&b=c\":\"+%&= *~\\r\\n\"}"),
|
||||
MissingNode.getInstance()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyContainersAreOmittedWhileNullAndEmptyScalarsAreKept() throws Exception {
|
||||
assertEquals("null=&empty=", HttpTaskFormEncoder.encode(
|
||||
mapper.readTree("{\"array\":[],\"object\":{},\"null\":null,\"empty\":\"\"}"),
|
||||
MissingNode.getInstance()));
|
||||
assertEquals("", HttpTaskFormEncoder.encode(mapper.nullNode(), MissingNode.getInstance()));
|
||||
assertEquals("", HttpTaskFormEncoder.encode(MissingNode.getInstance(), MissingNode.getInstance()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"null", "[]", "1", "\"INDICES\"", "{\"ArrayFormat\":null}",
|
||||
"{\"ArrayFormat\":{}}", "{\"ArrayFormat\":[]}", "{\"ArrayFormat\":7}",
|
||||
"{\"ArrayFormat\":\"INVALID\"}"})
|
||||
void malformedOptionsDoNotSilentlySelectTheDefault(String options) throws Exception {
|
||||
JsonNode parsed = mapper.readTree(options);
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> HttpTaskFormEncoder.encode(mapper.createObjectNode(), parsed));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"\"text\"", "[]", "123", "true"})
|
||||
void unverifiedNonObjectRootBodiesRemainExplicitlyUnsupported(String body) throws Exception {
|
||||
JsonNode parsed = mapper.readTree(body);
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> HttpTaskFormEncoder.encode(parsed, MissingNode.getInstance()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"{\"v\":[{}]}", "{\"v\":[[]]}"})
|
||||
void unverifiedCompoundCommaArraysAreNotSilentlyCoerced(String body) throws Exception {
|
||||
JsonNode parsed = mapper.readTree(body);
|
||||
JsonNode options = mapper.readTree("{\"ArrayFormat\":\"COMMAS\"}");
|
||||
assertThrows(IllegalArgumentException.class, () -> HttpTaskFormEncoder.encode(parsed, options));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user