Skip to content

Commit aaa35f8

Browse files
authored
Merge pull request #67 from clue-labs/tests
Fix online tests and add option to exclude tests against httpbin.org
2 parents 139e8e7 + 152eda0 commit aaa35f8

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ mess with most of the low-level details.
5454
* [UNIX domain sockets](#unix-domain-sockets)
5555
* [Options](#options)
5656
* [Install](#install)
57+
* [Tests](#tests)
5758
* [License](#license)
5859

5960
## Quickstart example
@@ -555,6 +556,30 @@ $ composer require clue/buzz-react:^1.1
555556

556557
See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
557558

559+
## Tests
560+
561+
To run the test suite, you first need to clone this repo and then install all
562+
dependencies [through Composer](https://getcomposer.org):
563+
564+
```bash
565+
$ composer install
566+
```
567+
568+
To run the test suite, go to the project root and run:
569+
570+
```bash
571+
$ php vendor/bin/phpunit
572+
```
573+
574+
The test suite also contains a number of functional integration tests that send
575+
test HTTP requests against the online service http://httpbin.org and thus rely
576+
on a stable internet connection.
577+
If you do not want to run these, they can simply be skipped like this:
578+
579+
```bash
580+
$ php vendor/bin/phpunit --exclude-group online
581+
```
582+
558583
## License
559584

560585
MIT

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
},
2727
"require-dev": {
2828
"clue/block-react": "^1.0",
29-
"phpunit/phpunit": "^4.5"
29+
"phpunit/phpunit": "^4.5",
30+
"react/http": "^0.7.2",
31+
"react/promise-stream": "^0.1.1",
32+
"react/socket": "^0.8"
3033
}
3134
}

tests/FunctionalBrowserTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Clue\React\Buzz\Message\ResponseException;
1111
use Clue\React\Block;
1212
use React\Stream\ReadableStream;
13+
use Psr\Http\Message\ServerRequestInterface;
14+
use React\Http\Response;
15+
use React\Promise\Stream;
1316

1417
class FunctionalBrowserTest extends TestCase
1518
{
@@ -25,28 +28,33 @@ public function setUp()
2528
$this->browser = new Browser($this->loop);
2629
}
2730

31+
/** @group online */
2832
public function testSimpleRequest()
2933
{
3034
Block\await($this->browser->get($this->base . 'get'), $this->loop);
3135
}
3236

37+
/** @group online */
3338
public function testRedirectRequestRelative()
3439
{
3540
Block\await($this->browser->get($this->base . 'redirect-to?url=get'), $this->loop);
3641
}
3742

43+
/** @group online */
3844
public function testRedirectRequestAbsolute()
3945
{
4046
Block\await($this->browser->get($this->base . 'redirect-to?url=' . urlencode($this->base . 'get')), $this->loop);
4147
}
4248

49+
/** @group online */
4350
public function testNotFollowingRedirectsResolvesWithRedirectResult()
4451
{
4552
$browser = $this->browser->withOptions(array('followRedirects' => false));
4653

4754
Block\await($browser->get($this->base . 'redirect/3'), $this->loop);
4855
}
4956

57+
/** @group online */
5058
public function testRejectingRedirectsRejects()
5159
{
5260
$browser = $this->browser->withOptions(array('maxRedirects' => 0));
@@ -55,6 +63,7 @@ public function testRejectingRedirectsRejects()
5563
Block\await($browser->get($this->base . 'redirect/3'), $this->loop);
5664
}
5765

66+
/** @group online */
5867
public function testCanAccessHttps()
5968
{
6069
if (!function_exists('stream_socket_enable_crypto')) {
@@ -64,6 +73,7 @@ public function testCanAccessHttps()
6473
Block\await($this->browser->get('https://www.google.com/'), $this->loop);
6574
}
6675

76+
/** @group online */
6777
public function testVerifyPeerEnabledForBadSslRejects()
6878
{
6979
if (!function_exists('stream_socket_enable_crypto')) {
@@ -89,6 +99,7 @@ public function testVerifyPeerEnabledForBadSslRejects()
8999
Block\await($browser->get('https://self-signed.badssl.com/'), $this->loop);
90100
}
91101

102+
/** @group online */
92103
public function testVerifyPeerDisabledForBadSslResolves()
93104
{
94105
if (!function_exists('stream_socket_enable_crypto')) {
@@ -113,12 +124,14 @@ public function testVerifyPeerDisabledForBadSslResolves()
113124
Block\await($browser->get('https://self-signed.badssl.com/'), $this->loop);
114125
}
115126

127+
/** @group online */
116128
public function testInvalidPort()
117129
{
118130
$this->setExpectedException('RuntimeException');
119131
Block\await($this->browser->get('http://www.google.com:443/'), $this->loop);
120132
}
121133

134+
/** @group online */
122135
public function testErrorStatusCodeRejectsWithResponseException()
123136
{
124137
try {
@@ -132,6 +145,7 @@ public function testErrorStatusCodeRejectsWithResponseException()
132145
}
133146
}
134147

148+
/** @group online */
135149
public function testPostString()
136150
{
137151
$response = Block\await($this->browser->post($this->base . 'post', array(), 'hello world'), $this->loop);
@@ -142,6 +156,28 @@ public function testPostString()
142156

143157
public function testPostStreamChunked()
144158
{
159+
// httpbin used to support `Transfer-Encoding: chunked` for requests,
160+
// but not rejects those, so let's start our own server instance
161+
$that = $this;
162+
$server = new \React\Http\Server(function (ServerRequestInterface $request) use ($that) {
163+
$that->assertFalse($request->hasHeader('Content-Length'));
164+
$that->assertNull($request->getBody()->getSize());
165+
166+
return Stream\buffer($request->getBody())->then(function ($body) {
167+
return new Response(
168+
200,
169+
array(),
170+
json_encode(array(
171+
'data' => $body
172+
))
173+
);
174+
});
175+
});
176+
$socket = new \React\Socket\Server(0, $this->loop);
177+
$server->listen($socket);
178+
179+
$this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/';
180+
145181
$stream = new ReadableStream();
146182

147183
$this->loop->addTimer(0.001, function () use ($stream) {
@@ -153,8 +189,11 @@ public function testPostStreamChunked()
153189
$data = json_decode((string)$response->getBody(), true);
154190

155191
$this->assertEquals('hello world', $data['data']);
192+
193+
$socket->close();
156194
}
157195

196+
/** @group online */
158197
public function testPostStreamKnownLength()
159198
{
160199
$stream = new ReadableStream();
@@ -170,6 +209,7 @@ public function testPostStreamKnownLength()
170209
$this->assertEquals('hello world', $data['data']);
171210
}
172211

212+
/** @group online */
173213
public function testPostStreamClosed()
174214
{
175215
$stream = new ReadableStream();

0 commit comments

Comments
 (0)