Skip to content

Commit a879b63

Browse files
committed
Use Base URIs via Browser
1 parent 98d5c14 commit a879b63

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ listing from the given ViewVC URL:
1010
```php
1111
$loop = React\EventLoop\Factory::create();
1212
$browser = new Clue\React\Buzz\Browser($loop);
13-
$client = new Client('http://example.com/viewvc/', $browser);
13+
$client = new Client($browser->withBase('http://example.com/viewvc/'));
1414

1515
$client->fetchDirectory('/')->then(function ($files) {
1616
echo 'Files: ' . implode(', ', $files) . PHP_EOL;
@@ -34,9 +34,12 @@ in order to handle async requests:
3434
$loop = React\EventLoop\Factory::create();
3535
$browser = new Clue\React\Buzz\Browser($loop);
3636

37-
$client = new Client($url, $browser);
37+
$client = new Client($browser->withBase('http://example.com/viewvc/'));
3838
```
3939

40+
The `Client` API uses relative URIs to reference files and directories in your
41+
ViewVC installation, so make sure to apply the base URI as depicted above.
42+
4043
If you need custom DNS or proxy settings, you can explicitly pass a
4144
custom [`Browser`](https://github.com/clue/php-buzz-react#browser) instance.
4245

@@ -95,7 +98,7 @@ use Clue\React\Block;
9598
$loop = React\EventLoop\Factory::create();
9699
$browser = new Clue\React\Buzz\Browser($loop);
97100

98-
$client = new Client($url /* change me */, $browser);
101+
$client = new Client($browser->withBase($uri /* change me */));
99102
$promise = $client->fetchFile($path /* change me */);
100103

101104
try {

examples/directory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626

2727
$browser = new Browser($loop, $sender);
2828

29-
$client = new Client($url, $browser);
29+
$client = new Client($browser->withBase($url));
3030

3131
if (substr($path, -1) === '/') {
32-
$client->fetchDirectory($path, $revision)->then('var_dump', 'printf');
32+
$client->fetchDirectory($path, $revision)->then('print_r', 'printf');
3333
} else {
34-
$client->fetchFile($path, $revision)->then('printf', 'printf');
34+
$client->fetchFile($path, $revision)->then('print_r', 'printf');
3535
}
3636

3737
$loop->run();

src/Client.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414

1515
class Client
1616
{
17-
private $url;
1817
private $brower;
1918

20-
public function __construct($url, Browser $browser, Parser $parser = null, Loader $loader = null)
19+
public function __construct(Browser $browser, Parser $parser = null, Loader $loader = null)
2120
{
2221
if ($parser === null) {
2322
$parser = new Parser();
@@ -31,7 +30,6 @@ public function __construct($url, Browser $browser, Parser $parser = null, Loade
3130
// 'follow_redirects' => false
3231
// ));
3332

34-
$this->url = rtrim($url, '/') . '/';
3533
$this->browser = $browser;
3634
$this->parser = $parser;
3735
$this->loader = $loader;
@@ -135,7 +133,7 @@ private function fetchXml($url)
135133

136134
private function fetch($url)
137135
{
138-
return $this->browser->get($this->url . ltrim($url, '/'))->then(
136+
return $this->browser->get(ltrim($url, '/'))->then(
139137
function (Response $response) {
140138
return (string)$response->getBody();
141139
},

tests/ClientTest.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77

88
class ClientTest extends TestCase
99
{
10-
private $url;
1110
private $browser;
1211
private $client;
1312

1413
public function setUp()
1514
{
16-
$this->url = 'http://viewvc.example.org';
17-
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock();
18-
$this->client = new Client($this->url, $this->browser);
15+
// test case does not take base URI into account
16+
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->setMethods(array('get'))->getMock();
17+
$this->client = new Client($this->browser);
1918
}
2019

2120
public function testInvalidDirectory()
@@ -33,7 +32,7 @@ public function testInvalidFile()
3332
public function testFetchFile()
3433
{
3534
$response = new Response('HTTP/1.0', 200, 'OK', array(), new Body('# hello'));
36-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co'))->will($this->returnValue(Promise\resolve($response)));
35+
$this->browser->expects($this->once())->method('get')->with($this->equalTo('README.md?view=co'))->will($this->returnValue(Promise\resolve($response)));
3736

3837
$promise = $this->client->fetchFile('README.md');
3938

@@ -42,17 +41,16 @@ public function testFetchFile()
4241

4342
public function testFetchFileExcessiveSlashesAreIgnored()
4443
{
45-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co'))->will($this->returnValue(Promise\reject()));
44+
$this->browser->expects($this->once())->method('get')->with($this->equalTo('README.md?view=co'))->will($this->returnValue(Promise\reject()));
4645

47-
$client = new Client($this->url . '/', $this->browser);
48-
$promise = $client->fetchFile('/README.md');
46+
$promise = $this->client->fetchFile('/README.md');
4947

5048
$this->expectPromiseReject($promise);
5149
}
5250

5351
public function testFetchFileRevision()
5452
{
55-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=co&pathrev=1.0'))->will($this->returnValue(Promise\reject()));
53+
$this->browser->expects($this->once())->method('get')->with($this->equalTo('README.md?view=co&pathrev=1.0'))->will($this->returnValue(Promise\reject()));
5654

5755
$promise = $this->client->fetchFile('/README.md', '1.0');
5856

@@ -61,7 +59,7 @@ public function testFetchFileRevision()
6159

6260
public function testFetchDirectoryRevision()
6361
{
64-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?pathrev=1.0'))->will($this->returnValue(Promise\reject()));
62+
$this->browser->expects($this->once())->method('get')->with($this->equalTo('directory/?pathrev=1.0'))->will($this->returnValue(Promise\reject()));
6563

6664
$promise = $this->client->fetchDirectory('/directory/', '1.0');
6765

@@ -70,7 +68,7 @@ public function testFetchDirectoryRevision()
7068

7169
public function testFetchDirectoryAttic()
7270
{
73-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?hideattic=0'))->will($this->returnValue(Promise\reject()));
71+
$this->browser->expects($this->once())->method('get')->with($this->equalTo('directory/?hideattic=0'))->will($this->returnValue(Promise\reject()));
7472

7573
$promise = $this->client->fetchDirectory('/directory/', null, true);
7674

@@ -79,7 +77,7 @@ public function testFetchDirectoryAttic()
7977

8078
public function testFetchDirectoryRevisionAttic()
8179
{
82-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/directory/?pathrev=1.1&hideattic=0'))->will($this->returnValue(Promise\reject()));
80+
$this->browser->expects($this->once())->method('get')->with($this->equalTo('directory/?pathrev=1.1&hideattic=0'))->will($this->returnValue(Promise\reject()));
8381

8482
$promise = $this->client->fetchDirectory('/directory/', '1.1', true);
8583

@@ -88,7 +86,7 @@ public function testFetchDirectoryRevisionAttic()
8886

8987
public function testFetchLogRevision()
9088
{
91-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=log&pathrev=1.0'))->will($this->returnValue(Promise\reject()));
89+
$this->browser->expects($this->once())->method('get')->with($this->equalTo('README.md?view=log&pathrev=1.0'))->will($this->returnValue(Promise\reject()));
9290

9391
$promise = $this->client->fetchLog('/README.md', '1.0');
9492

@@ -97,7 +95,7 @@ public function testFetchLogRevision()
9795

9896
public function testFetchPatch()
9997
{
100-
$this->browser->expects($this->once())->method('get')->with($this->equalTo('http://viewvc.example.org/README.md?view=patch&r1=1.0&r2=1.1'))->will($this->returnValue(Promise\reject()));
98+
$this->browser->expects($this->once())->method('get')->with($this->equalTo('README.md?view=patch&r1=1.0&r2=1.1'))->will($this->returnValue(Promise\reject()));
10199

102100
$promise = $this->client->fetchPatch('/README.md', '1.0', '1.1');
103101

tests/FunctionalApacheClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function setUp()
1717
$this->loop = LoopFactory::create();
1818
$browser = new Browser($this->loop);
1919

20-
$this->viewvc = new Client($url, $browser);
20+
$this->viewvc = new Client($browser->withBase($url));
2121
}
2222

2323
public function testFetchDirectory()

tests/FunctionalGentooClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function setUp()
3636
$this->markTestSkipped('Unable to reach Gentoo ViewVC');
3737
}
3838

39-
$this->viewvc = new Client($url, $browser);
39+
$this->viewvc = new Client($browser->withBase($url));
4040
}
4141

4242
public function testFetchDirectoryAttic()

0 commit comments

Comments
 (0)