From 2832e8e50c4846cf2c8b16249d2cf736a3205385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 4 Mar 2015 20:20:50 +0100 Subject: [PATCH 1/4] Expose Transaction options via Browser --- src/Browser.php | 15 ++++++++++++++- src/Io/Transaction.php | 8 +++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Browser.php b/src/Browser.php index adb78f2..4913708 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -13,6 +13,7 @@ class Browser { private $sender; private $loop; + private $options = array(); public function __construct(LoopInterface $loop, Sender $sender = null) { @@ -75,8 +76,20 @@ public function request($method, $url, $headers = array(), $content = null) public function send(Request $request) { - $transaction = new Transaction($request, $this->sender); + $transaction = new Transaction($request, $this->sender, $this->options); return $transaction->send(); } + + public function withOptions(array $options) + { + $browser = clone $this; + + // merge all options, but remove those explicitly assigned a null value + $browser->options = array_filter($options + $this->options, function ($value) { + return ($value !== null); + }); + + return $browser; + } } diff --git a/src/Io/Transaction.php b/src/Io/Transaction.php index 9f0d744..4072e48 100644 --- a/src/Io/Transaction.php +++ b/src/Io/Transaction.php @@ -25,8 +25,14 @@ class Transaction // context: http.ignore_errors private $obeySuccessCode = true; - public function __construct(Request $request, Sender $sender) + public function __construct(Request $request, Sender $sender, array $options = array()) { + foreach ($options as $name => $value) { + if (property_exists($this, $name)) { + $this->$name = $value; + } + } + $this->request = $request; $this->sender = $sender; } From a0a759dbeeb9ed7cdeb07426042041f1e08b45ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 4 Mar 2015 20:21:02 +0100 Subject: [PATCH 2/4] Add functional tests for Browser --- tests/FunctionalBrowserTest.php | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/FunctionalBrowserTest.php diff --git a/tests/FunctionalBrowserTest.php b/tests/FunctionalBrowserTest.php new file mode 100644 index 0000000..4ffc503 --- /dev/null +++ b/tests/FunctionalBrowserTest.php @@ -0,0 +1,55 @@ +loop = Factory::create(); + $this->browser = new Browser($this->loop); + } + + public function testSimpleRequest() + { + $this->expectPromiseResolve($this->browser->get('http://www.google.com')); + + $this->loop->run(); + } + + public function testNotFollowingRedirectsResolvesWithRedirectResult() + { + $browser = $this->browser->withOptions(array('followRedirects' => false)); + + $this->expectPromiseResolve($browser->get('http://www.google.com')); + + $this->loop->run(); + } + + public function testRejectingRedirectsRejects() + { + $browser = $this->browser->withOptions(array('maxRedirects' => 0)); + + $this->expectPromiseReject($browser->get('http://www.google.com')); + + $this->loop->run(); + } + + public function testInvalidPort() + { + $this->expectPromiseReject($this->browser->get('http://www.google.com:443')); + + $this->loop->run(); + } + + public function testInvalidPath() + { + $this->expectPromiseReject($this->browser->get('http://www.google.com/does-not-exist')); + + $this->loop->run(); + } +} From 1b400eaaa5c18089af919beb3ecb821127b8fe06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 6 Mar 2015 19:43:11 +0100 Subject: [PATCH 3/4] Use httpbin to perform functional tests --- tests/FunctionalBrowserTest.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/FunctionalBrowserTest.php b/tests/FunctionalBrowserTest.php index 4ffc503..871cd1e 100644 --- a/tests/FunctionalBrowserTest.php +++ b/tests/FunctionalBrowserTest.php @@ -8,6 +8,9 @@ class FunctionalBrowserTest extends TestCase private $loop; private $browser; + /** base url to the httpbin service **/ + private $base = 'http://httpbin.org/'; + public function setUp() { $this->loop = Factory::create(); @@ -16,7 +19,14 @@ public function setUp() public function testSimpleRequest() { - $this->expectPromiseResolve($this->browser->get('http://www.google.com')); + $this->expectPromiseResolve($this->browser->get($this->base . 'get')); + + $this->loop->run(); + } + + public function testRedirectRequest() + { + $this->expectPromiseResolve($this->browser->get($this->base . 'redirect-to?url=' . urlencode($this->base . 'get'))); $this->loop->run(); } @@ -25,7 +35,7 @@ public function testNotFollowingRedirectsResolvesWithRedirectResult() { $browser = $this->browser->withOptions(array('followRedirects' => false)); - $this->expectPromiseResolve($browser->get('http://www.google.com')); + $this->expectPromiseResolve($browser->get($this->base . 'redirect/3')); $this->loop->run(); } @@ -34,7 +44,7 @@ public function testRejectingRedirectsRejects() { $browser = $this->browser->withOptions(array('maxRedirects' => 0)); - $this->expectPromiseReject($browser->get('http://www.google.com')); + $this->expectPromiseReject($browser->get($this->base . 'redirect/3')); $this->loop->run(); } @@ -48,7 +58,7 @@ public function testInvalidPort() public function testInvalidPath() { - $this->expectPromiseReject($this->browser->get('http://www.google.com/does-not-exist')); + $this->expectPromiseReject($this->browser->get($this->base . 'status/404')); $this->loop->run(); } From 7f16fa6432f0c85a107dd4749e2fd1ecc6a0ca07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 13 Jun 2015 20:50:20 +0200 Subject: [PATCH 4/4] Documentation for options API --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 9fc2a78..e25c1ff 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,26 @@ $client = new Browser($loop, $sender); $client->get('http://localhost/demo'); ``` +### Options + +Note: This API is subject to change. + +The [`Browser`](#browser) class exposes several options for the handling of +HTTP transactions. These options resemble some of PHP's +[HTTP context options](http://php.net/manual/en/context.http.php) and +can be controlled via the following API (and their defaults): + +```php +$newBrowser = $browser->withOptions(array( + 'followRedirects' => true, + 'maxRedirects' => 10, + 'obeySuccessCode' => true +)); +``` + +Notice that the [`Browser`](#browser) is an immutable object, i.e. the `withOptions()` method +actually returns a *new* [`Browser`](#browser) instance with the options applied. + ### Streaming Note: This API is subject to change.