-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ResourceOutputStreamTest.php
105 lines (78 loc) · 2.89 KB
/
ResourceOutputStreamTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Amp\ByteStream\Test;
use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\ByteStream\StreamException;
use Amp\Delayed;
use Amp\PHPUnit\AsyncTestCase;
class ResourceOutputStreamTest extends AsyncTestCase
{
public function testGetResource()
{
$stream = new ResourceOutputStream(\STDOUT);
$this->assertSame(\STDOUT, $stream->getResource());
}
public function testNonStream()
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("Expected a valid stream");
new ResourceOutputStream(42);
}
public function testNotWritable()
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("Expected a writable stream");
new ResourceOutputStream(\STDIN);
}
public function testBrokenPipe()
{
if (($sockets = @\stream_socket_pair(
\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX,
STREAM_SOCK_STREAM,
STREAM_IPPROTO_IP
)) === false) {
$this->fail("Failed to create socket pair.");
}
list($a, $b) = $sockets;
$stream = new ResourceOutputStream($a);
\fclose($b);
$this->expectException(StreamException::class);
$this->expectExceptionMessage("fwrite(): send of 6 bytes failed with errno=32 Broken pipe");
yield $stream->write("foobar");
}
public function testClosedRemoteSocket()
{
$server = \stream_socket_server("tcp://127.0.0.1:0");
$address = \stream_socket_get_name($server, false);
$a = \stream_socket_client("tcp://" . $address);
$b = \stream_socket_accept($server);
$stream = new ResourceOutputStream($a);
\fclose($b);
$this->expectException(StreamException::class);
$this->expectExceptionMessage("fwrite(): send of 6 bytes failed with errno=32 Broken pipe");
// The first write still succeeds somehow...
yield $stream->write("foobar");
// A delay seems required for the OS to realize the socket is indeed closed.
yield new Delayed(10);
yield $stream->write("foobar");
}
/**
* @requires PHPUnit >= 7
*
* @see https://github.com/reactphp/stream/pull/150
*/
public function testUploadBiggerBlockSecure()
{
$size = 2 ** 18; // 256kb
$resource = \stream_socket_client('tls://httpbin.org:443');
$output = new ResourceOutputStream($resource);
$body = \str_repeat('.', $size);
yield $output->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . $body);
$input = new ResourceInputStream($resource);
$buffer = '';
while (null !== ($chunk = yield $input->read())) {
$buffer .= $chunk;
}
$this->assertStringContainsString($body, $buffer);
}
}