From 35ed50becc91bb688ab5b00023a4ccf52cf133d7 Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sat, 11 Feb 2023 00:03:47 +0000 Subject: [PATCH] fix: Fix Windows tests get conditional global hosts as windows does not allow 0.0.0.0 host --- integration_tests/conftest.py | 9 +++++---- integration_tests/helpers/__init__.py | 0 .../{ => helpers}/http_methods_helpers.py | 0 integration_tests/helpers/network_helpers.py | 14 ++++++++++++++ integration_tests/test_base_url.py | 7 +++++-- integration_tests/test_basic_routes.py | 2 +- integration_tests/test_delete_requests.py | 2 +- integration_tests/test_file_download.py | 2 +- integration_tests/test_get_requests.py | 2 +- integration_tests/test_middlewares.py | 2 +- integration_tests/test_patch_requests.py | 2 +- integration_tests/test_post_requests.py | 2 +- integration_tests/test_put_requests.py | 2 +- integration_tests/test_status_code.py | 2 +- robyn/__init__.py | 1 + 15 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 integration_tests/helpers/__init__.py rename integration_tests/{ => helpers}/http_methods_helpers.py (100%) create mode 100644 integration_tests/helpers/network_helpers.py diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index 0827f8cab..4b174f4ac 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -3,15 +3,16 @@ import signal import socket import subprocess -import sys import time from typing import List +import platform import pytest +from helpers.network_helpers import get_network_host def spawn_process(command: List[str]) -> subprocess.Popen: - if sys.platform.startswith("win32"): + if platform.system() == "Windows": command[0] = "python" process = subprocess.Popen( command, shell=True, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP @@ -22,7 +23,7 @@ def spawn_process(command: List[str]) -> subprocess.Popen: def kill_process(process: subprocess.Popen) -> None: - if sys.platform.startswith("win32"): + if platform.system() == "Windows": process.send_signal(signal.CTRL_BREAK_EVENT) process.kill() return @@ -84,7 +85,7 @@ def default_session(): @pytest.fixture(scope="session") def global_session(): - domain = "0.0.0.0" + domain = get_network_host() port = 8080 os.environ["ROBYN_URL"] = domain process = start_server(domain, port) diff --git a/integration_tests/helpers/__init__.py b/integration_tests/helpers/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/integration_tests/http_methods_helpers.py b/integration_tests/helpers/http_methods_helpers.py similarity index 100% rename from integration_tests/http_methods_helpers.py rename to integration_tests/helpers/http_methods_helpers.py diff --git a/integration_tests/helpers/network_helpers.py b/integration_tests/helpers/network_helpers.py new file mode 100644 index 000000000..1dc20a3a9 --- /dev/null +++ b/integration_tests/helpers/network_helpers.py @@ -0,0 +1,14 @@ +import socket +import platform + + +def get_network_host(): + hostname = socket.gethostname() + ip_address = socket.gethostbyname(hostname) + # windows return ip address else return 0.0.0.0 + # windows doesn't support 0.0.0.0 + + if platform.system() == "Windows": + return ip_address + else: + return "0.0.0.0" diff --git a/integration_tests/test_base_url.py b/integration_tests/test_base_url.py index 0bf12defc..146230c9e 100644 --- a/integration_tests/test_base_url.py +++ b/integration_tests/test_base_url.py @@ -2,6 +2,8 @@ import requests +from helpers.network_helpers import get_network_host + def test_default_url_index_request(default_session): BASE_URL = "http://127.0.0.1:8080" @@ -17,9 +19,10 @@ def test_local_index_request(session): def test_global_index_request(global_session): - BASE_URL = "http://0.0.0.0:8080" + host = get_network_host() + BASE_URL = f"http://{host}:8080" res = requests.get(f"{BASE_URL}") - assert os.getenv("ROBYN_URL") == "0.0.0.0" + assert os.getenv("ROBYN_URL") == f"{host}" assert res.status_code == 200 diff --git a/integration_tests/test_basic_routes.py b/integration_tests/test_basic_routes.py index 02b31cbcb..d07c723cf 100644 --- a/integration_tests/test_basic_routes.py +++ b/integration_tests/test_basic_routes.py @@ -7,7 +7,7 @@ import pytest -from http_methods_helpers import get +from helpers.http_methods_helpers import get @pytest.mark.parametrize( diff --git a/integration_tests/test_delete_requests.py b/integration_tests/test_delete_requests.py index 46f048100..818adc7eb 100644 --- a/integration_tests/test_delete_requests.py +++ b/integration_tests/test_delete_requests.py @@ -1,5 +1,5 @@ import pytest -from http_methods_helpers import delete +from helpers.http_methods_helpers import delete @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_file_download.py b/integration_tests/test_file_download.py index 2984ca54e..a6ec8a4e9 100644 --- a/integration_tests/test_file_download.py +++ b/integration_tests/test_file_download.py @@ -1,5 +1,5 @@ import pytest -from http_methods_helpers import get +from helpers.http_methods_helpers import get @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_get_requests.py b/integration_tests/test_get_requests.py index 1a7d42919..6792b5004 100644 --- a/integration_tests/test_get_requests.py +++ b/integration_tests/test_get_requests.py @@ -1,7 +1,7 @@ import pytest from requests import Response -from http_methods_helpers import get +from helpers.http_methods_helpers import get @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_middlewares.py b/integration_tests/test_middlewares.py index f6b5bc30f..608c4da85 100644 --- a/integration_tests/test_middlewares.py +++ b/integration_tests/test_middlewares.py @@ -1,6 +1,6 @@ import pytest -from http_methods_helpers import get +from helpers.http_methods_helpers import get @pytest.mark.skip(reason="Fix middleware request headers modification") diff --git a/integration_tests/test_patch_requests.py b/integration_tests/test_patch_requests.py index ef7a6f922..6aa90b6c1 100644 --- a/integration_tests/test_patch_requests.py +++ b/integration_tests/test_patch_requests.py @@ -1,5 +1,5 @@ import pytest -from http_methods_helpers import patch +from helpers.http_methods_helpers import patch @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_post_requests.py b/integration_tests/test_post_requests.py index a78cf0053..5e538e5c8 100644 --- a/integration_tests/test_post_requests.py +++ b/integration_tests/test_post_requests.py @@ -1,5 +1,5 @@ import pytest -from http_methods_helpers import post +from helpers.http_methods_helpers import post @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_put_requests.py b/integration_tests/test_put_requests.py index 395ac20d3..b4e7b77d9 100644 --- a/integration_tests/test_put_requests.py +++ b/integration_tests/test_put_requests.py @@ -1,5 +1,5 @@ import pytest -from http_methods_helpers import put +from helpers.http_methods_helpers import put @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_status_code.py b/integration_tests/test_status_code.py index 898cc9cad..17061ce13 100644 --- a/integration_tests/test_status_code.py +++ b/integration_tests/test_status_code.py @@ -1,5 +1,5 @@ import pytest -from http_methods_helpers import get +from helpers.http_methods_helpers import get def test_404_status_code(session): diff --git a/robyn/__init__.py b/robyn/__init__.py index bfc24987c..14dda2b6e 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -2,6 +2,7 @@ import logging import multiprocess as mp import os +from platform import platform import signal from typing import Callable, List, Optional