/** * @param $method * @param $path * @param $body * @param $options * @return Request */ protected function requestFactory($method, $path, $body = [], $options = []) { $uri = Uri::createFromString($path); $headers = new Headers(); $cookies = []; $_POST['_METHOD'] = $method; if (strtolower($method) != 'get' && is_array($body)) { foreach ($body as $key => $value) { $_POST[$key] = $value; } } $envMethod = 'POST'; if (strtolower($method) == 'get') { $envMethod = 'GET'; } $env = Environment::mock(['REQUEST_URI' => $path, 'REQUEST_METHOD' => $envMethod, 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo']); $serverParams = $env->all(); $body = $this->buildBody($body); //echo $body->getContents(); // @todo // $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, []); $request = Request::createFromEnvironment($env); unset($_POST); return $request; }
public function testAbsoluteUrlGetsRouteAndAppendsPortWhenNotStandard() { $uri = SlimUri::createFromString('http://host:8443/path/page?query=1'); $this->router->shouldReceive('relativePathFor')->with('route.name', ['param1' => '1'], [])->andReturn('/test-route-page'); $url = new URI($this->router); $actual = $url->absoluteURIFor($uri, 'route.name', ['param1' => '1']); $this->assertSame('http://host:8443/test-route-page', $actual); }
public function requestFactory() { $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $headers = new Headers(); $cookies = []; $serverParams = []; $body = new Body(fopen('php://temp', 'r+')); return new Request('GET', $uri, $headers, $cookies, $serverParams, $body); }
/** * {@inheritdoc} */ public function createUri($uri) { if ($uri instanceof UriInterface) { return $uri; } if (is_string($uri)) { return Uri::createFromString($uri); } throw new \InvalidArgumentException('URI must be a string or UriInterface'); }
/** * @param $method * @param string $url * * @return Request */ public function makeRequest($method, $url = '/') { $env = Environment::mock(); $uri = Uri::createFromString('http://example.com' . $url); $headers = Headers::createFromEnvironment($env); $serverParams = $env->all(); $body = new RequestBody(); $uploadedFiles = UploadedFile::createFromEnvironment($env); $request = new Request($method, $uri, $headers, [], $serverParams, $body, $uploadedFiles); return $request; }
/** * Run before each test */ public function setUp() { $uri = \Slim\Http\Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $headers = new \Slim\Http\Headers(); $cookies = new \Slim\Collection(); $env = \Slim\Http\Environment::mock(); $serverParams = new \Slim\Collection($env->all()); $body = new \Slim\Http\Body(fopen('php://temp', 'r+')); $this->request = new \Slim\Http\Request('GET', $uri, $headers, $cookies, $serverParams, $body); $this->response = new \Slim\Http\Response(); }
public function requestFactory($acceptType = 'application/json') { $env = Environment::mock(['HTTP_ACCEPT' => $acceptType]); $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $headers = Headers::createFromEnvironment($env); $serverParams = $env->all(); $body = new RequestBody(); $uploadedFiles = UploadedFile::createFromEnvironment($env); $request = new Request('GET', $uri, $headers, [], $serverParams, $body, $uploadedFiles); return $request; }
/** * Run before each test. */ public function setUp() { $uri = Uri::createFromString('https://example.com:443/foo/bar'); $headers = new Headers(); $cookies = []; $env = Environment::mock(); $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $this->request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $this->response = new Response(); }
public function requestFactory($requestUrl) { $uri = Uri::createFromString($requestUrl); $headers = new Headers(); $cookies = []; $env = Slim\Http\Environment::mock(); $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); return $request; }
public function requestFactory($queryString = '') { $env = Environment::mock(); $uri = Uri::createFromString('https://example.com:443/foo/bar' . $queryString); $headers = Headers::createFromEnvironment($env); $cookies = ['user' => 'john', 'id' => '123']; $serverParams = $env->all(); $body = new RequestBody(); $uploadedFiles = UploadedFile::createFromEnvironment($env); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); return $request; }
private function requestFactory($endpoint) { // Request $uri = Uri::createFromString('https://example.com:443/' . $endpoint); $headers = new Headers(); $cookies = []; $serverParams = []; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $request = $request->withAttribute('route', new Route(['get'], '/' . $endpoint, 'CallableFunction')); return $request; }
/** * @param string $method * @param string $uri * @return Request */ public function makeRequest($method = 'GET', $uri = '') { $env = Environment::mock([]); $uri = Uri::createFromString($uri); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $uploadedFiles = UploadedFile::createFromEnvironment($env); $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); $request = $request->withHeader("Content-Type", "application/x-www-form-urlencoded"); return $request; }
/** * Setup for the XML POST requests. * * @param string $xml The XML to use to mock the body of the request. */ public function setUpXmlPost($xml) { $uri = Uri::createFromString('https://example.com:443/foo'); $headers = new Headers(); $headers->set('Content-Type', 'application/xml;charset=utf8'); $cookies = []; $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'POST']); $serverParams = $env->all(); $body = new RequestBody(); $body->write($xml); $this->request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); $this->response = new Response(); }
/** * run application with POST method. * adds C.S.R.F. token if $post is set. * * @param string $path * @param array $post */ protected function runPost($path = '', $post = []) { if (!empty($post)) { // add csrf tokens. $key = 'unit-csrf'; $val = 'unit-csrf-value'; $_SESSION['csrf'][$key] = $val; $post['csrf_name'] = $key; $post['csrf_value'] = $val; } $uri = Uri::createFromString($this->root_url . $path); $this->runApp('POST', $uri, $post); }
/** * Run before each test. */ public function setUp() { parent::setUp(); $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $this->request = new Request('GET', $uri, new Headers(), [], Environment::mock()->all(), new Body(fopen('php://temp', 'r+'))); $this->response = new Response(); // Prepare container and populate it with Slim services that are needed for further tests $this->addToContainer('callableResolver', function ($c) { return new CallableResolver($c); }); $this->addToContainer('foundHandler', function ($c) { return new RequestResponse(); }); }
/** * Set up the test environment. */ public function setUp() { parent::setUp(); $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $this->request = new Request('GET', $uri, new Headers(), [], Environment::mock()->all(), new Body(fopen('php://temp', 'r+'))); $this->response = new Response(); $this->container = new Container(); $this->container['callableResolver'] = function ($c) { return new CallableResolver($c); }; $this->container['foundHandler'] = function () { return new RequestResponse(); }; }
public function request($method, $path) { $routeConfig = __DIR__ . '/../../src/config/routes.yaml'; $app = new \app\RestFulWeb(); $app->setRouteFromFile($routeConfig); $app->setLogFromFile(__DIR__ . '/logs.yaml'); $env = Environment::mock(); $uri = Uri::createFromString('https://slim.dev' . $path); $headers = new Headers(); $cookies = []; $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $this->response = new Response(); $this->response = $app->getApp()->__invoke($request, new Response()); }
public function createUriObject($url = '') { return Uri::createFromString($url); }
/** * @expectedException \RuntimeException */ public function testMiddlewareBadReturnValue() { // Build middleware stack $stack = new Stackable(); $stack->add(function ($req, $res, $next) { $res = $res->write('In1'); $res = $next($req, $res); $res = $res->write('Out1'); // NOTE: No return value here }); // Request $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $headers = new Headers(); $cookies = []; $serverParams = []; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); // Response $response = new Response(); // Invoke call stack $stack->callMiddlewareStack($request, $response); }
/** * @param array $mockEnv An array representing a mock environment. * * @return Request */ public function requestFactory(array $mockEnv) { $env = Environment::mock(); $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $uploadedFiles = UploadedFile::createFromEnvironment($env); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); return $request; }
public function testCallingAContainerCallable() { $settings = ['foo' => function ($c) { return function ($a) { return $a; }; }]; $app = new App($settings); $result = $app->foo('bar'); $this->assertSame('bar', $result); $headers = new Headers(); $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', Uri::createFromString(''), $headers, [], [], $body); $response = new Response(); $response = $app->notFoundHandler($request, $response); $this->assertSame(404, $response->getStatusCode()); }
public function testGetForwardedIp() { $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $env = Environment::mock(['HTTP_X_FORWARDED_FOR' => '192.168.1.3, 192.168.1.2, 192.168.1.1']); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $this->assertEquals('192.168.1.3', $request->getIp()); }
protected function getSlimTest(string $method, string $url, array $headers = []) : ResponseInterface { $slim = new \Slim\App(['settings' => ['displayErrorDetails' => true]]); // add the CORs middleware $cors = $this->getCors($slim->getContainer()); $slim->add($cors); // finish adding the CORs middleware // add our own error handler. $errorHandler = function (ContainerInterface $container) : callable { $handler = function (ServerRequestInterface $request, ResponseInterface $response, \Exception $e) : ResponseInterface { $body = new Body(fopen('php://temp', 'r+')); $body->write('Error Handler caught exception type ' . get_class($e) . ': ' . $e->getMessage()); $return = $response->withStatus(500)->withBody($body); return $return; }; return $handler; }; // add the error handler. $slim->getContainer()['errorHandler'] = $errorHandler; // add dummy routes $slim->get('/foo', function (Request $req, Response $res) { $res->write('getted hi'); return $res; }); $slim->post('/foo', function (Request $req, Response $res) { $res->write('postted hi'); return $res; }); // Prepare request and response objects $uri = Uri::createFromString($url); $slimHeaders = new Headers($headers); $body = new RequestBody(); $request = new Request($method, $uri, $slimHeaders, [], [], $body); $response = new Response(); // override the Slim request and responses with our dummies $slim->getContainer()['request'] = $request; $slim->getContainer()['response'] = $response; // invoke Slim /* @var \Slim\Http\Response $result */ $result = $slim->run(true); // check we got back what we expected $this->assertInstanceOf('\\Psr\\Http\\Message\\ResponseInterface', $result); $this->assertInstanceOf('\\Slim\\Http\\Response', $result); return $result; }
/** * @expectedException \RuntimeException */ public function testGetParsedBodyAsArray() { $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); $headers = new Headers(['Content-Type' => 'application/json;charset=utf8']); $cookies = []; $serverParams = []; $body = new RequestBody(); $body->write('{"foo": "bar"}'); $body->rewind(); $request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); $request->registerMediaTypeParser('application/json', function ($input) { return 10; // <-- Return invalid body value }); $request->getParsedBody(); // <-- Triggers exception }
/** * @param Container $container The DI container. * @return void */ protected function registerHandlerServices(Container $container) { $config = $container['config']; if (!isset($container['base-url'])) { /** * Base URL as a PSR-7 UriInterface object for the current request * or the Charcoal application. * * @param Container $container * @return \Psr\Http\Message\UriInterface */ $container['base-url'] = function (Container $container) { if (isset($container['config']['base_url'])) { $baseUrl = $container['config']['base_url']; } else { $baseUrl = $container['request']->getUri()->getBaseUrl(); } $baseUrl = Uri::createFromString($baseUrl)->withUserInfo(''); return $baseUrl; }; } if (!isset($config['handlers'])) { return; } /** * HTTP 404 (Not Found) handler. * * @param object|HandlerInterface $handler An error handler instance. * @param Container $container A container instance. * @return HandlerInterface */ $container->extend('notFoundHandler', function ($handler, Container $container) use($config) { if ($handler instanceof \Slim\Handlers\NotFound) { $handler = new NotFound($container); if (isset($config['handlers']['notFound'])) { $handler->config()->merge($config['handlers']['notFound']); } $handler->init(); } return $handler; }); /** * HTTP 405 (Not Allowed) handler. * * @param object|HandlerInterface $handler An error handler instance. * @param Container $container A container instance. * @return HandlerInterface */ $container->extend('notAllowedHandler', function ($handler, Container $container) use($config) { if ($handler instanceof \Slim\Handlers\NotAllowed) { $handler = new NotAllowed($container); if (isset($config['handlers']['notAllowed'])) { $handler->config()->merge($config['handlers']['notAllowed']); } $handler->init(); } return $handler; }); /** * HTTP 500 (Error) handler for PHP 7+ Throwables. * * @param object|HandlerInterface $handler An error handler instance. * @param Container $container A container instance. * @return HandlerInterface */ $container->extend('phpErrorHandler', function ($handler, Container $container) use($config) { if ($handler instanceof \Slim\Handlers\PhpError) { $handler = new PhpError($container); if (isset($config['handlers']['phpError'])) { $handler->config()->merge($config['handlers']['phpError']); } $handler->init(); } return $handler; }); /** * HTTP 500 (Error) handler. * * @param object|HandlerInterface $handler An error handler instance. * @param Container $container A container instance. * @return HandlerInterface */ $container->extend('errorHandler', function ($handler, Container $container) use($config) { if ($handler instanceof \Slim\Handlers\Error) { $handler = new Error($container); if (isset($config['handlers']['error'])) { $handler->config()->merge($config['handlers']['error']); } $handler->init(); } return $handler; }); if (!isset($container['shutdownHandler'])) { /** * HTTP 503 (Service Unavailable) handler. * * This handler is not part of Slim. * * @param Container $container A container instance. * @return HandlerInterface */ $container['shutdownHandler'] = function (Container $container) { $config = $container['config']; $handler = new Shutdown($container); if (isset($config['handlers']['shutdown'])) { $handler->config()->merge($config['handlers']['shutdown']); } return $handler->init(); }; } }
/** * Ensure that if `outputBuffering` property is set to `false` correct response * body is returned by __invoke(). */ public function testInvokeWhenDisablingOutputBuffer() { ob_start(); $callable = function ($req, $res, $args) { echo 'foo'; return $res->write('bar'); }; $route = new Route(['GET'], '/', $callable); $route->setOutputBuffering(false); $env = Environment::mock(); $uri = Uri::createFromString('https://example.com:80'); $headers = new Headers(); $cookies = []; $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $response = new Response(); $response = $route->__invoke($request, $response); $this->assertEquals('bar', (string) $response->getBody()); $output = ob_get_clean(); $this->assertEquals('foo', $output); }
/** * @param string $redirection The route's destination. * @param RequestInterface $request A PSR-7 compatible Request instance. * @return Uri|null */ protected function parseRedirect($redirection, RequestInterface $request) { $uri = $request->getUri()->withUserInfo(''); $parts = parse_url($redirection); if (!empty($parts)) { if (isset($parts['host'])) { $uri = Uri::createFromString($redirection); } else { if (isset($parts['path'])) { $uri = $uri->withPath($parts['path']); } if (isset($parts['query'])) { $uri = $uri->withQuery($parts['query']); } if (isset($parts['fragment'])) { $uri = $uri->withFragment($parts['fragment']); } } if ((string) $uri !== (string) $request->getUri()) { return $uri; } } return null; }
/** * @covers Slim\Http\Uri::createFromString * @expectedException InvalidArgumentException * @expectedExceptionMessage Uri must be a string */ public function testCreateFromStringWithInvalidType() { Uri::createFromString(['https://example.com:8080/foo/bar?abc=123']); }
/** * Set the application's fully qualified base URL to the public web directory. * * @param string|\Psr\Http\Message\UriInterface $uri The base URI to the application's web directory. * @return AppConfig Chainable */ public function setBaseUrl($uri) { $this->baseUrl = \Slim\Http\Uri::createFromString($uri); return $this; }