Skip to content

Add trailing slash to base URL #5

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

Merged
merged 1 commit into from
Apr 7, 2015
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
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
},
Expand Down
24 changes: 23 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Clue\React\ViewVcApi\Client;
use Clue\React\Buzz\Message\Response;
use Clue\React\Buzz\Message\Body;

class ClientTest extends TestCase
{
Expand All @@ -10,7 +12,7 @@ class ClientTest extends TestCase

public function setUp()
{
$this->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);
}
Expand All @@ -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);
}
}
39 changes: 39 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use React\Promise\Deferred;

require __DIR__ . '/../vendor/autoload.php';

class TestCase extends PHPUnit_Framework_TestCase
Expand All @@ -15,6 +17,18 @@ protected function expectCallableOnce()
return $mock;
}

protected function expectCallableOnceWith($value)
{
$mock = $this->createCallableMock();

$mock
->expects($this->once())
->method('__invoke')
->with($this->equalTo($value));

return $mock;
}

protected function expectCallableNever()
{
$mock = $this->createCallableMock();
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down