Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions test/src/auto_response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use worker::{
durable_object, DurableObject, Env, Request, Response, Result, State,
durable_object, js_sys::Uint8Array, DurableObject, Env, Request, Response, Result, State,
WebSocketRequestResponsePair,
};

Expand All @@ -16,11 +16,20 @@ impl DurableObject for AutoResponseObject {
async fn fetch(&self, req: Request) -> Result<Response> {
match req.path().as_str() {
"/set" => {
// Configure ping -> pong auto-response for all websockets bound to this DO.
let pair = WebSocketRequestResponsePair::new("ping", "pong")?;
self.state.set_websocket_auto_response(&pair);
Response::ok("ok")
}
"/set-binary" => {
let request_data: &[u8] = &[0x01, 0x02, 0x03];
let response_data: &[u8] = &[0x04, 0x05, 0x06];
let pair = WebSocketRequestResponsePair::new_bytes(
&Uint8Array::from(request_data),
&Uint8Array::from(response_data),
)?;
self.state.set_websocket_auto_response(&pair);
Response::ok("ok")
}
"/get" => {
if let Some(pair) = self.state.get_websocket_auto_response() {
let request_str = pair.request();
Expand All @@ -30,12 +39,20 @@ impl DurableObject for AutoResponseObject {
Response::ok("none")
}
}
"/get-binary" => {
if let Some(pair) = self.state.get_websocket_auto_response() {
let req_bytes = pair.request_bytes().to_vec();
let res_bytes = pair.response_bytes().to_vec();
Response::ok(format!("{req_bytes:?}:{res_bytes:?}"))
} else {
Response::ok("none")
}
}
_ => Response::error("Not Found", 404),
}
}
}

// Route handler to exercise the Durable Object from tests.
#[worker::send]
pub async fn handle_auto_response(
_req: Request,
Expand All @@ -44,8 +61,18 @@ pub async fn handle_auto_response(
) -> Result<Response> {
let namespace = env.durable_object("AUTO")?;
let stub = namespace.id_from_name("singleton")?.get_stub()?;
// Ensure auto-response is configured
stub.fetch_with_str("https://fake-host/set").await?;
// Retrieve and return it for assertion
stub.fetch_with_str("https://fake-host/get").await
}

#[worker::send]
pub async fn handle_auto_response_binary(
_req: Request,
env: Env,
_data: crate::SomeSharedData,
) -> Result<Response> {
let namespace = env.durable_object("AUTO")?;
let stub = namespace.id_from_name("singleton-binary")?.get_stub()?;
stub.fetch_with_str("https://fake-host/set-binary").await?;
stub.fetch_with_str("https://fake-host/get-binary").await
}
1 change: 1 addition & 0 deletions test/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ macro_rules! add_routes (
add_route!($obj, get, "/socket/failed", socket::handle_socket_failed);
add_route!($obj, get, "/socket/read", socket::handle_socket_read);
add_route!($obj, get, "/durable/auto-response", auto_response::handle_auto_response);
add_route!($obj, get, "/durable/auto-response-binary", auto_response::handle_auto_response_binary);
add_route!($obj, get, "/durable/hello", durable::handle_hello);
add_route!($obj, get, "/durable/hello-unique", durable::handle_hello_unique);
add_route!($obj, get, "/durable/storage", durable::handle_storage);
Expand Down
8 changes: 8 additions & 0 deletions test/tests/auto_response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@
const text = await resp.text();
expect(text).toBe("ping:pong");
});

test("set and get binary auto-response pair", async () => {
const resp = await mf.dispatchFetch(
`${mfUrl}durable/auto-response-binary`
);
const text = await resp.text();
expect(text).toBe("[1, 2, 3]:[4, 5, 6]");

Check failure on line 16 in test/tests/auto_response.spec.ts

View workflow job for this annotation

GitHub Actions / Test (panic-unwind)

tests/auto_response.spec.ts > durable object websocket auto-response > set and get binary auto-response pair

AssertionError: expected '[1, 0, 2, 0, 3]:[4, 0, 5, 0, 6]' to be '[1, 2, 3]:[4, 5, 6]' // Object.is equality Expected: "[1, 2, 3]:[4, 5, 6]" Received: "[1, 0, 2, 0, 3]:[4, 0, 5, 0, 6]" ❯ tests/auto_response.spec.ts:16:18

Check failure on line 16 in test/tests/auto_response.spec.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

tests/auto_response.spec.ts > durable object websocket auto-response > set and get binary auto-response pair

AssertionError: expected '[1, 0, 2, 0, 3]:[4, 0, 5, 0, 6]' to be '[1, 2, 3]:[4, 5, 6]' // Object.is equality Expected: "[1, 2, 3]:[4, 5, 6]" Received: "[1, 0, 2, 0, 3]:[4, 0, 5, 0, 6]" ❯ tests/auto_response.spec.ts:16:18
});
});
13 changes: 13 additions & 0 deletions worker-sys/src/types/websocket_request_response_pair.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use js_sys::Uint8Array;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand All @@ -9,9 +10,21 @@ extern "C" {
#[wasm_bindgen(constructor, catch)]
pub fn new(request: &str, response: &str) -> Result<WebSocketRequestResponsePair, JsValue>;

#[wasm_bindgen(constructor, catch, js_class = "WebSocketRequestResponsePair")]
pub fn new_bytes(
request: &Uint8Array,
response: &Uint8Array,
) -> Result<WebSocketRequestResponsePair, JsValue>;

#[wasm_bindgen(method, getter)]
pub fn request(this: &WebSocketRequestResponsePair) -> String;

#[wasm_bindgen(method, getter)]
pub fn response(this: &WebSocketRequestResponsePair) -> String;

#[wasm_bindgen(method, getter, js_name = request)]
pub fn request_bytes(this: &WebSocketRequestResponsePair) -> Uint8Array;

#[wasm_bindgen(method, getter, js_name = response)]
pub fn response_bytes(this: &WebSocketRequestResponsePair) -> Uint8Array;
}
Loading