Skip to content

Commit

Permalink
feat: include url path for proxying requests
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeheal committed Jun 15, 2023
1 parent cd3875f commit 63a816f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).",
Expand All @@ -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;
}

/**
Expand All @@ -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() ?? '');
}
}
25 changes: 25 additions & 0 deletions tests/Unit/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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&param2=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');
});
8 changes: 8 additions & 0 deletions tests/Unit/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit 63a816f

Please sign in to comment.