From 23894a0c98d3390b455f0be91a4ec14456cb9363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 7 Apr 2015 17:06:18 +0200 Subject: [PATCH] Add trailing slash to base URL --- src/Client.php | 4 ++-- tests/ClientTest.php | 24 +++++++++++++++++++++++- tests/bootstrap.php | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index e77e7c7..8be5ac4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -31,7 +31,7 @@ public function __construct($url, Browser $browser, Parser $parser = null, Loade // 'follow_redirects' => false // )); - $this->url = $url; + $this->url = rtrim($url, '/') . '/'; $this->browser = $browser; $this->parser = $parser; $this->loader = $loader; @@ -116,7 +116,7 @@ private function fetchXml($url) private function fetch($url) { - return $this->browser->get($this->url . $url)->then( + return $this->browser->get($this->url . ltrim($url, '/'))->then( function (Response $response) { return (string)$response->getBody(); }, diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 31a339d..0434d0c 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -1,6 +1,8 @@ url = 'http://viewvc.example.org/'; + $this->url = 'http://viewvc.example.org'; $this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock(); $this->client = new Client($this->url, $this->browser); } @@ -26,4 +28,24 @@ public function testInvalidFile() $promise = $this->client->fetchFile('invalid/'); $this->expectPromiseReject($promise); } + + public function testFetchFile() + { + $response = new Response('HTTP/1.0', 200, 'OK', null, new Body('# hello')); + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co'))->will($this->returnValue($this->createPromiseResolved($response))); + + $promise = $this->client->fetchFile('README.md'); + + $this->expectPromiseResolveWith('# hello', $promise); + } + + public function testFetchFileExcessiveSlashesAreIgnored() + { + $this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co'))->will($this->returnValue($this->createPromiseRejected())); + + $client = new Client($this->url . '/', $this->browser); + $promise = $client->fetchFile('/README.md'); + + $this->expectPromiseReject($promise); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 74873e5..237f0b4 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,7 @@ createCallableMock(); + + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->equalTo($value)); + + return $mock; + } + protected function expectCallableNever() { $mock = $this->createCallableMock(); @@ -42,6 +56,15 @@ protected function expectPromiseResolve($promise) return $promise; } + protected function expectPromiseResolveWith($value, $promise) + { + $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + + $promise->then($this->expectCallableOnceWith($value), $this->expectCallableNever()); + + return $promise; + } + protected function expectPromiseReject($promise) { $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); @@ -50,6 +73,22 @@ protected function expectPromiseReject($promise) return $promise; } + + protected function createPromiseResolved($value = null) + { + $deferred = new Deferred(); + $deferred->resolve($value); + + return $deferred->promise(); + } + + protected function createPromiseRejected($value = null) + { + $deferred = new Deferred(); + $deferred->reject($value); + + return $deferred->promise(); + } } class CallableStub