forked from roastedroot/proxy-wasm-java-host
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkTest.java
More file actions
103 lines (82 loc) · 3.68 KB
/
NetworkTest.java
File metadata and controls
103 lines (82 loc) · 3.68 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
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 org.junit.jupiter.api.AfterEach;
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/network/main_test.go
*/
public class NetworkTest {
private final MockHandler handler = new MockHandler();
private ProxyWasm host;
private NetworkContext context;
@BeforeEach
void setUp() throws StartException {
var module = Parser.parse(Path.of("./src/test/go-examples/network/main.wasm"));
this.host = ProxyWasm.builder().withPluginHandler(handler).build(module);
this.context = host.createNetworkContext(handler);
}
@AfterEach
void tearDown() {
context.close();
host.close();
}
@Test
public void testNetworkOnNewConnection() throws StartException {
Action action = context.callOnNewConnection();
// Verify the action is CONTINUE
assertEquals(Action.CONTINUE, action, "Expected CONTINUE action for new connection");
// Check logs for expected message
handler.assertLogsContain("new connection!");
}
@Test
public void testNetworkOnDownstreamClose() throws StartException {
Action action = context.callOnNewConnection();
assertEquals(Action.CONTINUE, action, "Expected CONTINUE action for new connection");
// Call onDownstreamClose
context.callOnDownstreamConnectionClose();
// Check logs for expected message
handler.assertLogsContain("downstream connection close!");
}
@Test
public void testNetworkOnDownstreamData() throws StartException {
Action action = context.callOnNewConnection();
assertEquals(Action.CONTINUE, action, "Expected CONTINUE action for new connection");
// Call onDownstreamData with test message
String msg = "this is downstream data";
byte[] data = bytes(msg);
handler.setDownStreamData(data);
context.callOnDownstreamData(false); // false = not end of stream
// Check logs for expected message
handler.assertLogsContain(">>>>>> downstream data received >>>>>>\n" + msg);
}
@Test
public void testNetworkOnUpstreamData() throws StartException {
Action action = context.callOnNewConnection();
assertEquals(Action.CONTINUE, action, "Expected CONTINUE action for new connection");
// Call onUpstreamData with test message
String msg = "this is upstream data";
byte[] data = bytes(msg);
handler.setUpstreamData(data);
context.callOnUpstreamData(false); // false = not end of stream
// Check logs for expected message
handler.assertLogsContain("<<<<<< upstream data received <<<<<<\n" + msg);
}
@Test
public void testNetworkCounter() throws StartException {
Action action = context.callOnNewConnection();
assertEquals(Action.CONTINUE, action, "Expected CONTINUE action for new connection");
// Complete the connection
context.close();
// Check logs for expected message
handler.assertLogsContain("connection complete!");
// Check counter metric
String metricName = "proxy_wasm_go.connection_counter";
MockHandler.Metric metric = handler.getMetric(metricName);
assertEquals(MetricType.COUNTER, metric.type, "Expected metric to be a counter");
assertEquals(1, metric.value, "Expected connection counter to be 1");
}
}