This is an automated email from the ASF dual-hosted git repository.
chenjunxu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/apisix-java-plugin-runner.git
The following commit(s) were added to refs/heads/main by this push:
new cdbd81f feat: `PostResponse` supports charset other than `UTF-8`
(#239)
cdbd81f is described below
commit cdbd81f26e693d6bef4cb7cc8c92541e049cf4b4
Author: Junyeong Jang <[email protected]>
AuthorDate: Mon May 22 15:32:53 2023 +0900
feat: `PostResponse` supports charset other than `UTF-8` (#239)
---
runner-plugin-sdk/pom.xml | 5 ++
.../apache/apisix/plugin/runner/PostResponse.java | 10 +++-
.../apisix/plugin/runner/PostResponseTest.java | 55 ++++++++++++++++++++++
3 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/runner-plugin-sdk/pom.xml b/runner-plugin-sdk/pom.xml
index 44108d4..c48418d 100644
--- a/runner-plugin-sdk/pom.xml
+++ b/runner-plugin-sdk/pom.xml
@@ -60,5 +60,10 @@
</exclusion>
</exclusions>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git
a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/PostResponse.java
b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/PostResponse.java
index ab9d330..127de5a 100644
---
a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/PostResponse.java
+++
b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/PostResponse.java
@@ -26,6 +26,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@@ -43,8 +44,11 @@ public class PostResponse implements A6Response {
private Map<String, String> headers;
+ private Charset charset;
+
public PostResponse(long requestId) {
this.requestId = requestId;
+ this.charset = StandardCharsets.UTF_8;
}
@Override
@@ -53,7 +57,7 @@ public class PostResponse implements A6Response {
int bodyIndex = -1;
if (StringUtils.hasText(body)) {
- byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8);
+ byte[] bodyBytes = body.getBytes(this.charset);
bodyIndex = Resp.createBodyVector(builder, bodyBytes);
}
@@ -122,4 +126,8 @@ public class PostResponse implements A6Response {
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
+
+ public void setCharset(Charset charset) {
+ this.charset = charset;
+ }
}
diff --git
a/runner-plugin-sdk/src/test/java/org/apache/apisix/plugin/runner/PostResponseTest.java
b/runner-plugin-sdk/src/test/java/org/apache/apisix/plugin/runner/PostResponseTest.java
new file mode 100644
index 0000000..512aca9
--- /dev/null
+++
b/runner-plugin-sdk/src/test/java/org/apache/apisix/plugin/runner/PostResponseTest.java
@@ -0,0 +1,55 @@
+package org.apache.apisix.plugin.runner;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class PostResponseTest {
+
+ @Test
+ @DisplayName("test encode with set charset")
+ void testEncodeWithSetCharset() {
+ long requestId = 1L;
+ String body = "dummy body";
+ Charset charset = StandardCharsets.UTF_16;
+
+ PostResponse postResponse = new PostResponse(requestId);
+ postResponse.setBody(body);
+ postResponse.setCharset(charset);
+
+ ByteBuffer encoded = postResponse.encode();
+
+
assertTrue(Collections.indexOfSubList(byteArrayToList(encoded.array()),
byteArrayToList(body.getBytes(charset))) >= 0);
+ }
+
+ @Test
+ @DisplayName("test encode without set charset")
+ void testEncodeWithoutSetCharset() {
+ long requestId = 1L;
+ String body = "dummy body";
+ Charset charset = StandardCharsets.UTF_8;
+
+ PostResponse postResponse = new PostResponse(requestId);
+ postResponse.setBody(body);
+
+ ByteBuffer encoded = postResponse.encode();
+
+
assertTrue(Collections.indexOfSubList(byteArrayToList(encoded.array()),
byteArrayToList(body.getBytes(charset))) >= 0);
+ }
+
+ private List<Byte> byteArrayToList(byte[] array) {
+ List<Byte> list = new ArrayList<>();
+ for (byte b : array) {
+ list.add(b);
+ }
+ return list;
+ }
+}