Skip to content

Commit 73fd999

Browse files
committed
Merge pull request #76 from johnkary/portWithHost
Add port number to generated absolute URLs
2 parents b93f01e + 527b80c commit 73fd999

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

Extractor/ExposedRoutesExtractor.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,26 @@ public function getPrefix($locale)
128128
*/
129129
public function getHost()
130130
{
131-
return $this->router->getContext()->getHost();
131+
$requestContext = $this->router->getContext();
132+
133+
$host = $requestContext->getHost();
134+
135+
if ($this->usesNonStandardPort()) {
136+
$method = sprintf('get%sPort', ucfirst($requestContext->getScheme()));
137+
$host .= ':' . $requestContext->$method();
138+
}
139+
140+
return $host;
141+
}
142+
143+
/**
144+
* Check whether server is serving this request from a non-standard port.
145+
*
146+
* @return bool
147+
*/
148+
protected function usesNonStandardPort()
149+
{
150+
return $this->usesNonStandardHttpPort() || $this->usesNonStandardHttpsPort();
132151
}
133152

134153
/**
@@ -180,4 +199,24 @@ protected function buildPattern()
180199

181200
return implode($patterns, '|');
182201
}
202+
203+
/**
204+
* Checks whether server is serving HTTP over a non-standard port.
205+
*
206+
* @return bool
207+
*/
208+
private function usesNonStandardHttpPort()
209+
{
210+
return 'http' === $this->getScheme() && '80' != $this->router->getContext()->getHttpPort();
211+
}
212+
213+
/**
214+
* Checks whether server is serving HTTPS over a non-standard port.
215+
*
216+
* @return bool
217+
*/
218+
private function usesNonStandardHttpsPort()
219+
{
220+
return 'https' === $this->getScheme() && '443' != $this->router->getContext()->getHttpsPort();
221+
}
183222
}

Extractor/ExposedRoutesExtractorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getBaseUrl();
4242
public function getPrefix($locale);
4343

4444
/**
45-
* Get the host from RequestContext
45+
* Get the host and applicable port from RequestContext
4646
*
4747
* @return string
4848
*/

Tests/Extractor/ExposedRoutesExtractorTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor;
1515
use FOS\JsRoutingBundle\Extractor\ExtractedRoute;
16+
use Symfony\Component\Routing\RequestContext;
1617
use Symfony\Component\Routing\Route;
1718
use Symfony\Component\HttpKernel\Kernel;
1819

@@ -105,6 +106,54 @@ public function testGetCachePath()
105106
$this->assertEquals($this->cacheDir . DIRECTORY_SEPARATOR . 'fosJsRouting' . DIRECTORY_SEPARATOR . 'data.json', $extractor->getCachePath(''));
106107
}
107108

109+
/**
110+
* @dataProvider provideTestGetHostOverHttp
111+
*/
112+
public function testGetHostOverHttp($host, $httpPort, $expected)
113+
{
114+
$requestContext = new RequestContext('/app_dev.php', 'GET', $host, 'http', $httpPort);
115+
116+
$router = $this->getMock('Symfony\\Component\\Routing\\Router', array(), array(), '', false);
117+
$router->expects($this->atLeastOnce())
118+
->method('getContext')
119+
->will($this->returnValue($requestContext));
120+
121+
$extractor = new ExposedRoutesExtractor($router, array(), $this->cacheDir, array());
122+
123+
$this->assertEquals($expected, $extractor->getHost());
124+
}
125+
public function provideTestGetHostOverHttp()
126+
{
127+
return array(
128+
'HTTP Standard' => array('127.0.0.1', 80, '127.0.0.1'),
129+
'HTTP Non-Standard' => array('127.0.0.1', 8888, '127.0.0.1:8888'),
130+
);
131+
}
132+
133+
/**
134+
* @dataProvider provideTestGetHostOverHttps
135+
*/
136+
public function testGetHostOverHttps($host, $httpsPort, $expected)
137+
{
138+
$requestContext = new RequestContext('/app_dev.php', 'GET', $host, 'https', 80, $httpsPort);
139+
140+
$router = $this->getMock('Symfony\\Component\\Routing\\Router', array(), array(), '', false);
141+
$router->expects($this->atLeastOnce())
142+
->method('getContext')
143+
->will($this->returnValue($requestContext));
144+
145+
$extractor = new ExposedRoutesExtractor($router, array(), $this->cacheDir, array());
146+
147+
$this->assertEquals($expected, $extractor->getHost());
148+
}
149+
public function provideTestGetHostOverHttps()
150+
{
151+
return array(
152+
'HTTPS Standard' => array('127.0.0.1', 443, '127.0.0.1'),
153+
'HTTPS Non-Standard' => array('127.0.0.1', 9876, '127.0.0.1:9876'),
154+
);
155+
}
156+
108157
/**
109158
* Get a mock object which represents a Router.
110159
* @return \Symfony\Component\Routing\Router

0 commit comments

Comments
 (0)