public function testGetFromSocketBody() { $body = <<<HTTP GET /favicon.ico HTTP/1.1 Host: localhost Connection: keep-alive Pragma: no-cache Cache-Control: no-cache User-Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 Referer: http://localhost/ Cookie: FOO=135050505050; BAR=1; PHPSESSID=djiar0p36a1nhrc3j6pd6r0gp3 HTTP; $body = str_replace("\n", "\r\n", $body); $request = $this->factory->createFromSocketBody($body); static::assertEquals('1.1', $request->getProtocolVersion()); static::assertEquals('GET', $request->getMethod()); static::assertEquals('http://localhost/favicon.ico', $request->getUri()->__toString()); static::assertEquals('localhost', $request->getHeaderLine('host')); static::assertEquals('keep-alive', $request->getHeaderLine('connection')); static::assertEquals('no-cache', $request->getHeaderLine('cache-control')); static::assertEquals('Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', $request->getHeaderLine('user-agent')); static::assertEquals('http://localhost/', $request->getHeaderLine('referer')); static::assertEquals('http://localhost/', $request->getHeaderLine('referer')); static::assertEquals('FOO=135050505050; BAR=1; PHPSESSID=djiar0p36a1nhrc3j6pd6r0gp3', $request->getHeaderLine('cookie')); }
/** * @dataProvider provideMethodAndBody */ public function testRequestPost($method, $contentType) { $servers = ['HTTP_CONTENT_TYPE' => $contentType['type'], 'REQUEST_METHOD' => $method]; $body = Mockery::mock(StreamInterface::class); $body->shouldReceive('__toString')->andReturn($contentType['input']); $factory = new ServerRequestFactory(new UploadedFileFactory()); // if method === post => php can parsed body! $posts = $method === 'POST' && $contentType['type'] !== 'application/json' ? $contentType['expected'] : []; $request = $factory->create($servers, [], $posts, [], [], $body); $this->assertEquals($contentType['expected'], $request->getParsedBody()); }
/** * @param string $host * @param int $port * @param callable $handler * @throws \Exception */ public function listen($host, $port, callable $handler) { // create a socket $socket = socket_create(AF_INET, SOCK_STREAM, 0); // bind the socket if (!socket_bind($socket, $host, (int) $port)) { throw new \Exception("Could not bind: {$host}:{$port} - " . socket_strerror(socket_last_error())); } while (1) { // listen for connections socket_listen($socket); // try to get the client socket resource // if false we got an error close the connection and continue if (!($client = socket_accept($socket))) { socket_close($client); continue; } // create new request instance with the clients header. // In the real world of course you cannot just fix the max size to 1024.. $contents = socket_read($client, 1024); echo $contents; // execute the callback $response = call_user_func($handler, $this->requestFactory->fromSocketBody($contents)); // check if we really recived an Response object // if not return a 404 response object if (!$response || !$response instanceof ResponseInterface) { $response = new NotFoundException(); } // make a string out of our response $responseBody = $this->responseSender->parseToSocketBody($response); // write the response to the client socket socket_write($client, $responseBody, strlen($responseBody)); // close the connetion so we can accept new ones socket_close($client); } }
public function testGetUriMinimum() { $server = array('SERVER_NAME' => '0.0.0.0', 'SERVER_PORT' => '8002', 'REQUEST_URI' => '/abk?sdnkf'); $this->assertEquals('http://0.0.0.0:8002/abk?sdnkf', ServerRequestFactory::getUri($server)->__toString()); }
<?php namespace Wandu\Publ; use Wandu\Http\Factory\ServerRequestFactory; use Wandu\Http\Sender\ResponseSender; $basePath = dirname(__DIR__); require $basePath . '/vendor/autoload.php'; $app = new Application($basePath); $response = $app->execute(ServerRequestFactory::fromGlobals()); ResponseSender::send($response);