forked from roastedroot/proxy-wasm-java-host
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonValidationTest.java
More file actions
185 lines (166 loc) · 6.13 KB
/
JsonValidationTest.java
File metadata and controls
185 lines (166 loc) · 6.13 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
package io.roastedroot.proxywasm;
import static io.roastedroot.proxywasm.Helpers.bytes;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.dylibso.chicory.wasm.Parser;
import java.nio.file.Path;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
// This file what the go code was:
//
// func TestOnHTTPRequestHeaders(t *testing.T) {
// type testCase struct {
// contentType string
// expectedAction types.Action
// }
//
// vmTest(t, func(t *testing.T, vm types.VMContext) {
// for name, tCase := range map[string]testCase{
// "fails due to unsupported content type": {
// contentType: "text/html",
// expectedAction: types.ActionPause,
// },
// "success for JSON": {
// contentType: "application/json",
// expectedAction: types.ActionContinue,
// },
// } {
// t.Run(name, func(t *testing.T) {
// opt := proxytest.NewEmulatorOption().WithVMContext(vm)
// host, reset := proxytest.NewHostEmulator(opt)
// defer reset()
//
// require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
//
// id := host.InitializeHttpContext()
//
// hs := [][2]string{{"content-type", tCase.contentType}}
//
// action := host.CallOnRequestHeaders(id, hs, false)
// assert.Equal(t, tCase.expectedAction, action)
// })
// }
// })
// }
//
// func TestOnHTTPRequestBody(t *testing.T) {
// type testCase struct {
// body string
// expectedAction types.Action
// }
//
// vmTest(t, func(t *testing.T, vm types.VMContext) {
//
// for name, tCase := range map[string]testCase{
// "pauses due to invalid payload": {
// body: "invalid_payload",
// expectedAction: types.ActionPause,
// },
// "pauses due to unknown keys": {
// body: `{"unknown_key":"unknown_value"}`,
// expectedAction: types.ActionPause,
// },
// "success": {
// body: "{\"my_key\":\"my_value\"}",
// expectedAction: types.ActionContinue,
// },
// } {
// t.Run(name, func(t *testing.T) {
// opt := proxytest.
// NewEmulatorOption().
// WithPluginConfiguration([]byte(`{"requiredKeys": ["my_key"]}`)).
// WithVMContext(vm)
// host, reset := proxytest.NewHostEmulator(opt)
// defer reset()
//
// require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
//
// id := host.InitializeHttpContext()
//
// action := host.CallOnRequestBody(id, []byte(tCase.body), true)
// assert.Equal(t, tCase.expectedAction, action)
// })
// }
// })
// }
/**
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/json_validation/main_test.go
*/
public class JsonValidationTest {
private final MockHandler handler = new MockHandler();
@Nested
class OnHttpRequestHeaders {
private ProxyWasm host;
private HttpContext context;
@BeforeEach
void setUp() throws StartException {
var module = Parser.parse(Path.of("./src/test/go-examples/json_validation/main.wasm"));
this.host = ProxyWasm.builder().build(module);
this.context = host.createHttpContext(handler);
}
@AfterEach
void tearDown() {
context.close();
host.close();
}
@Test
void testFailsDueToUnsupportedContentType() {
var contentType = "application/json";
var expectedAction = Action.CONTINUE;
handler.setHttpRequestHeaders(Map.of("content-type", contentType));
var action = context.callOnRequestHeaders(false);
assertEquals(expectedAction, action);
}
@Test
void successForJson() {
var contentType = "application/json";
var expectedAction = Action.CONTINUE;
handler.setHttpRequestHeaders(Map.of("content-type", contentType));
var action = context.callOnRequestHeaders(false);
assertEquals(expectedAction, action);
}
}
@Nested
class OnHttpRequestBody {
private ProxyWasm host;
private HttpContext context;
@BeforeEach
void setUp() throws StartException {
var config = "{\"requiredKeys\": [\"my_key\"]}";
var module = Parser.parse(Path.of("./src/test/go-examples/json_validation/main.wasm"));
this.host = ProxyWasm.builder().withPluginConfig(config).build(module);
this.context = host.createHttpContext(handler);
}
@AfterEach
void tearDown() {
context.close();
host.close();
}
@Test
public void pausesDueToInvalidPayload() {
String body = "invalid_payload";
Action expectedAction = Action.PAUSE;
handler.setHttpRequestBody(bytes(body));
var action = context.callOnRequestBody(true);
assertEquals(expectedAction, action);
}
@Test
public void pausesDueToUnknownKeys() {
String body = "{\"unknown_key\":\"unknown_value\"}";
Action expectedAction = Action.PAUSE;
handler.setHttpRequestBody(bytes(body));
var action = context.callOnRequestBody(true);
assertEquals(expectedAction, action);
}
@Test
public void success() {
String body = "{\"my_key\":\"my_value\"}";
Action expectedAction = Action.CONTINUE;
handler.setHttpRequestBody(bytes(body));
var action = context.callOnRequestBody(true);
assertEquals(expectedAction, action);
}
}
}