Repository: pyscript/pyscript
File: pyscriptjs/tests/integration/conftest.py (Python)

Code before:

@pytest.fixture(scope="session")
def browser_type_launch_args(request):
    """
    Override the browser_type_launch_args defined by pytest-playwright to
    support --devtools.

    NOTE: this has been tested with pytest-playwright==0.3.0. It might break
    with newer versions of it.
    """
    # this calls the "original" fixture defined by pytest_playwright.py
    launch_options = request.getfixturevalue("browser_type_launch_args")
    if request.config.option.dev:
        launch_options["devtools"] = True
    return launch_options


class HTTPServer(SuperHTTPServer):
    """
    Class for wrapper to run SimpleHTTPServer on Thread.
    Ctrl +Only Thread remains dead when terminated with C.
    Keyboard Interrupt passes.
    """

    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 run(self):
        try:
            self.serve_forever()
        except KeyboardInterrupt:
            pass
        finally:
            self.server_close()


@pytest.fixture(scope="session")
def dev_server(logger):
    class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
        use_cors = True

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

        def send_my_headers(self):
            if self.use_cors:

Review comment (suggestion):
this is superfluous. `HTTPServer` is a subclass of `socketserver.SocketServer`, and the class is already stored as `RequestHandlerClass`:
https://github.com/antocuni/cpython/blob/deaf509e8fc6e0363bd6f26d52ad42f976ec42f2/Lib/socketserver.py#L201-L206




Code after:

@pytest.fixture(scope="session")
def browser_type_launch_args(request):
    """
    Override the browser_type_launch_args defined by pytest-playwright to
    support --devtools.

    NOTE: this has been tested with pytest-playwright==0.3.0. It might break
    with newer versions of it.
    """
    # this calls the "original" fixture defined by pytest_playwright.py
    launch_options = request.getfixturevalue("browser_type_launch_args")
    if request.config.option.dev:
        launch_options["devtools"] = True
    return launch_options


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 run(self):
        try:
            self.serve_forever()
        except KeyboardInterrupt:
            pass
        finally:
            self.server_close()


@pytest.fixture(scope="session")
def dev_server(logger):
    class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
        enable_cors_headers = True

        @classmethod
        def my_headers(cls):
            if cls.enable_cors_headers:
                return {
                    "Cross-Origin-Embedder-Policy": "require-corp",
                    "Cross-Origin-Opener-Policy": "same-origin",
                }
            return {}