8000 [Worker support] test for no cors headers by madhur-tandon · Pull Request #1374 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

[Worker support] test for no cors headers #1374

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 14 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
suggested changes
  • Loading branch information
madhur-tandon committed Apr 10, 2023
commit bfd5250fd3f3e6357de02339fae5796473db4b38
21 changes: 10 additions & 11 deletions pyscriptjs/tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,18 @@ def browser_type_launch_args(request):
return launch_options


class HTTPServer(SuperHTTPServer):
class DevServer(SuperHTTPServer):
"""
Class for wrapper to run SimpleHTTPServer on Thread.
Ctrl +Only Thread remains dead when terminated with C.
Keyboard Interrupt passes.
"""
def __init__(self, base_url, *args, **kwargs):
self.base_url = base_url
super().__init__(*args, **kwargs)

def __init__(self, host_and_port, handler_class):
self.handler_class = handler_class
super().__init__(host_and_port, handler_class)

def disable_cors(self):
self.handler_class.use_cors = False
def disable_cors_headers(self):
self.RequestHandlerClass.enable_cors_headers = False

def run(self):
try:
Expand All @@ -149,14 +148,14 @@ def run(self):
@pytest.fixture(scope="session")
def dev_server(logger):
class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
use_cors = True
enable_cors_headers = True

def end_headers(self):
self.send_my_headers()
SimpleHTTPRequestHandler.end_headers(self)

def send_my_headers(self):
if self.use_cors:
if self.enable_cors_headers:
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
self.send_header("Cross-Origin-Opener-Policy", "same-origin")

Expand All @@ -167,12 +166,12 @@ def log_message(self, fmt, *args):
base_url = f"http://{host}:{port}"

# serve_Run forever under thread
server = HTTPServer((host, port), MyHTTPRequestHandler)
server = DevServer(base_url, (host, port), MyHTTPRequestHandler)

thread = threading.Thread(None, server.run)
thread.start()

yield server, base_url # Transition to test here
yield server # Transition to test here

# End thread
server.shutdown()
Expand Down
11 changes: 6 additions & 5 deletions pyscriptjs/tests/integration/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ def init(self, request, tmpdir, logger, page):
if request.config.option.no_fake_server:
# use a real HTTP server. Note that as soon as we request the
# fixture, the server automatically starts in its own thread.
self.dev_server, self.http_server_addr = request.getfixturevalue(
self.dev_server = request.getfixturevalue(
"dev_server"
)
self.http_server_addr = self.dev_server.base_url
self.router = None
else:
# use the internal playwright routing
Expand Down Expand Up @@ -129,9 +130,9 @@ def init_page(self, page):

def disable_cors_headers(self):
if self.dev_server is None:
self.router.use_cors = False
self.router.enable_cors_headers = False
else:
self.dev_server.disable_cors()
self.dev_server.disable_cors_headers()

def run_js(self, code):
"""
Expand Down Expand Up @@ -658,7 +659,7 @@ class SmartRouter:
locally
"""

use_cors = True
enable_cors_headers = True

@dataclass
class CachedResponse:
Expand Down Expand Up @@ -740,7 +741,7 @@ def _router(self, route):
self.log_request(200, "fake_server", full_url)
assert url.path[0] == "/"
relative_path = url.path[1:]
if self.use_cors:
if self.enable_cors_headers:
headers = {
"Cross-Origin-Embedder-Policy": "require-corp",
"Cross-Origin-Opener-Policy": "same-origin",
Expand Down
4 changes: 2 additions & 2 deletions pyscriptjs/tests/integration/test_00_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def test_no_cors_headers(self):
self.disable_cors_headers()
doc = """
<html>
<script>console.log(typeof SharedArrayBuffer === "undefined")</script>
<script>console.log(crossOriginIsolated)</script>
</html>
"""
self.writefile("mytest.html", doc)
self.goto("mytest.html")
assert self.console.log.lines[-1] == "true"
assert self.console.log.lines[-1] == "false"

def test_await_with_run_js(self):
self.run_js(
Expand Down
0