fix: stop request but not calling setStatusCode will trigger an exception In APISIX (#56)
diff --git a/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/FlatBuffersEncoderTest.java b/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/FlatBuffersEncoderTest.java
index 2d3ddbd..7cb9c11 100644
--- a/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/FlatBuffersEncoderTest.java
+++ b/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/FlatBuffersEncoderTest.java
@@ -263,6 +263,24 @@
Assertions.assertEquals(bytes[0], array[1]);
Assertions.assertEquals(bytes[1], array[2]);
Assertions.assertEquals(bytes[2], array[3]);
+ }
+ @Test
+ @DisplayName("test stop the request without setStatusCode")
+ void testDoFilterWithoutSetStatusCode() {
+ HttpResponse httpResponse = new HttpResponse(0L);
+ // only set header, without setStatusCode, use 200 as default
+ httpResponse.setHeader("Foo", "Bar");
+ ByteBuffer result = flatBuffersEncoder.encode(httpResponse);
+ result.position(4);
+ io.github.api7.A6.HTTPReqCall.Resp resp = io.github.api7.A6.HTTPReqCall.Resp.getRootAsResp(result);
+ Assertions.assertEquals(resp.actionType(), Action.Stop);
+ Stop stop = (Stop) resp.action(new Stop());
+ Assertions.assertEquals(stop.status(), 200);
+ for (int i = 0; i < stop.headersLength(); i++) {
+ if (stop.headers(i).name().equals("Foo")) {
+ Assertions.assertEquals(stop.headers(i).value(), "Bar");
+ }
+ }
}
}
diff --git a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
index 916f748..409e955 100644
--- a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
+++ b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
@@ -22,6 +22,8 @@
import io.github.api7.A6.HTTPReqCall.Rewrite;
import io.github.api7.A6.HTTPReqCall.Stop;
import io.github.api7.A6.TextEntry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
@@ -38,6 +40,7 @@
* }
*/
public class HttpResponse implements A6Response {
+ private final Logger logger = LoggerFactory.getLogger(HttpResponse.class);
private final long requestId;
@@ -194,6 +197,14 @@
Stop.startStop(builder);
if (!Objects.isNull(statusCode)) {
Stop.addStatus(builder, statusCode);
+ } else {
+ /**
+ * Avoid APISIX using 0 as the default HTTP Status Code
+ * {@link org.apache.apisix.plugin.runner.HttpResponse#setStatusCode(int statusCode)}
+ * @see https://github.com/apache/apisix-java-plugin-runner/issues/55
+ */
+ Stop.addStatus(builder, 200);
+ logger.info("Use 200 as the default HTTP Status Code when setStatusCode is not called");
}
if (-1 != headerIndex) {
Stop.addHeaders(builder, headerIndex);