diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index d8c0ce7..9882e2c 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -12,11 +12,15 @@ jobs: - name: install deps run: | sudo apt-get update - sudo apt-get install -y make cmake emscripten autoconf automake libtool pkg-config wget xxd + sudo apt-get install -y make cmake emscripten autoconf automake libtool pkg-config wget xxd python3-selenium python3-websockets - name: run build working-directory: ./client run: ./build.sh all + + - name: run tests + working-directory: ./client + run: ./tests/run.sh - name: upload img uses: actions/upload-artifact@v4 diff --git a/client/tests/index.html b/client/tests/index.html new file mode 100644 index 0000000..db5bee9 --- /dev/null +++ b/client/tests/index.html @@ -0,0 +1,43 @@ + + +
+ + + + + + +libcurl.js unit test runner
+ + \ No newline at end of file diff --git a/client/tests/run.sh b/client/tests/run.sh new file mode 100755 index 0000000..a2d19a2 --- /dev/null +++ b/client/tests/run.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +trap "exit" INT TERM +trap "kill 0" EXIT +STATIC="$(pwd)" python3 ../server/wisp_server/main.py >/dev/null & + +sleep 1 +echo "Running tests" +python3 tests/run_tests.py diff --git a/client/tests/run_tests.py b/client/tests/run_tests.py new file mode 100644 index 0000000..8f709d5 --- /dev/null +++ b/client/tests/run_tests.py @@ -0,0 +1,44 @@ +import unittest + +from selenium import webdriver +from selenium.webdriver.common.desired_capabilities import DesiredCapabilities +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +class JSTest(unittest.TestCase): + def setUp(self): + options = webdriver.ChromeOptions() + options.add_argument("--headless") + options.set_capability("goog:loggingPrefs", {"browser": "ALL"}) + + self.browser = webdriver.Chrome(options=options) + + def tearDown(self): + self.browser.quit() + + def run_test(self, script): + self.browser.get(f"http://localhost:6001/tests/#{script}") + wait = WebDriverWait(self.browser, 20) + result = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.flag'))).get_attribute("result") + + if result != "success": + for entry in self.browser.get_log("browser"): + print(f"{entry['level']}: {entry['message']}") + + assert result == "success" + + def test_fetch_once(self): + self.run_test("fetch_once.js") + + def test_fetch_multiple(self): + self.run_test("fetch_multiple.js") + + def test_fetch_parallel(self): + self.run_test("fetch_parallel.js") + + def test_websocket(self): + self.run_test("test_websocket.js") + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/client/tests/scripts/fetch_multiple.js b/client/tests/scripts/fetch_multiple.js new file mode 100644 index 0000000..fe7cb86 --- /dev/null +++ b/client/tests/scripts/fetch_multiple.js @@ -0,0 +1,7 @@ +async function test() { + await libcurl.fetch("https://example.com/"); + for (let i=0; i<20; i++) { + let r = await libcurl.fetch("https://example.com/"); + assert(r.status === 200, "wrong status"); + } +} \ No newline at end of file diff --git a/client/tests/scripts/fetch_once.js b/client/tests/scripts/fetch_once.js new file mode 100644 index 0000000..e94fefa --- /dev/null +++ b/client/tests/scripts/fetch_once.js @@ -0,0 +1,4 @@ +async function test() { + let r = await libcurl.fetch("https://example.com/"); + assert(r.status === 200, "wrong status"); +} \ No newline at end of file diff --git a/client/tests/scripts/fetch_parallel.js b/client/tests/scripts/fetch_parallel.js new file mode 100644 index 0000000..98a083a --- /dev/null +++ b/client/tests/scripts/fetch_parallel.js @@ -0,0 +1,8 @@ +async function test() { + await libcurl.fetch("https://www.example.com/"); + let promises = []; + for (let i=0; i<10; i++) { + promises.push(libcurl.fetch("https://www.example.com/")) + } + await Promise.all(promises); +} diff --git a/client/tests/scripts/test_websocket.js b/client/tests/scripts/test_websocket.js new file mode 100644 index 0000000..4a10cf3 --- /dev/null +++ b/client/tests/scripts/test_websocket.js @@ -0,0 +1,24 @@ +function test() { + let message_len = 128*1024; + + return new Promise((resolve, reject) => { + let ws = new libcurl.WebSocket("wss://echo.websocket.org"); + ws.addEventListener("open", () => { + ws.send("hello".repeat(message_len)); + }); + + let messages = 0; + ws.addEventListener("message", (event) => { + messages += 1; + if (messages >= 2) { + if (event.data !== "hello".repeat(message_len)) reject("unexpected response"); + if (messages >= 11) resolve(); + ws.send("hello".repeat(message_len)); + } + }); + + ws.addEventListener("error", () => { + reject("ws error occurred"); + }); + }) +} \ No newline at end of file diff --git a/server/wisp_server b/server/wisp_server index 138ef0f..bd7bca9 160000 --- a/server/wisp_server +++ b/server/wisp_server @@ -1 +1 @@ -Subproject commit 138ef0f73027b3d237ec84bee7ea43b4f30c8b74 +Subproject commit bd7bca97b22023a1b49d0f5a09081ddcbb4ad49c