fix(apigateway): answer an HTTP API proxy result without a Content-Type as text/plain (#6201)

* fix(apigateway): answer an HTTP API proxy result without a Content-Type as text/plain

Measured on AWS (eu-west-1, 2026-10-19): an HTTP API answers a Lambda proxy
result that carries a body and no Content-Type header of its own with
text/plain; charset=utf-8, in both payload formats. Floci fell back to
application/json, the REST API's default, so a client that trusts the
header read a plain body as JSON.

The fallback now follows the API kind: an HTTP API answers text/plain, a
REST API keeps answering application/json.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* test(apigateway): fold the Content-Type cases into the inference test

The three cases repeated the inference test's setup, route helper and
cleanup, so they join it: two results without a Content-Type in the two
payload formats, and one that names application/json. The docs say a
statusCode and a body, since a format 2.0 result without a statusCode
still takes the inferred JSON path, and the compatibility test's get
helper and parameterized name are back on one line each.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

---------

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-22 16:04:51 -04:00
1 parent b58947c70a
commit 34f592f7e7
5 files changed
+98 -7

No files matched your search

@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
import io.github.hectorvent.floci.services.apigatewayv2.model.Api;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
@@ -18,6 +19,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.net.URI;
import java.nio.charset.StandardCharsets;
@@ -372,6 +374,19 @@ class ApiGatewayExecuteControllerTest {
assertEquals("application/json", response.getMediaType().toString());
}
@ParameterizedTest(name = "payload format {0}")
@ValueSource(strings = {"1.0", "2.0"})
void buildProxyResponseAnswersTextPlainWhenAnHttpApiResultHasNoContentType(String format) {
// Measured on HTTP APIs (eu-west-1, 2026-10-19): a result with a statusCode and a body but
// no Content-Type header of its own is answered text/plain; charset=utf-8, in both payload
// formats. A REST API keeps answering application/json.
ApiGatewayExecuteController controller = controller(new ObjectMapper());
Response response = controller.buildProxyResponse(proxyPayload("""
{"statusCode":200,"headers":{"X-Other":"value"},"body":"hello"}
"""), true, "2.0".equals(format));
assertEquals(MediaType.valueOf("text/plain; charset=utf-8"), response.getMediaType());
}
@Test
void buildProxyResponseDefaultsToJsonWhenContentTypeIsJsonNull() {
// A JSON-null Content-Type must fall back to the default, the same as a missing header —
@@ -8,16 +8,23 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsStringIgnoringCase;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
/**
* End to end check of how an HTTP API answers a payload format 2.0 Lambda proxy integration whose
* function returns no {@code statusCode} or throws. AWS infers a 200 JSON response whose body is
* the function's result, and answers a function error with {@code {"message":"Internal Server
* Error"}}. An integration configured with format 1.0 keeps the 1.0 response rules.
* End to end check of how an HTTP API answers a Lambda proxy integration whose function returns
* no {@code statusCode} or throws. AWS infers a 200 JSON response whose body is the function's
* result, and answers a function error with {@code {"message":"Internal Server Error"}}. An
* integration configured with format 1.0 keeps the 1.0 response rules.
*
* <p>A result that carries a {@code statusCode} and a {@code body} and leaves the
* {@code Content-Type} header out is answered {@code text/plain; charset=utf-8}, in both payload
* formats (measured on AWS, eu-west-1, 2026-10-19). A result that names a {@code Content-Type}
* keeps it.
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@@ -27,6 +34,9 @@ class HttpApiLambdaProxyResponseInferenceIntegrationTest {
private static final String STRING_FUNCTION = "httpv2-inference-string-fn";
private static final String THROW_FUNCTION = "httpv2-inference-throw-fn";
private static final String V1_FUNCTION = "httpv2-inference-v1-fn";
private static final String PLAIN_FUNCTION = "httpv2-inference-plain-fn";
private static final String PLAIN_V1_FUNCTION = "httpv2-inference-plain-v1-fn";
private static final String JSON_FUNCTION = "httpv2-inference-json-fn";
private static String apiId;
@@ -60,6 +70,12 @@ class HttpApiLambdaProxyResponseInferenceIntegrationTest {
"exports.handler = async () => { throw new Error('boom'); };");
createProxyRoute(V1_FUNCTION, "/v1", "1.0",
"exports.handler = async () => ({ headers: { 'X-Trace': 'value' }, body: 'inner' });");
createProxyRoute(PLAIN_FUNCTION, "/plain", "2.0",
"exports.handler = async () => ({ statusCode: 200, body: 'hello' });");
createProxyRoute(PLAIN_V1_FUNCTION, "/plain-v1", "1.0",
"exports.handler = async () => ({ statusCode: 200, body: 'hello' });");
createProxyRoute(JSON_FUNCTION, "/json", "2.0",
"exports.handler = async () => ({ statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: '{\"a\":1}' });");
}
private static void createProxyRoute(String functionName, String path, String handlerCode) throws Exception {
@@ -145,12 +161,46 @@ class HttpApiLambdaProxyResponseInferenceIntegrationTest {
.body("message", equalTo("Internal Server Error"));
}
@Test
@Order(14)
void aResultWithoutAContentTypeIsAnsweredAsTextPlain() {
given()
.when().get("/execute-api/" + apiId + "/test/plain")
.then()
.statusCode(200)
.contentType(allOf(startsWith("text/plain"), containsStringIgnoringCase("charset=utf-8")))
.body(equalTo("hello"));
}
@Test
@Order(15)
void aResultWithoutAContentTypeIsAnsweredAsTextPlainInPayloadFormatOne() {
given()
.when().get("/execute-api/" + apiId + "/test/plain-v1")
.then()
.statusCode(200)
.contentType(allOf(startsWith("text/plain"), containsStringIgnoringCase("charset=utf-8")))
.body(equalTo("hello"));
}
@Test
@Order(16)
void aResultThatNamesAContentTypeKeepsIt() {
given()
.when().get("/execute-api/" + apiId + "/test/json")
.then()
.statusCode(200)
.contentType(startsWith("application/json"))
.body(equalTo("{\"a\":1}"));
}
@Test
@Order(999)
void cleanup() throws Exception {
if (apiId != null) {
given().when().delete("/v2/apis/" + apiId);
}
WebSocketTestSupport.deleteFunctions(BARE_FUNCTION, STRING_FUNCTION, THROW_FUNCTION, V1_FUNCTION);
WebSocketTestSupport.deleteFunctions(BARE_FUNCTION, STRING_FUNCTION, THROW_FUNCTION, V1_FUNCTION,
PLAIN_FUNCTION, PLAIN_V1_FUNCTION, JSON_FUNCTION);
}
}