Skip to content

Commit

Permalink
RequestFactory::createHttpRequest() renamed to fromGlobals()
Browse files Browse the repository at this point in the history
To be consistent with ResponseFactory::fromGlobals()
  • Loading branch information
dg committed Oct 17, 2019
1 parent 85e19f3 commit 0011929
Show file tree
Hide file tree
Showing 23 changed files with 85 additions and 78 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ And then we let the factory generate a new `httpRequest` and we store it in a sy

```php
// $container is a system container
$container->addService('httpRequest', $requestFactory->createHttpRequest());
$container->addService('httpRequest', $requestFactory->fromGlobals());
```


Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/HttpDI/HttpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function loadConfiguration()
->addSetup('setProxy', [$config->proxy]);

$builder->addDefinition($this->prefix('request'))
->setFactory('@Nette\Http\RequestFactory::createHttpRequest');
->setFactory('@Nette\Http\RequestFactory::fromGlobals');

$response = $builder->addDefinition($this->prefix('response'))
->setFactory(Nette\Http\Response::class);
Expand Down
9 changes: 8 additions & 1 deletion src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function setProxy($proxy)
/**
* Creates current HttpRequest object.
*/
public function createHttpRequest(): Request
public function fromGlobals(): Request
{
$url = new Url;
$this->getServer($url);
Expand Down Expand Up @@ -345,4 +345,11 @@ private function useNonstandardProxy(Url $url, &$remoteAddr, &$remoteHost): void
}
}
}


/** @deprecated */
public function createHttpRequest(): Request
{
return $this->fromGlobals();
}
}
2 changes: 1 addition & 1 deletion tests/Http/Request.files.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ $_FILES = [
];

$factory = new Http\RequestFactory;
$request = $factory->createHttpRequest();
$request = $factory->fromGlobals();

