-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #437 from Distributive-Network/caleb/fix/xhr-test
Fix XHR Test
- Loading branch information
Showing
2 changed files
with
97 additions
and
136 deletions.
There are no files selected for viewing
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, *args) -> 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()) |