Skip to content

Commit

Permalink
Add test for tail workers via workerd config.
Browse files Browse the repository at this point in the history
  • Loading branch information
kentonv committed Nov 29, 2024
1 parent cbe7864 commit 8e98ef4
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/workerd/server/server-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,106 @@ KJ_TEST("Server: Durable Objects websocket hibernation") {
wsConn.send(kj::str("\x81\x1a", confirmEviction));
wsConn.recvWebSocket(evicted);
}

KJ_TEST("Server: tail workers") {
TestServer test(R"((
services = [
( name = "hello",
worker = (
compatibilityDate = "2024-11-01",
modules = [
( name = "main.js",
esModule =
`export default {
` async fetch(req, env, ctx) {
` console.log("foo", "bar");
` console.log("baz");
` return new Response("OK");
` }
`}
)
],
logging = (
toServices = ["tail", "tail2"]
),
)
),
( name = "tail",
worker = (
compatibilityDate = "2024-11-01",
modules = [
( name = "main.js",
esModule =
`export default {
` async tail(req, env, ctx) {
` await fetch("http://tail", {
` method: "POST",
` body: JSON.stringify(req[0].logs.map(log => log.message))
` });
` }
`}
)
],
)
),
( name = "tail2",
worker = (
compatibilityDate = "2024-11-01",
modules = [
( name = "main.js",
esModule =
`export default {
` async tail(req, env, ctx) {
` await fetch("http://tail2/" + req[0].logs.length);
` }
`}
)
],
)
),
],
sockets = [
( name = "main",
address = "test-addr",
service = "hello"
)
]
))"_kj);

test.start();
auto conn = test.connect("test-addr");
conn.sendHttpGet("/");
conn.recvHttp200("OK");

auto subreq = test.receiveInternetSubrequest("tail");
subreq.recv(R"(
POST / HTTP/1.1
Content-Length: 23
Host: tail
Content-Type: text/plain;charset=UTF-8
[["foo","bar"],["baz"]])"_blockquote);

auto subreq2 = test.receiveInternetSubrequest("tail2");
subreq2.recv(R"(
GET /2 HTTP/1.1
Host: tail2
)"_blockquote);

subreq.send(R"(
HTTP/1.1 200 OK
Content-Length: 0
)"_blockquote);

subreq2.send(R"(
HTTP/1.1 200 OK
Content-Length: 0
)"_blockquote);
}

// =======================================================================================
// Test HttpOptions on receive

Expand Down

0 comments on commit 8e98ef4

Please sign in to comment.