From 2a28eb6b9431d49bdc8d6d651df2d6e9e8eb0fcc Mon Sep 17 00:00:00 2001 From: Angger Pradana Date: Fri, 24 Nov 2023 19:23:56 +0700 Subject: [PATCH] feat: add shorthand url parse from request (#246) --- src/System/Http/Url.php | 5 +++++ tests/Http/UrlTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/System/Http/Url.php b/src/System/Http/Url.php index 2781dec1..c8bdc929 100644 --- a/src/System/Http/Url.php +++ b/src/System/Http/Url.php @@ -52,6 +52,11 @@ public static function parse(string $url): self return new self(parse_url($url)); } + public static function fromRequest(Request $from): self + { + return new self(parse_url($from->getUrl())); + } + /** * @return string|null */ diff --git a/tests/Http/UrlTest.php b/tests/Http/UrlTest.php index 1a2d431d..73eea475 100644 --- a/tests/Http/UrlTest.php +++ b/tests/Http/UrlTest.php @@ -1,6 +1,7 @@ assertEquals('anchor', $url->fragment()); } + /** + * @test + */ + public function itUrlParseUsingRequest(): void + { + $request = new Request('http://username:password@hostname:9090/path?arg=value#anchor'); + $url = Url::fromRequest($request); + + $this->assertEquals('http', $url->schema()); + $this->assertEquals('hostname', $url->host()); + $this->assertEquals(9090, $url->port()); + $this->assertEquals('username', $url->user()); + $this->assertEquals('password', $url->password()); + $this->assertEquals('/path', $url->path()); + $this->assertEquals(['arg' => 'value'], $url->query()); + $this->assertEquals('anchor', $url->fragment()); + } + /** * @test */