diff --git a/src/Url.php b/src/Url.php index 23784a9..3c7556b 100644 --- a/src/Url.php +++ b/src/Url.php @@ -16,6 +16,11 @@ */ private string $host; + /** + * @var string|null - e.g. /my-feed + */ + private ?string $path; + /** * @param string $url url to parse * @throws \Fakeheal\CorsAnywhere\Exceptions\NoValidUrlProvidedException @@ -26,9 +31,9 @@ public function __construct(string $url) throw new NoValidUrlProvidedException(sprintf("'%s' is invalid URL.", $url)); } - ['scheme' => $scheme, 'host' => $host,] = parse_url($url); + $parsedUrl = parse_url($url); - if (! $host || ! $scheme) { + if (! isset($parsedUrl['scheme']) || ! $parsedUrl['scheme'] || ! isset($parsedUrl['host']) || ! $parsedUrl['host'] ) { throw new NoValidUrlProvidedException( sprintf( "'%s' is missing scheme (e.g. http(s) or host (e.g. example.com).", @@ -37,8 +42,9 @@ public function __construct(string $url) ); } - $this->scheme = $scheme; - $this->host = $host; + $this->scheme = $parsedUrl['scheme']; + $this->host = $parsedUrl['host']; + $this->path = $parsedUrl['path'] ?? null; } /** @@ -57,8 +63,21 @@ public function getHost(): string return $this->host; } + /** + * @return ?string + */ + public function getPath(): ?string + { + return $this->path; + } + + /** + * Builds URL as string using parsed schema, host & path. + * + * @return string + */ public function build(): string { - return sprintf("%s://%s", $this->getScheme(), $this->getHost()); + return sprintf("%s://%s%s", $this->getScheme(), $this->getHost(), $this->getPath() ?? ''); } } \ No newline at end of file diff --git a/tests/Unit/ProxyTest.php b/tests/Unit/ProxyTest.php index 5db58fa..2c910cd 100644 --- a/tests/Unit/ProxyTest.php +++ b/tests/Unit/ProxyTest.php @@ -104,4 +104,29 @@ expect(count($container))->toBe(1) ->and($container[0]['request']->getMethod())->toBe('POST') ->and($container[0]['request']->getBody()->getContents())->toBe('param1=value1¶m2=value2'); +}); + +it('uses correct URL for proxy request', function () { + // History middleware for "recording" requests made during test + // https://docs.guzzlephp.org/en/stable/testing.html#history-middleware + $container = []; + $history = Middleware::history($container); + $mock = new MockHandler([ + new Response(200, ['X-Foo' => 'Bar'], 'Hello, World'), + ]); + $handlerStack = HandlerStack::create($mock); + $handlerStack->push($history); + $client = new Client(['handler' => $handlerStack]); + + // Add custom body params to proxy request + $request = new Request(['url' => 'https://google.com/no-wrong-paths']); + + $response = new HttpFoundationResponse(); + + (new Proxy(['google.com'], $request, $response, $client)) + ->handle(); + + + expect(count($container))->toBe(1) + ->and((string)$container[0]['request']->getUri())->toBe('https://google.com/no-wrong-paths'); }); \ No newline at end of file diff --git a/tests/Unit/UrlTest.php b/tests/Unit/UrlTest.php index 9febcd0..0be6212 100644 --- a/tests/Unit/UrlTest.php +++ b/tests/Unit/UrlTest.php @@ -33,4 +33,12 @@ expect($url->getHost())->toBe('www.google.com') ->and($url->getScheme())->toBe('http') ->and($url->build())->toBe('http://www.google.com'); +}); + +it('url parses path properly', function() { + $url = new Url('http://www.google.com/metallica-path'); + + expect($url->getHost())->toBe('www.google.com') + ->and($url->getPath())->toBe('/metallica-path') + ->and($url->build())->toBe('http://www.google.com/metallica-path'); }); \ No newline at end of file