diff --git a/src/local_pathfinding/local_pathfinding/global_path.py b/src/local_pathfinding/local_pathfinding/global_path.py index 1128a76c8..620a70ee4 100644 --- a/src/local_pathfinding/local_pathfinding/global_path.py +++ b/src/local_pathfinding/local_pathfinding/global_path.py @@ -140,11 +140,12 @@ def get_path(file_path: str) -> Path: return path -def post_path(path: Path) -> bool: +def post_path(path: Path, url: str = PATH_URL) -> bool: """Sends the global path to NET via POST request. Args: path (Path): The global path. + url (str): URL to post path to. Should always use default value except for tests. Returns: bool: Whether or not the global path was successfully posted. @@ -160,8 +161,9 @@ def post_path(path: Path) -> bool: data = {"waypoints": waypoints, "timestamp": timestamp} json_data = json.dumps(data).encode("utf-8") + try: - urlopen(PATH_URL, json_data) + urlopen(url, json_data) return True except HTTPError as http_error: print(f"HTTP Error: {http_error.code}") diff --git a/src/local_pathfinding/test/post_server.py b/src/local_pathfinding/test/post_server.py index 7681b63e3..a2fd3582a 100644 --- a/src/local_pathfinding/test/post_server.py +++ b/src/local_pathfinding/test/post_server.py @@ -8,6 +8,9 @@ import threading from http.server import BaseHTTPRequestHandler, HTTPServer +TEST_PORT = 8085 +POST_TEST_URL = "http://localhost:" + str(TEST_PORT) + "/global-path" + class CustomRequestHandler(BaseHTTPRequestHandler): def _set_response(self, status_code=200, content_type="application/json"): @@ -32,7 +35,7 @@ def do_POST(self): ) -def run_server(port=8081) -> HTTPServer: +def run_server(port=TEST_PORT) -> HTTPServer: server_address = ("localhost", port) httpd = HTTPServer(server_address, CustomRequestHandler) diff --git a/src/local_pathfinding/test/test_global_path.py b/src/local_pathfinding/test/test_global_path.py index ceca31e7f..a01d88a46 100644 --- a/src/local_pathfinding/test/test_global_path.py +++ b/src/local_pathfinding/test/test_global_path.py @@ -1,6 +1,6 @@ import os -import post_server +import post_server as ps import pytest from custom_interfaces.msg import HelperLatLon, Path @@ -338,11 +338,11 @@ def test_post_path(global_path: Path): """ # Launch http server - server = post_server.run_server() + server = ps.run_server() - assert post_path(global_path), "Failed to post global path" + assert post_path(global_path, url=ps.POST_TEST_URL), "Failed to post global path" - post_server.shutdown_server(httpd=server) + ps.shutdown_server(httpd=server) # ------------------------- TEST WRITE_TO_FILE ------------------------------