-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests to cover connecting to durable object via websocket using t…
…he hibernatable apis
- Loading branch information
Showing
6 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,46 @@ | ||
import { describe, test, expect } from "vitest"; | ||
import {describe, test, expect, vi} from "vitest"; | ||
import { mf } from "./mf"; | ||
import {MessageEvent} from "miniflare"; | ||
|
||
describe("durable", () => { | ||
test("put-raw", async () => { | ||
const resp = await mf.dispatchFetch("https://fake.host/durable/put-raw"); | ||
expect(await resp.text()).toBe("ok"); | ||
}); | ||
|
||
test("websocket-to-durable", async () => { | ||
const resp = await mf.dispatchFetch("http://fake.host/durable/websocket", { | ||
headers: { | ||
upgrade: "websocket", | ||
}, | ||
}); | ||
expect(resp.webSocket).not.toBeNull(); | ||
|
||
const socket = resp.webSocket!; | ||
socket.accept(); | ||
|
||
const handlers = { | ||
messageHandler: (event: MessageEvent) => { | ||
expect(event.data).toMatch(/^10|20|30$/); | ||
}, | ||
close(event: CloseEvent) {}, | ||
}; | ||
|
||
const messageHandlerWrapper = vi.spyOn(handlers, "messageHandler"); | ||
const closeHandlerWrapper = vi.spyOn(handlers, "messageHandler"); | ||
socket.addEventListener("message", handlers.messageHandler); | ||
socket.addEventListener("close", handlers.close); | ||
|
||
socket.send("hi, can you ++?"); | ||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
expect(messageHandlerWrapper).toHaveBeenCalledTimes(1); | ||
|
||
socket.send("hi again, more ++?"); | ||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
expect(messageHandlerWrapper).toHaveBeenCalledTimes(2); | ||
|
||
socket.close(); | ||
expect(closeHandlerWrapper).toBeCalled(); | ||
}); | ||
}); | ||
|