Skip to content

Commit cdbd81f

Browse files
authored
feat: PostResponse supports charset other than UTF-8 (#239)
1 parent e18da5c commit cdbd81f

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

runner-plugin-sdk/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@
6060
</exclusion>
6161
</exclusions>
6262
</dependency>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
</dependency>
6368
</dependencies>
6469
</project>

runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/PostResponse.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.util.StringUtils;
2727

2828
import java.nio.ByteBuffer;
29+
import java.nio.charset.Charset;
2930
import java.nio.charset.StandardCharsets;
3031
import java.util.HashMap;
3132
import java.util.Map;
@@ -43,8 +44,11 @@ public class PostResponse implements A6Response {
4344

4445
private Map<String, String> headers;
4546

47+
private Charset charset;
48+
4649
public PostResponse(long requestId) {
4750
this.requestId = requestId;
51+
this.charset = StandardCharsets.UTF_8;
4852
}
4953

5054
@Override
@@ -53,7 +57,7 @@ public ByteBuffer encode() {
5357

5458
int bodyIndex = -1;
5559
if (StringUtils.hasText(body)) {
56-
byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8);
60+
byte[] bodyBytes = body.getBytes(this.charset);
5761
bodyIndex = Resp.createBodyVector(builder, bodyBytes);
5862
}
5963

@@ -122,4 +126,8 @@ public void setBody(String body) {
122126
public void setStatusCode(int statusCode) {
123127
this.statusCode = statusCode;
124128
}
129+
130+
public void setCharset(Charset charset) {
131+
this.charset = charset;
132+
}
125133
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.apache.apisix.plugin.runner;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.nio.ByteBuffer;
7+
import java.nio.charset.Charset;
8+
import java.nio.charset.StandardCharsets;
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
15+
class PostResponseTest {
16+
17+
@Test
18+
@DisplayName("test encode with set charset")
19+
void testEncodeWithSetCharset() {
20+
long requestId = 1L;
21+
String body = "dummy body";
22+
Charset charset = StandardCharsets.UTF_16;
23+
24+
PostResponse postResponse = new PostResponse(requestId);
25+
postResponse.setBody(body);
26+
postResponse.setCharset(charset);
27+
28+
ByteBuffer encoded = postResponse.encode();
29+
30+
assertTrue(Collections.indexOfSubList(byteArrayToList(encoded.array()), byteArrayToList(body.getBytes(charset))) >= 0);
31+
}
32+
33+
@Test
34+
@DisplayName("test encode without set charset")
35+
void testEncodeWithoutSetCharset() {
36+
long requestId = 1L;
37+
String body = "dummy body";
38+
Charset charset = StandardCharsets.UTF_8;
39+
40+
PostResponse postResponse = new PostResponse(requestId);
41+
postResponse.setBody(body);
42+
43+
ByteBuffer encoded = postResponse.encode();
44+
45+
assertTrue(Collections.indexOfSubList(byteArrayToList(encoded.array()), byteArrayToList(body.getBytes(charset))) >= 0);
46+
}
47+
48+
private List<Byte> byteArrayToList(byte[] array) {
49+
List<Byte> list = new ArrayList<>();
50+
for (byte b : array) {
51+
list.add(b);
52+
}
53+
return list;
54+
}
55+
}

0 commit comments

Comments
 (0)