/** * Tests echoing data back to the client. */ public function testEcho() { $this->server->connectHandler(function ($socket) { $this->assertNotNull($socket); $socket->dataHandler(function ($buffer) use($socket) { $socket->write($buffer); }); }); $this->server->listen(8080, '0.0.0.0', function ($server, $error) { $this->assertNotNull($server); $this->assertNull($error); $this->client->connect(8080, 'localhost', function ($socket, $error) { $this->assertNotNull($socket); $this->assertNull($error); $sends = 10; $size = 100; $sent = new Buffer(); $received = new Buffer(); $socket->dataHandler(function ($buffer) use($sent, $received, $sends, $size) { $received->appendBuffer($buffer); if ($received->length == $sends * $size) { $this->assertEquals((string) $sent, (string) $received); $this->complete(); } }); $socket->pause(); $socket->resume(); $socket->writeQueueFull; $socket->writeQueueMaxSize = 100000; for ($i = 0; $i < $sends; $i++) { $buffer = $this->createBuffer($size); $sent->appendBuffer($buffer); $socket->write($buffer); } }); }); }
<?php use Vertx\Buffer; $client = Vertx::createNetClient(); $logger = Vertx::logger(); $client->connectTimeout = 30; $client->connect(1234, NULL, function ($socket, $error) use($logger) { if (!$error) { $buffer = new Buffer(); $socket->dataHandler(function ($buffer) use($logger) { $logger->info("Client receiving " . $buffer); $buffer->appendBuffer($buffer); }); for ($i = 0; $i < 10; $i++) { $message = "Hello {$i}."; $logger->info("Client sending " . $message); $socket->write($message); } } else { $logger->info("Failed to connect to the server. " . $error->getMessage()); } });
/** * Tests writing to and reading from a file synchronously. */ public function testWriteReadFileSync() { $filename = TEST_OUTPUT_DIRECTORY . '/test-file.txt'; $alphabet = 'abcdefghijklmnopqrstuvwxyz'; $this->fileSystem->createFileSync($filename, 'rwxr-xr-x'); $this->assertTrue($this->fileSystem->existsSync($filename)); $buffer = new Buffer(); $buffer->appendString($alphabet); $this->fileSystem->writeFileSync($filename, $buffer); $contents = $this->fileSystem->readFileSync($filename); $this->assertEquals($alphabet, (string) $contents); $this->fileSystem->deleteSync($filename); $this->complete(); }
/** * Tests the basic buffer append method. */ public function testAppendAny() { $buffer = new Buffer(substr(self::ALPHABET, 0, 10)); $buffer->append(substr(self::ALPHABET, 10)); $this->assertEquals(count($buffer), strlen(self::ALPHABET)); $this->assertEquals($buffer->toString(), self::ALPHABET); $this->assertEquals((string) $buffer, self::ALPHABET); $this->complete(); }
/** * Executes an HTTP request method. */ private function executeMethod($method, $chunked) { $sent_buff = $this->createBuffer(1000); $this->server->requestHandler(function ($request) use($method, $chunked) { $this->assertEquals((string) $request->version, 'HTTP_1_1', 'HTTP version is incorrect.'); $this->assertEquals($request->uri, self::$uri, 'URI is incorect.'); $this->assertEquals($request->method, $method, 'HTTP method is incorrect.'); $this->assertEquals($request->path, self::$path, 'Path is incorrect.'); $this->assertEquals($request->query, self::$query, 'Query is incorrect.'); $this->assertEquals($request->headers['header1'], 'vheader1', 'First header is incorrect.'); $this->assertEquals($request->headers['header2'], 'vheader2', 'Second header is incorrect.'); $this->assertEquals($request->params['param1'], 'vparam1', 'First parameter is incorrect.'); $this->assertEquals($request->params['param2'], 'vparam2', 'Second parameter is incorrect.'); $request->response->putHeader('rheader1', 'vrheader1'); $request->response->putHeader('rheader2', 'vrheader2'); $body = new Buffer(); $request->dataHandler(function ($buffer) use($body) { $body->appendBuffer($buffer); }); if ($method != 'HEAD' && $method != 'CONNECT') { $request->response->chunked = $chunked; } $request->endHandler(function () use($method, $chunked, $body, $request) { if ($method != 'HEAD' && $method != 'CONNECT') { if (empty($chunked)) { $request->response->putHeader('Content-Length', (string) count($body)); } $request->response->write($body); if (!empty($chunked)) { $request->response->putTrailer('trailer1', 'vtrailer1'); $request->response->putTrailer('trailer2', 'vtrailer2'); } } $request->response->end(); }); }); $this->server->listen(8080, '0.0.0.0', function ($server, $error) use($method, $chunked, $sent_buff) { $this->assertNotNull($server); $this->assertNull($error); $request = $this->client->request($method, self::$uri, function ($response) use($method, $chunked, $sent_buff) { $this->assertEquals($response->statusCode, 200); $this->assertEquals($response->headers['rheader1'], 'vrheader1', 'First header is incorrect.'); $this->assertEquals($response->headers['rheader2'], 'vrheader2', 'Second header is incorrect.'); $body = new Buffer(); $response->dataHandler(function ($buffer) use($body) { $body->appendBuffer($buffer); }); $response->endHandler(function () use($method, $chunked, $sent_buff, $body, $response) { if ($method != 'HEAD' && $method != 'CONNECT') { $this->assertEquals((string) $sent_buff, (string) $body, 'Sent and received buffers do not match.'); if (!empty($chunked)) { $this->assertEquals($response->trailers['trailer1'], 'vtrailer1', 'First trailer is incorrect.'); $this->assertEquals($response->trailers['trailer2'], 'vtrailer2', 'Second trailer is incorrect.'); } } $this->complete(); }); }); $request->chunked = $chunked; $request->putHeader('header1', 'vheader1'); $request->putHeader('header2', 'vheader2'); if (empty($chunked)) { $request->putHeader('Content-Length', (string) $sent_buff->length); } $request->headers['header3'] = 'vheader3_1'; $request->headers['header3'] = 'vheader3'; $request->write($sent_buff); $request->end(); }); }