mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-12 05:50:01 -04:00
add unit tests
This commit is contained in:
parent
b071019761
commit
2d98b82ee3
9 changed files with 147 additions and 2 deletions
6
.github/workflows/build.yaml
vendored
6
.github/workflows/build.yaml
vendored
|
@ -12,11 +12,15 @@ jobs:
|
||||||
- name: install deps
|
- name: install deps
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get update
|
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
|
- name: run build
|
||||||
working-directory: ./client
|
working-directory: ./client
|
||||||
run: ./build.sh all
|
run: ./build.sh all
|
||||||
|
|
||||||
|
- name: run tests
|
||||||
|
working-directory: ./client
|
||||||
|
run: ./tests/run.sh
|
||||||
|
|
||||||
- name: upload img
|
- name: upload img
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
|
|
43
client/tests/index.html
Normal file
43
client/tests/index.html
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link rel="icon" href="data:;base64,=">
|
||||||
|
|
||||||
|
<script defer src="/out/libcurl.js" onload="libcurl.load_wasm('/out/libcurl.wasm');"></script>
|
||||||
|
<script>
|
||||||
|
function create_flag(result) {
|
||||||
|
let element = document.createElement("div");
|
||||||
|
element.setAttribute("result", result);
|
||||||
|
element.className = "flag";
|
||||||
|
document.body.append(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
function assert(condition, message) {
|
||||||
|
if (!condition) {
|
||||||
|
throw new Error(message || "Assertion failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let script = document.createElement("script");
|
||||||
|
script.src = "/tests/scripts/" + location.hash.substring(1);
|
||||||
|
script.async = false;
|
||||||
|
script.defer = false;
|
||||||
|
document.head.append(script);
|
||||||
|
|
||||||
|
document.addEventListener("libcurl_load", async ()=>{
|
||||||
|
try {
|
||||||
|
libcurl.set_websocket(`ws://localhost:6001/ws/`);
|
||||||
|
await test();
|
||||||
|
create_flag("success");
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error(e.stack || e);
|
||||||
|
create_flag("error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>libcurl.js unit test runner</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
11
client/tests/run.sh
Executable file
11
client/tests/run.sh
Executable file
|
@ -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
|
44
client/tests/run_tests.py
Normal file
44
client/tests/run_tests.py
Normal file
|
@ -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()
|
7
client/tests/scripts/fetch_multiple.js
Normal file
7
client/tests/scripts/fetch_multiple.js
Normal file
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
4
client/tests/scripts/fetch_once.js
Normal file
4
client/tests/scripts/fetch_once.js
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
async function test() {
|
||||||
|
let r = await libcurl.fetch("https://example.com/");
|
||||||
|
assert(r.status === 200, "wrong status");
|
||||||
|
}
|
8
client/tests/scripts/fetch_parallel.js
Normal file
8
client/tests/scripts/fetch_parallel.js
Normal file
|
@ -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);
|
||||||
|
}
|
24
client/tests/scripts/test_websocket.js
Normal file
24
client/tests/scripts/test_websocket.js
Normal file
|
@ -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");
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
|
@ -1 +1 @@
|
||||||
Subproject commit 138ef0f73027b3d237ec84bee7ea43b4f30c8b74
|
Subproject commit bd7bca97b22023a1b49d0f5a09081ddcbb4ad49c
|
Loading…
Add table
Add a link
Reference in a new issue