private function request($method, $path, $data = array(), $optionalHeaders = array()) { //Make method uppercase $method = strtoupper($method); $options = array('REQUEST_METHOD' => $method, 'REQUEST_URI' => $path); if ($method === 'GET') { $options['QUERY_STRING'] = http_build_query($data); } else { $params = json_encode($data); } // Prepare a mock environment $env = Environment::mock(array_merge($options, $optionalHeaders)); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = $this->cookies; $serverParams = $env->all(); $body = new RequestBody(); // Attach JSON request if (isset($params)) { $headers->set('Content-Type', 'application/json;charset=utf8'); $body->write($params); } $this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); $response = new Response(); // Invoke request $app = $this->app; $this->response = $app($this->request, $response); // Return the application output. return (string) $this->response->getBody(); }
protected function dispatch($path, $method = 'GET', $data = array(), $cookies = array()) { $container = $this->app->getContainer(); // seperate the path from the query string so we can set in the environment @(list($path, $queryString) = explode('?', $path)); // Prepare a mock environment $env = Environment::mock(array('REQUEST_URI' => $path, 'REQUEST_METHOD' => $method, 'QUERY_STRING' => is_null($queryString) ? '' : $queryString)); // Prepare request and response objects $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = $cookies; $serverParams = $env->all(); $body = new RequestBody(); // create request, and set params $req = new $container['request']($method, $uri, $headers, $cookies, $serverParams, $body); if (!empty($data)) { $req = $req->withParsedBody($data); } $res = new $container['response'](); // // Fix for body, but breaks POST params in tests - http://stackoverflow.com/questions/34823328/response-getbody-is-empty-when-testing-slim-3-routes-with-phpunit // $body = new RequestBody(); // if (!empty($data)) // $body->write(json_encode($data)); // // // create request, and set params // $req = new $container['request']($method, $uri, $headers, $cookies, $serverParams, $body); // $res = new $container['response'](); $this->headers = $headers; $this->request = $req; $this->response = call_user_func_array($this->app, array($req, $res)); }
protected function createRequest($env) { $method = $env["REQUEST_METHOD"]; $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = Cookies::parseHeader($headers->get("Cookie", [])); $serverParams = $env->all(); $body = new Body(fopen("php://input", "r")); return new Request($method, $uri, $headers, $cookies, $serverParams, $body); }
protected function createRequest() { $env = (new Environment())->mock(["PATH_INFO" => "/index/hello/", "SCRIPT_NAME" => "/index.php"]); $method = $env["REQUEST_METHOD"]; $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = Cookies::parseHeader($headers->get("Cookie", [])); $serverParams = $env->all(); $body = new Body(fopen("php://input", "r")); return new Request($method, $uri, $headers, $cookies, $serverParams, $body); }
public function setRequest($method = 'GET', $uri = '/', $other = []) { // Prepare request and response objects $base = ['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => $uri, 'REQUEST_METHOD' => $method]; $env = \Slim\Http\Environment::mock(array_merge($base, $other)); $uri = \Slim\Http\Uri::createFromEnvironment($env); $headers = \Slim\Http\Headers::createFromEnvironment($env); $cookies = (array) new \Slim\Collection(); $serverParams = (array) new \Slim\Collection($env->all()); $body = new \Slim\Http\Body(fopen('php://temp', 'r+')); return new \Slim\Http\Request('GET', $uri, $headers, $cookies, $serverParams, $body); }
public function requestFactory($uri = '/') { // Prepare request and response objects $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/', 'REQUEST_METHOD' => 'GET']); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); return $req; }
public function request($method, $path, $options = array()) { $env = Environment::mock(array_merge(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => $path, 'REQUEST_METHOD' => $method], $options)); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $req = new Request($method, $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); $app = (require ROOT_DIR . '/bootstrap/bootstrap.php'); $app($req, $res); return $app($req, $res); }
public function setupRequest($url, $method, $request, $files) { $env = Environment::mock(['REQUEST_URI' => $url, 'REQUEST_METHOD' => $method, 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo']); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $files = UploadedFile::createFromEnvironment(Environment::mock()); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, $files); $this->response = new Response(); $app = $this->app; return $app($this->request, $this->response); }
private function prepareRequest($method, $url, array $requestParameters) { $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => $url, 'REQUEST_METHOD' => $method]); $parts = explode('?', $url); if (isset($parts[1])) { $env['QUERY_STRING'] = $parts[1]; } $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $body->write(json_encode($requestParameters)); $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); return $request->withHeader('Content-Type', 'application/json'); }
public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null) { $environment = new Environment($server); $method = $environment['REQUEST_METHOD']; $uri = Uri::createFromEnvironment($environment); $headers = Headers::createFromEnvironment($environment); $cookies = Cookies::parseHeader($headers->get('Cookie', [])); $serverParams = $environment->all(); $body = new RequestBody(); $uploadedFiles = UploadedFile::createFromEnvironment($environment); $request = new ServerRequest($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) { // parsed body must be $_POST $request = $request->withParsedBody($_POST); } return $request; }
public function testException() { $app = new App(); $app->add(new WhoopsMiddleware()); $app->get('/foo', function ($req, $res) use($app) { return $app->router->pathFor('index'); }); $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET']); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); $this->setExpectedException('\\RuntimeException'); $app($req, $res); }
protected function dispatch($path, $method = 'GET', $data = array()) { // Prepare a mock environment $env = Environment::mock(array('REQUEST_URI' => $path, 'REQUEST_METHOD' => $method)); // Prepare request and response objects $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $container = $this->app->getContainer(); // create request, and set params $req = new $container['request']($method, $uri, $headers, $cookies, $serverParams, $body); if (!empty($data)) { $req = $req->withParsedBody($data); } $res = new $container['response'](); $this->headers = $headers; $this->request = $req; $this->response = call_user_func_array($this->app, array($req, $res)); }
/** * Perform a sub-request from within an application route * * This method allows you to prepare and initiate a sub-request, run within * the context of the current request. This WILL NOT issue a remote HTTP * request. Instead, it will route the provided URL, method, headers, * cookies, body, and server variables against the set of registered * application routes. The result response object is returned. * * @param string $method The request method (e.g., GET, POST, PUT, etc.) * @param string $path The request URI path * @param string $query The request URI query string * @param array $headers The request headers (key-value array) * @param array $cookies The request cookies (key-value array) * @param string $bodyContent The request body * @param ResponseInterface $response The response object (optional) * @return ResponseInterface */ public function subRequest($method, $path, $query = '', array $headers = [], array $cookies = [], $bodyContent = '', ResponseInterface $response = null) { $env = $this->container->get('environment'); $uri = Uri::createFromEnvironment($env)->withPath($path)->withQuery($query); $headers = new Headers($headers); $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $body->write($bodyContent); $body->rewind(); $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); if (!$response) { $response = $this->container->get('response'); } return $this($request, $response); }
/** * When the URL is /foo/index.php/bar/baz, we need the baseURL to be * /foo/index.php so that routing works correctly. * * @ticket 1639 as a fix to 1590 broke this. */ public function testRequestURIContainsIndexDotPhp() { $uri = Uri::createFromEnvironment(Environment::mock(['SCRIPT_NAME' => '/foo/index.php', 'REQUEST_URI' => '/foo/index.php/bar/baz'])); $this->assertSame('/foo/index.php', $uri->getBasePath()); }
/** * Integration test IpRestrictMiddleware::_invoke() when the given IP is allowed. */ public function testIpAllow() { // Prepare the Request and the application. $app = new App(); // Setup a demo environment $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.2']); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $headers->set('Accept', 'text/html'); $cookies = []; $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); $app->getContainer()['request'] = $req; $app->getContainer()['response'] = $res; // Set the options value. $this->options = ['error_code' => 403, 'exception_message' => 'NOT ALLOWED']; $app->add(new IpRestrictMiddleware($this->ipSet, false, $this->options)); $appMessage = 'I am In'; $app->get('/foo', function ($req, $res) use($appMessage) { $res->write($appMessage); return $res; }); $resOut = $app->run(); $body = (string) $resOut->getBody(); $this->assertEquals($appMessage, $body, 'The client is allowed to access the application.'); }
public function testGetNoPostFromId() { $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/api/posts/124', 'REQUEST_METHOD' => 'GET']); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $serverParams = $env->all(); $cookies = []; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); $headers = new Headers(); $serverParams = $env->all(); // Invoke app $app = $this->myApp; $resOut = $app($req, $res); $post = json_decode($resOut->getBody()); $this->assertInstanceOf('\\Psr\\Http\\Message\\ResponseInterface', $resOut); $this->assertEquals([], $resOut->getHeader('Content-Length')); $this->assertEquals(200, $resOut->getStatusCode()); $this->assertTrue(isset($post->post)); $this->assertFalse(isset($post->post->id)); }
/** * @runInSeparateProcess */ public function testExceptionErrorHandlerDisplaysErrorDetails() { $app = new App(['settings' => ['displayErrorDetails' => true]]); // Prepare request and response objects $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET']); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); $app->getContainer()['request'] = $req; $app->getContainer()['response'] = $res; $mw = function ($req, $res, $next) { throw new \Exception('middleware exception'); }; $app->add($mw); $app->get('/foo', function ($req, $res) { return $res; }); $resOut = $app->run(); $this->assertEquals(500, $resOut->getStatusCode()); $this->expectOutputRegex('/.*middleware exception.*/'); }
/** * @covers Slim\Http\Uri::createFromEnvironment * @ticket 1380 */ public function testWithPathWhenBaseRootIsEmpty() { $environment = \Slim\Http\Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/bar']); $uri = \Slim\Http\Uri::createFromEnvironment($environment); $this->assertEquals('http://localhost/test', (string) $uri->withPath('test')); }
/** * SlimアプリケーションのMockを作成 * * @param String $path アクセスするURI * @param String $method 使用するhttpメソッド * @return Object */ protected function create($path, $method = 'GET') { $app = new App(); $env = Environment::mock(['REQUEST_URI' => $path, 'REQUEST_METHOD' => $method]); $uri = Uri::createFromEnvironment($env); //$headers = Headers::createFromEnvironment($env); $headers = new Headers(['Content-Type' => 'application/json;charset=utf8']); $cookies = []; $serverParams = $env->all(); $this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $this->body); $this->response = new Response(); $this->setContainer($app->getContainer()); return $app; }