-
Notifications
You must be signed in to change notification settings - Fork 41
Fix XHR Test #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix XHR Test #437
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4efe82
fix(tests): remove old xhr.simple test, add new test that uses a loca…
zollqir 67f9248
remove old test
zollqir c4e53e6
Merge branch 'main' into caleb/fix/xhr-test
philippedistributive 225c753
fix(tests): fix syntax error in test_xhr.py for python3.8-3.9
zollqir 169e083
Merge branch 'main' into caleb/fix/xhr-test
Xmader a00347b
Merge branch 'main' into caleb/fix/xhr-test
Xmader File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.