From c4efe828f0d1ecf9611e4ff5b8940d03c8349a12 Mon Sep 17 00:00:00 2001 From: Caleb Aikens Date: Wed, 11 Sep 2024 10:20:53 -0400 Subject: [PATCH 1/3] fix(tests): remove old xhr.simple test, add new test that uses a local server --- tests/python/test_xhr.py | 97 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/python/test_xhr.py diff --git a/tests/python/test_xhr.py b/tests/python/test_xhr.py new file mode 100644 index 00000000..40d99e62 --- /dev/null +++ b/tests/python/test_xhr.py @@ -0,0 +1,97 @@ +from http.server import HTTPServer, BaseHTTPRequestHandler +import pythonmonkey as pm +import threading +import asyncio +import json + +def test_xhr(): + class TestHTTPRequestHandler(BaseHTTPRequestHandler): + def log_request(self, code: int | str = "-", size: int | str = "-") -> None: + return + + def do_GET(self): + self.send_response(200) + self.end_headers() + self.wfile.write(b"get response") + + def do_POST(self): + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + length = int(self.headers.get('Content-Length')) + json_string = self.rfile.read(length).decode("utf-8") + parameter_dict = json.loads(json_string) + parameter_dict["User-Agent"] = self.headers['User-Agent'] + data = json.dumps(parameter_dict).encode("utf-8") + self.wfile.write(data) + + httpd = HTTPServer(('localhost', 4001), TestHTTPRequestHandler) + thread = threading.Thread(target = httpd.serve_forever) + thread.daemon = True + thread.start() + + async def async_fn(): + assert "get response" == await pm.eval(""" + new Promise(function (resolve, reject) { + let xhr = new XMLHttpRequest(); + xhr.open('GET', 'http://localhost:4001'); + + xhr.onload = function () + { + if (this.status >= 200 && this.status < 300) + { + resolve(this.response); + } + else + { + reject(new Error(JSON.stringify({ + status: this.status, + statusText: this.statusText + }))); + } + }; + + xhr.onerror = function (ev) + { + reject(ev.error); + }; + xhr.send(); + }); + """) + + post_result = await pm.eval(""" + new Promise(function (resolve, reject) + { + let xhr = new XMLHttpRequest(); + xhr.open('POST', 'http://localhost:4001'); + + xhr.onload = function () + { + if (this.status >= 200 && this.status < 300) + { + resolve(this.response); + } + else + { + reject(new Error(JSON.stringify({ + status: this.status, + statusText: this.statusText + }))); + } + }; + + xhr.onerror = function (ev) + { + console.log(ev) + reject(ev.error); + }; + + xhr.send(JSON.stringify({fromPM: "snakesandmonkeys"})); + }) + """) + + result_json = json.loads(post_result) + assert result_json["fromPM"] == "snakesandmonkeys" + assert result_json["User-Agent"].startswith("Python/") + httpd.shutdown() + asyncio.run(async_fn()) \ No newline at end of file From 67f92487502896fcd1c0f8de96279a27df1907c6 Mon Sep 17 00:00:00 2001 From: Caleb Aikens Date: Wed, 11 Sep 2024 10:37:20 -0400 Subject: [PATCH 2/3] remove old test --- tests/js/xhr.simple | 136 -------------------------------------------- 1 file changed, 136 deletions(-) delete mode 100644 tests/js/xhr.simple diff --git a/tests/js/xhr.simple b/tests/js/xhr.simple deleted file mode 100644 index 8c61e6dc..00000000 --- a/tests/js/xhr.simple +++ /dev/null @@ -1,136 +0,0 @@ -/** - * @file xhr.simple - * - * Simple smoke test which ensures that XMLHttpRequest is initialized, that the - * constructor works, and that it can send requests and receive responses. - * - * @author Caleb Aikens, caleb@distributive.network - * @date September 2023 - */ - -const expectedBody = ` - - - Example Domain - - - - - - - - -
-

Example Domain

-

This domain is for use in illustrative examples in documents. You may use this - domain in literature without prior coordination or asking for permission.

-

More information...

-
- - -`; - -new Promise(function (resolve, reject) -{ - let xhr = new XMLHttpRequest(); - xhr.open('GET', 'http://www.example.org/'); - - xhr.onload = function () - { - if (this.status >= 200 && this.status < 300) - { - resolve(this.response); - } - else - { - reject(new Error(JSON.stringify({ - status: this.status, - statusText: this.statusText - }))); - } - }; - - xhr.onerror = function (ev) - { - reject(ev.error); - }; - - xhr.send(); -}).then((value) => -{ - if (value !== expectedBody) - { - console.error('expected ', expectedBody, ' but got ', value); - throw new Error('Test failed'); - } - console.log('Test passed'); -}).catch((error) => -{ - throw error; -}); - - -new Promise(function (resolve, reject) -{ - let xhr = new XMLHttpRequest(); - xhr.open('POST', 'http://httpbin.org/post'); - - xhr.onload = function () - { - if (this.status >= 200 && this.status < 300) - { - resolve(this.response); - } - else - { - reject(new Error(JSON.stringify({ - status: this.status, - statusText: this.statusText - }))); - } - }; - - xhr.onerror = function (ev) - { - reject(ev.error); - }; - - xhr.send(); -}).then((value) => -{ - value = JSON.parse(value); - if (!value['headers']['User-Agent'].startsWith('Python/')) - { - console.error('expected Python/* User-Agent, but got ', value.headers['User-Agent']); - } - console.log('Test passed'); -}).catch((error) => -{ - throw error; -}); \ No newline at end of file From 225c7531257c03521a30ff1dd8ec8f2186871074 Mon Sep 17 00:00:00 2001 From: Caleb Aikens Date: Wed, 11 Sep 2024 11:10:17 -0400 Subject: [PATCH 3/3] fix(tests): fix syntax error in test_xhr.py for python3.8-3.9 --- tests/python/test_xhr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/test_xhr.py b/tests/python/test_xhr.py index 40d99e62..990193eb 100644 --- a/tests/python/test_xhr.py +++ b/tests/python/test_xhr.py @@ -6,7 +6,7 @@ def test_xhr(): class TestHTTPRequestHandler(BaseHTTPRequestHandler): - def log_request(self, code: int | str = "-", size: int | str = "-") -> None: + def log_request(self, *args) -> None: return def do_GET(self):