Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add host(), httpHost(), schemeAndHttpHost() to Request #42797

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,36 @@ public function userAgent()
return $this->headers->get('User-Agent');
}

/**
* Get the host name.
*
* @return string
*/
public function host()
{
return $this->getHost();
}

/**
* Get the HTTP host being requested.
*
* @return string
*/
public function httpHost()
{
return $this->getHttpHost();
}

/**
* Get the scheme and HTTP host.
*
* @return string
*/
public function schemeAndHttpHost()
{
return $this->getSchemeAndHttpHost();
}

/**
* Merge new input into the current request's input array.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,45 @@ public function testUserAgentMethod()
$this->assertSame('Laravel', $request->userAgent());
}

public function testHostMethod()
{
$request = Request::create('http://example.com');
$this->assertSame('example.com', $request->host());

$request = Request::create('https://example.com');
$this->assertSame('example.com', $request->host());
}

public function testHttpHostMethod()
{
$request = Request::create('http://example.com');
$this->assertSame('example.com', $request->httpHost());

$request = Request::create('https://example.com');
$this->assertSame('example.com', $request->httpHost());

$request = Request::create('http://example.com:8080');
$this->assertSame('example.com:8080', $request->httpHost());

$request = Request::create('https://example.com:8080');
$this->assertSame('example.com:8080', $request->httpHost());
}

public function testSchemeAndHttpHostMethod()
{
$request = Request::create('http://example.com');
$this->assertSame('http://example.com', $request->schemeAndHttpHost());

$request = Request::create('https://example.com');
$this->assertSame('https://example.com', $request->schemeAndHttpHost());

$request = Request::create('http://example.com:8080');
$this->assertSame('http://example.com:8080', $request->schemeAndHttpHost());

$request = Request::create('https://example.com:8080');
$this->assertSame('https://example.com:8080', $request->schemeAndHttpHost());
}

public function testHasMethod()
{
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);
Expand Down