|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Tests\Stream; |
| 4 | + |
| 5 | +use React\Stream\DuplexResourceStream; |
| 6 | +use React\EventLoop\Factory; |
| 7 | +use React\Stream\WritableResourceStream; |
| 8 | + |
| 9 | +/** |
| 10 | + * @group internet |
| 11 | + */ |
| 12 | +class FunctionalInternetTest extends TestCase |
| 13 | +{ |
| 14 | + public function testUploadKilobytePlain() |
| 15 | + { |
| 16 | + $size = 1000; |
| 17 | + $stream = stream_socket_client('tcp://httpbin.org:80'); |
| 18 | + |
| 19 | + $loop = Factory::create(); |
| 20 | + $stream = new DuplexResourceStream($stream, $loop); |
| 21 | + |
| 22 | + $buffer = ''; |
| 23 | + $stream->on('data', function ($chunk) use (&$buffer) { |
| 24 | + $buffer .= $chunk; |
| 25 | + }); |
| 26 | + |
| 27 | + $stream->on('error', $this->expectCallableNever()); |
| 28 | + |
| 29 | + $stream->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . str_repeat('.', $size)); |
| 30 | + |
| 31 | + $loop->run(); |
| 32 | + |
| 33 | + $this->assertNotEquals('', $buffer); |
| 34 | + } |
| 35 | + |
| 36 | + public function testUploadBiggerBlockPlain() |
| 37 | + { |
| 38 | + $size = 1000 * 30; |
| 39 | + $stream = stream_socket_client('tcp://httpbin.org:80'); |
| 40 | + |
| 41 | + $loop = Factory::create(); |
| 42 | + $stream = new DuplexResourceStream($stream, $loop); |
| 43 | + |
| 44 | + $buffer = ''; |
| 45 | + $stream->on('data', function ($chunk) use (&$buffer) { |
| 46 | + $buffer .= $chunk; |
| 47 | + }); |
| 48 | + |
| 49 | + $stream->on('error', $this->expectCallableNever()); |
| 50 | + |
| 51 | + $stream->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . str_repeat('.', $size)); |
| 52 | + |
| 53 | + $loop->run(); |
| 54 | + |
| 55 | + $this->assertNotEquals('', $buffer); |
| 56 | + } |
| 57 | + |
| 58 | + public function testUploadKilobyteSecure() |
| 59 | + { |
| 60 | + $size = 1000; |
| 61 | + $stream = stream_socket_client('tls://httpbin.org:443'); |
| 62 | + |
| 63 | + $loop = Factory::create(); |
| 64 | + $stream = new DuplexResourceStream($stream, $loop); |
| 65 | + |
| 66 | + $buffer = ''; |
| 67 | + $stream->on('data', function ($chunk) use (&$buffer) { |
| 68 | + $buffer .= $chunk; |
| 69 | + }); |
| 70 | + |
| 71 | + $stream->on('error', $this->expectCallableNever()); |
| 72 | + |
| 73 | + $stream->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . str_repeat('.', $size)); |
| 74 | + |
| 75 | + $loop->run(); |
| 76 | + |
| 77 | + $this->assertNotEquals('', $buffer); |
| 78 | + } |
| 79 | + |
| 80 | + public function testUploadBiggerBlockSecureRequiresSmallerChunkSize() |
| 81 | + { |
| 82 | + $size = 1000 * 30000; |
| 83 | + $stream = stream_socket_client('tls://httpbin.org:443'); |
| 84 | + |
| 85 | + $loop = Factory::create(); |
| 86 | + $stream = new DuplexResourceStream( |
| 87 | + $stream, |
| 88 | + $loop, |
| 89 | + null, |
| 90 | + new WritableResourceStream($stream, $loop, null, 8192) |
| 91 | + ); |
| 92 | + |
| 93 | + $buffer = ''; |
| 94 | + $stream->on('data', function ($chunk) use (&$buffer) { |
| 95 | + $buffer .= $chunk; |
| 96 | + }); |
| 97 | + |
| 98 | + $stream->on('error', $this->expectCallableNever()); |
| 99 | + |
| 100 | + $stream->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . str_repeat('.', $size)); |
| 101 | + |
| 102 | + $loop->run(); |
| 103 | + |
| 104 | + $this->assertNotEquals('', $buffer); |
| 105 | + } |
| 106 | +} |
0 commit comments