diff --git a/src/Support/Utils.php b/src/Support/Utils.php index 58ec2700..b9f1ab9b 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -38,7 +38,9 @@ class Utils public static function apiUrl(string $path, ?array $queryParams = [], int $port = 80): string { $isLocalDevelopment = app()->environment(['local', 'development']); - $baseURL = url($path, $queryParams, !$isLocalDevelopment); + // Laravel's url() renders extra parameters as path segments, not a query string, + // so the query string must be built here + $baseURL = url($path, [], !$isLocalDevelopment); // Check if default port is used to avoid appending it unnecessarily if (!in_array($port, [80, 443])) { @@ -52,6 +54,10 @@ public static function apiUrl(string $path, ?array $queryParams = [], int $port } } + if (!empty($queryParams)) { + $baseURL .= (str_contains($baseURL, '?') ? '&' : '?') . http_build_query($queryParams); + } + return $baseURL; } diff --git a/tests/Pest.php b/tests/Pest.php index 28ddbb13..77acb87e 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -311,11 +311,15 @@ function storage_path(string $path = ''): string if (!function_exists('url')) { function url(string $path = '', mixed $parameters = [], ?bool $secure = null): string { + // Mirrors Illuminate\Routing\UrlGenerator::to(): extra parameters become + // rawurlencoded path segments with keys discarded, NOT a query string $base = $secure ? 'https://fleetbase.test' : 'http://fleetbase.test'; $path = '/' . ltrim($path, '/'); if (is_array($parameters) && $parameters !== []) { - return $base . $path . '?' . http_build_query($parameters); + $tail = implode('/', array_map('rawurlencode', array_values($parameters))); + + return $base . rtrim($path, '/') . '/' . $tail; } return $base . $path;