forked from roastedroot/proxy-wasm-java-host
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHeadersTest.java
More file actions
91 lines (80 loc) · 3.59 KB
/
HttpHeadersTest.java
File metadata and controls
91 lines (80 loc) · 3.59 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
package io.roastedroot.proxywasm.examples;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
import java.nio.file.Path;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/http_headers/main_test.go
*/
public class HttpHeadersTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/http_headers/main.wasm"));
private MockHandler handler = new MockHandler();
@Test
public void onHttpRequestHeaders() throws StartException {
try (var proxyWasm = ProxyWasm.builder().build(module)) {
int id = 0;
try (var host = proxyWasm.createHttpContext(handler)) {
id = host.id();
handler.setHttpRequestHeaders(
Map.of(
"key1", "value1",
"key2", "value2"));
var action = host.callOnRequestHeaders(false);
Assertions.assertEquals(Action.CONTINUE, action);
// Check headers
var httpRequestHeaders = handler.getHttpRequestHeaders();
assertNotNull(httpRequestHeaders);
assertEquals("best", httpRequestHeaders.get("test"));
}
// Check logs
handler.assertSortedLogsEqual(
String.format("%d finished", id),
"request header --> key2: value2",
"request header --> key1: value1",
"request header --> test: best");
}
}
@Test
public void onHttpResponseHeaders() throws StartException {
var module = Parser.parse(Path.of("./src/test/go-examples/http_headers/main.wasm"));
var config =
String.format(
"{\"header\": \"%s\", \"value\": \"%s\"}", "x-wasm-header", "x-value");
try (var proxyWasm = ProxyWasm.builder().withPluginConfig(config).build(module)) {
int id = 0;
try (var host = proxyWasm.createHttpContext(handler)) {
id = host.id();
handler.setHttpResponseHeaders(
Map.of(
"key1", "value1",
"key2", "value2"));
var action = host.callOnResponseHeaders(false);
assertEquals(Action.CONTINUE, action);
}
// Check headers
assertEquals(
Map.of(
"key1", "value1",
"key2", "value2",
"x-wasm-header", "x-value",
"x-proxy-wasm-go-sdk-example", "http_headers"),
handler.getHttpResponseHeaders());
// Check logs
handler.assertSortedLogsEqual(
String.format("%d finished", id),
"response header <-- key2: value2",
"response header <-- key1: value1",
"adding header: x-wasm-header=x-value",
"response header <-- x-wasm-header: x-value",
"response header <-- x-proxy-wasm-go-sdk-example: http_headers");
}
}
}