Assert::type(Nette\Http\FileUpload::class, $request->files['file1']);
Assert::type(Nette\Http\FileUpload::class, $request->files['file2'][2]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Request.files2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $_FILES = [
];

$factory = new Http\RequestFactory;
$request = $factory->createHttpRequest();
$request = $factory->fromGlobals();

Assert::type('array', $request->files['files']);
Assert::count(1, $request->files['files']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Request.invalidEncoding.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ $_FILES = [
test(function () { // unfiltered data
$factory = new Http\RequestFactory;
$factory->setBinary();
$request = $factory->createHttpRequest();
$request = $factory->fromGlobals();

Assert::same($request->getQuery('invalid'), INVALID);
Assert::same($request->getQuery('control'), CONTROL_CHARACTERS);
Expand Down Expand Up @@ -96,7 +96,7 @@ test(function () { // unfiltered data

test(function () { // filtered data
$factory = new Http\RequestFactory;
$request = $factory->createHttpRequest();
$request = $factory->fromGlobals();

Assert::same('', $request->getQuery('invalid'));
Assert::same('ABC', $request->getQuery('control'));
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Request.request.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test(function () {
$factory = new Http\RequestFactory;
$factory->urlFilters['path'] = ['#%20#' => ''];
$factory->urlFilters['url'] = ['#[.,)]\z#' => ''];
$request = $factory->createHttpRequest();
$request = $factory->fromGlobals();

Assert::same('GET', $request->getMethod());
Assert::true($request->isSecured());
Expand Down Expand Up @@ -59,7 +59,7 @@ test(function () {
$factory = new Http\RequestFactory;
$factory->urlFilters['path'] = [];
$factory->urlFilters['url'] = [];
$request = $factory->createHttpRequest();
$request = $factory->fromGlobals();

Assert::same('https', $request->getUrl()->scheme);
Assert::same('', $request->getUrl()->user);
Expand Down
12 changes: 6 additions & 6 deletions tests/Http/RequestFactory.host.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@ $_SERVER = [
'HTTP_HOST' => 'localhost',
];
$factory = new RequestFactory;
Assert::same('http://localhost/', (string) $factory->createHttpRequest()->getUrl());
Assert::same('http://localhost/', (string) $factory->fromGlobals()->getUrl());


$_SERVER = [
'HTTP_HOST' => 'www-x.nette.org',
];
$factory = new RequestFactory;
Assert::same('http://www-x.nette.org/', (string) $factory->createHttpRequest()->getUrl());
Assert::same('http://www-x.nette.org/', (string) $factory->fromGlobals()->getUrl());


$_SERVER = [
'HTTP_HOST' => '192.168.0.1:8080',
];
$factory = new RequestFactory;
Assert::same('http://192.168.0.1:8080/', (string) $factory->createHttpRequest()->getUrl());
Assert::same('http://192.168.0.1:8080/', (string) $factory->fromGlobals()->getUrl());


$_SERVER = [
'HTTP_HOST' => '[::1aF]:8080',
];
$factory = new RequestFactory;
Assert::same('http://[::1af]:8080/', (string) $factory->createHttpRequest()->getUrl());
Assert::same('http://[::1af]:8080/', (string) $factory->fromGlobals()->getUrl());


$_SERVER = [
'HTTP_HOST' => "a.cz\n",
];
$factory = new RequestFactory;
Assert::same('http:/', (string) $factory->createHttpRequest()->getUrl());
Assert::same('http:/', (string) $factory->fromGlobals()->getUrl());


$_SERVER = [
'HTTP_HOST' => 'AB',
];
$factory = new RequestFactory;
Assert::same('http://ab/', (string) $factory->createHttpRequest()->getUrl());
Assert::same('http://ab/', (string) $factory->fromGlobals()->getUrl());
6 changes: 3 additions & 3 deletions tests/Http/RequestFactory.method.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ $_SERVER = [
'HTTP_X_HTTP_METHOD_OVERRIDE' => 'PATCH',
];
$factory = new RequestFactory;
Assert::same('GET', $factory->createHttpRequest()->getMethod());
Assert::same('GET', $factory->fromGlobals()->getMethod());


$_SERVER = [
'REQUEST_METHOD' => 'POST',
'HTTP_X_HTTP_METHOD_OVERRIDE' => 'PATCH',
];
$factory = new RequestFactory;
Assert::same('PATCH', $factory->createHttpRequest()->getMethod());
Assert::same('PATCH', $factory->fromGlobals()->getMethod());


$_SERVER = [
'REQUEST_METHOD' => 'POST',
'HTTP_X_HTTP_METHOD_OVERRIDE' => ' *',
];
$factory = new RequestFactory;
Assert::same('POST', $factory->createHttpRequest()->getMethod());
Assert::same('POST', $factory->fromGlobals()->getMethod());
4 changes: 2 additions & 2 deletions tests/Http/RequestFactory.port.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RequestFactoryPortTest extends Tester\TestCase
$_SERVER = $server;

$factory = new Nette\Http\RequestFactory;
Assert::same($expectedPort, $factory->createHttpRequest()->getUrl()->getPort());
Assert::same($expectedPort, $factory->fromGlobals()->getUrl()->getPort());
}


Expand Down Expand Up @@ -61,7 +61,7 @@ class RequestFactoryPortTest extends Tester\TestCase

$factory = new Nette\Http\RequestFactory;
$factory->setProxy(['10.0.0.1']);
Assert::same($expectedPort, $factory->createHttpRequest()->getUrl()->getPort());
Assert::same($expectedPort, $factory->fromGlobals()->getUrl()->getPort());
}


Expand Down
30 changes: 15 additions & 15 deletions tests/Http/RequestFactory.proxy.forwarded.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ test(function () {

$factory = new RequestFactory;
$factory->setProxy('127.0.0.1');
Assert::same('127.0.0.3', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('localhost', $factory->createHttpRequest()->getRemoteHost());
Assert::same('127.0.0.3', $factory->fromGlobals()->getRemoteAddress());
Assert::same('localhost', $factory->fromGlobals()->getRemoteHost());

$factory->setProxy('127.0.0.1/8');
Assert::same('23.75.45.200', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('192.168.0.1', $factory->createHttpRequest()->getRemoteHost());
Assert::same('23.75.45.200', $factory->fromGlobals()->getRemoteAddress());
Assert::same('192.168.0.1', $factory->fromGlobals()->getRemoteHost());

$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();
Assert::same('http', $url->getScheme());
Assert::same('192.168.0.1', $url->getHost());
});
Expand All @@ -43,10 +43,10 @@ test(function () {
$factory = new RequestFactory;

$factory->setProxy('127.0.0.3');
Assert::same('23.75.45.200', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('192.168.0.1', $factory->createHttpRequest()->getRemoteHost());
Assert::same('23.75.45.200', $factory->fromGlobals()->getRemoteAddress());
Assert::same('192.168.0.1', $factory->fromGlobals()->getRemoteHost());

$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();
Assert::same(8080, $url->getPort());
Assert::same('192.168.0.1', $url->getHost());
});
Expand All @@ -62,10 +62,10 @@ test(function () {
$factory = new RequestFactory;

$factory->setProxy('127.0.0.3');
Assert::same('2001:db8:cafe::17', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('2001:db8:cafe::18', $factory->createHttpRequest()->getRemoteHost());
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteAddress());
Assert::same('2001:db8:cafe::18', $factory->fromGlobals()->getRemoteHost());

$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();
Assert::same('2001:db8:cafe::18', $url->getHost());
});

Expand All @@ -79,10 +79,10 @@ test(function () {
$factory = new RequestFactory;

$factory->setProxy('127.0.0.3');
Assert::same('2001:db8:cafe::17', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('2001:db8:cafe::18', $factory->createHttpRequest()->getRemoteHost());
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteAddress());
Assert::same('2001:db8:cafe::18', $factory->fromGlobals()->getRemoteHost());

$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();
Assert::same(47832, $url->getPort());
Assert::same('2001:db8:cafe::18', $url->getHost());
});
Expand All @@ -98,6 +98,6 @@ test(function () {
$factory = new RequestFactory;
$factory->setProxy('127.0.0.3');

$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();
Assert::same('https', $url->getScheme());
});
20 changes: 10 additions & 10 deletions tests/Http/RequestFactory.proxy.x-forwarded.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ test(function () {

$factory = new RequestFactory;
$factory->setProxy('127.0.0.1');
Assert::same('127.0.0.3', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('localhost', $factory->createHttpRequest()->getRemoteHost());
Assert::same('127.0.0.3', $factory->fromGlobals()->getRemoteAddress());
Assert::same('localhost', $factory->fromGlobals()->getRemoteHost());

$factory->setProxy('127.0.0.1/8');
Assert::same('23.75.45.200', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('otherhost', $factory->createHttpRequest()->getRemoteHost());
Assert::same('23.75.45.200', $factory->fromGlobals()->getRemoteAddress());
Assert::same('otherhost', $factory->fromGlobals()->getRemoteHost());

$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();
Assert::same('otherhost', $url->getHost());
});

Expand All @@ -44,13 +44,13 @@ test(function () {

$factory = new RequestFactory;
$factory->setProxy('10.0.0.0/24');
Assert::same('172.16.0.1', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('real', $factory->createHttpRequest()->getRemoteHost());
Assert::same('172.16.0.1', $factory->fromGlobals()->getRemoteAddress());
Assert::same('real', $factory->fromGlobals()->getRemoteHost());

$factory->setProxy(['10.0.0.1', '10.0.0.2']);
Assert::same('172.16.0.1', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('real', $factory->createHttpRequest()->getRemoteHost());
Assert::same('172.16.0.1', $factory->fromGlobals()->getRemoteAddress());
Assert::same('real', $factory->fromGlobals()->getRemoteHost());

$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();
Assert::same('real', $url->getHost());
});
4 changes: 2 additions & 2 deletions tests/Http/RequestFactory.query.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test(function () use ($factory) {
'REQUEST_URI' => '/',
];

Assert::same('http://nette.org/', (string) $factory->createHttpRequest()->getUrl());
Assert::same('http://nette.org/', (string) $factory->fromGlobals()->getUrl());
});


Expand All @@ -31,5 +31,5 @@ test(function () use ($factory) {
'REQUEST_URI' => '/?a=b',
];

Assert::same('http://nette.org/?a=b', (string) $factory->createHttpRequest()->getUrl());
Assert::same('http://nette.org/?a=b', (string) $factory->fromGlobals()->getUrl());
});
4 changes: 2 additions & 2 deletions tests/Http/RequestFactory.scheme.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RequestFactorySchemeTest extends Tester\TestCase
$_SERVER = $server;

$factory = new Nette\Http\RequestFactory;
$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();

Assert::same($expectedScheme, $url->getScheme());
Assert::same($expectedScheme === 'https' ? 443 : 80, $url->getPort());
Expand Down Expand Up @@ -60,7 +60,7 @@ class RequestFactorySchemeTest extends Tester\TestCase

$factory = new Nette\Http\RequestFactory;
$factory->setProxy(['10.0.0.1']);
$url = $factory->createHttpRequest()->getUrl();
$url = $factory->fromGlobals()->getUrl();

Assert::same($expectedScheme, $url->getScheme());
Assert::same($expectedPort, $url->getPort());
Expand Down
Loading

0 comments on commit 0011929

Please sign in to comment.