forked from roastedroot/proxy-wasm-java-host
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpBodyTest.java
More file actions
216 lines (174 loc) · 7.77 KB
/
HttpBodyTest.java
File metadata and controls
216 lines (174 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package io.roastedroot.proxywasm.examples;
import static io.roastedroot.proxywasm.Helpers.bytes;
import static io.roastedroot.proxywasm.Helpers.string;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.dylibso.chicory.wasm.Parser;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.HttpContext;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/http_body/main_test.go
*/
public class HttpBodyTest {
private MockHandler handler;
private ProxyWasm proxyWasm;
private HttpContext httpContext;
@BeforeEach
void setUp() throws StartException {
this.handler = new MockHandler();
ProxyWasm.Builder builder = ProxyWasm.builder();
var module = Parser.parse(Path.of("./src/test/go-examples/http_body/main.wasm"));
this.proxyWasm = builder.build(module);
this.httpContext = proxyWasm.createHttpContext(handler);
}
@AfterEach
void tearDown() {
httpContext.close();
proxyWasm.close();
}
@Test
public void testOnHttpRequestHeadersRemoveRequestHeader() throws StartException {
// Call OnRequestHeaders.
handler.setHttpRequestHeaders(
Map.of(
"content-length", "10",
"buffer-operation", "replace"));
var action = httpContext.callOnRequestHeaders(false);
// Must be continued.
Assertions.assertEquals(Action.CONTINUE, action);
var headers = handler.getHttpRequestHeaders();
assertEquals(Map.of("buffer-operation", "replace"), headers);
}
@Test
public void testOnHttpRequestHeaders400Response() throws StartException {
// Call OnRequestHeaders.
var action = httpContext.callOnRequestHeaders(false);
// Must be paused.
assertEquals(Action.PAUSE, action);
// Check the local response.
var response = handler.getSentHttpResponse();
assertNotNull(response);
assertEquals(400, response.statusCode);
assertEquals("content must be provided", string(response.body));
}
@Test
public void testOnHttpRequestBodyPauseUntilEOS() throws StartException {
// Call callOnRequestBody
handler.setHttpRequestBody(bytes("aaaa"));
var action = httpContext.callOnRequestBody(false /* end of stream */);
// Must be paused.
assertEquals(Action.PAUSE, action);
}
@Test
public void testOnHttpRequestBodyAppend() throws StartException {
// Call callOnRequestBody
handler.setHttpRequestHeaders(
Map.of(
"content-length", "10",
"buffer-operation", "append"));
var action = httpContext.callOnRequestHeaders(false /* end of stream */);
// Must be continued.
assertEquals(Action.CONTINUE, action);
// Call callOnRequestBody
handler.setHttpRequestBody("[original body]".getBytes(StandardCharsets.UTF_8));
action = httpContext.callOnRequestBody(true);
assertEquals(Action.CONTINUE, action);
handler.assertLogsEqual("original request body: [original body]");
assertEquals(
"[original body][this is appended body]", string(handler.getHttpRequestBody()));
}
@Test
public void testOnHttpRequestBodyPrepend() throws StartException {
// Call callOnRequestBody
handler.setHttpRequestHeaders(
Map.of(
"content-length", "10",
"buffer-operation", "prepend"));
var action = httpContext.callOnRequestHeaders(false /* end of stream */);
// Must be continued.
assertEquals(Action.CONTINUE, action);
// Call callOnRequestBody
handler.setHttpRequestBody("[original body]".getBytes(StandardCharsets.UTF_8));
action = httpContext.callOnRequestBody(true);
assertEquals(Action.CONTINUE, action);
handler.assertLogsEqual("original request body: [original body]");
assertEquals(
"[this is prepended body][original body]", string(handler.getHttpRequestBody()));
}
@Test
public void testOnHttpRequestBodyReplace() throws StartException {
// Call callOnRequestBody
handler.setHttpRequestHeaders(
Map.of(
"content-length", "10",
"buffer-operation", "replace"));
var action = httpContext.callOnRequestHeaders(false /* end of stream */);
// Must be continued.
assertEquals(Action.CONTINUE, action);
// Call callOnRequestBody
handler.setHttpRequestBody(bytes("[original body]"));
action = httpContext.callOnRequestBody(true);
assertEquals(Action.CONTINUE, action);
handler.assertLogsEqual("original request body: [original body]");
assertEquals("[this is replaced body]", string(handler.getHttpRequestBody()));
}
@Test
public void testOnHttpResponseBodyAppend() throws StartException {
// Call OnRequestHeaders
handler.setHttpRequestHeaders(
Map.of(
"buffer-replace-at", "response",
"content-length", "10",
"buffer-operation", "append"));
var action = httpContext.callOnRequestHeaders(false /* end of stream */);
assertEquals(Action.CONTINUE, action);
handler.setHttpResponseBody(bytes("[original body]"));
action = httpContext.callOnResponseBody(true);
assertEquals(Action.CONTINUE, action);
handler.assertLogsEqual("original response body: [original body]");
assertEquals(
"[original body][this is appended body]", string(handler.getHttpResponseBody()));
}
@Test
public void testOnHttpResponseBodyPrepend() throws StartException {
// Call OnRequestHeaders
handler.setHttpRequestHeaders(
Map.of(
"buffer-replace-at", "response",
"content-length", "10",
"buffer-operation", "prepend"));
var action = httpContext.callOnRequestHeaders(false /* end of stream */);
assertEquals(Action.CONTINUE, action);
handler.setHttpResponseBody(bytes("[original body]"));
action = httpContext.callOnResponseBody(true);
assertEquals(Action.CONTINUE, action);
handler.assertLogsEqual("original response body: [original body]");
assertEquals(
"[this is prepended body][original body]", string(handler.getHttpResponseBody()));
}
@Test
public void testOnHttpResponseBodyReplace() throws StartException {
// Call OnRequestHeaders
handler.setHttpRequestHeaders(
Map.of(
"buffer-replace-at", "response",
"content-length", "10",
"buffer-operation", "replace"));
var action = httpContext.callOnRequestHeaders(false /* end of stream */);
assertEquals(Action.CONTINUE, action);
handler.setHttpResponseBody(bytes("[original body]"));
action = httpContext.callOnResponseBody(true);
assertEquals(Action.CONTINUE, action);
handler.assertLogsEqual("original response body: [original body]");
assertEquals("[this is replaced body]", string(handler.getHttpResponseBody()));
}
}