public function request($method, $path, $options = array()) { // Capture STDOUT ob_start(); // Prepare a mock environment Slim_Environment::mock(array_merge(array('REQUEST_METHOD' => $method, 'PATH_INFO' => $path, 'SERVER_NAME' => 'njh.me'), $options)); // Run the application require __DIR__ . '/../public/index.php'; $this->app = $app; $this->request = $app->request(); $this->response = $app->response(); // Return STDOUT return ob_get_clean(); }
/** * Test middleware returns diagnostic screen for error response */ public function testReturnsDiagnosticsForErrorResponse() { Slim_Environment::mock(array('SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/foo')); $app = new Slim(array('log.enabled' => false)); $app->get('/foo', function () { throw new Exception('Test Message', 100); }); $mw = new Slim_Middleware_PrettyExceptions(); $mw->setApplication($app); $mw->setNextMiddleware($app); $mw->call(); $this->assertEquals(1, preg_match('@Slim Application Error@', $app->response()->body())); $this->assertEquals(500, $app->response()->status()); }
/** * Test middleware overrides response content type to html */ public function testResponseContentTypeIsOverriddenToHtml() { Slim_Environment::mock(array('SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/foo')); $app = new Slim(array('log.enabled' => false)); $app->get('/foo', function () use($app) { $app->contentType('application/json;charset=utf-8'); //<-- set content type to something else throw new Exception('Test Message', 100); }); $mw = new Slim_Middleware_PrettyExceptions(); $mw->setApplication($app); $mw->setNextMiddleware($app); $mw->call(); $response = $app->response(); $this->assertEquals('text/html', $response['Content-Type']); }
$env_mock[substr($key, 5)] = $value; } else { if (strpos($key, 'X_') === 0 || in_array($key, $specialHeaders)) { $env_mock[$key] = $value; } } } $env_mock['PATH_INFO'] = $_REQUEST['route']; $env_mock['slim.url_scheme'] = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https'; $rawInput = @file_get_contents('php://input'); if (!$rawInput) { $rawInput = ''; } $env_mock['slim.input'] = $rawInput; $env_mock['slim.errors'] = fopen('php://stderr', 'w'); Slim_Environment::mock($env_mock); $we_betatext = new Slim(); // die Funktionen für die einzelnen Abfragetypen liegen in eigenen Dateien $method = strtolower($we_betatext->request()->getMethod()); require BBT_restpath . '/actions/' . $method . '.php'; // Standard-Funktionen function send_response($out) { global $we_betatext; if ($out !== false) { $response = $we_betatext->response(); $response['Content-Type'] = 'application/json'; $response['Cache-Control'] = 'no-cache'; echo json_encode($out); } else { }
/** * Get Instance * * This method returns the Singleton instance of the app * environment; if not already set or if a refreshed * environment is requested, parse the environment * and return the newly stored results. * * @param bool $refresh * @return array */ public static function getInstance($refresh = false) { if (!isset(self::$env) || $refresh) { self::$env = self::prepare(); } return self::$env; }
/** * Get mock environment instance * * @param array $userSettings * @return Environment */ public static function mock($userSettings = array()) { self::$environment = new self(array_merge(array('REQUEST_METHOD' => 'GET', 'SCRIPT_NAME' => '', 'PATH_INFO' => '', 'QUERY_STRING' => '', 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => 80, 'ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'ACCEPT_LANGUAGE' => 'en-US,en;q=0.8', 'ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'USER_AGENT' => 'Slim Framework', 'REMOTE_ADDR' => '127.0.0.1', 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => @fopen('php://stderr', 'w')), $userSettings)); return self::$environment; }
/** * Test $_SESSION is populated as empty array if no HTTP cookie */ public function testSessionIsPopulatedAsEmptyIfNoCookie() { Slim_Environment::mock(array('SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/foo')); $app = new Slim(); $app->get('/foo', function () { echo "Success"; }); $mw = new Slim_Middleware_SessionCookie(array('expires' => '10 years')); $mw->setApplication($app); $mw->setNextMiddleware($app); $mw->call(); $this->assertEquals(array(), $_SESSION); }
/** * Test get user agent string when not set */ public function testGetUserAgentWhenNotExists() { $env = Slim_Environment::mock(); unset($env['USER_AGENT']); $req = new Slim_Http_Request($env); $this->assertNull($req->getUserAgent()); }
/** * Test valid resource handle to php://stdErr */ public function testErrorResource() { $env = Slim_Environment::getInstance(true); $this->assertTrue(is_resource($env['slim.errors'])); }
/** * Test pass when there is not a subsequent fallback route */ public function testPassWithoutSubsequentRoute() { Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '/foo', 'PATH_INFO' => '/name/Frank', 'QUERY_STRING' => 'one=foo&two=bar', 'SERVER_NAME' => 'slimframework.com', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => @fopen('php://stderr', 'w'))); $s = new Slim(); $s->get('/name/Frank', function () use($s) { echo "Fail"; //<-- Should not be in response body! $s->pass(); }); $env = $s->environment(); list($status, $header, $body) = $s->call($env); $this->assertEquals(404, $status); }
/** * Constructor * @param array $userSettings Key-Value array of application settings * @return void */ public function __construct($userSettings = array()) { //Setup Slim application $this->environment = Slim_Environment::getInstance(); $this->request = new Slim_Http_Request($this->environment); $this->response = new Slim_Http_Response(); $this->router = new Slim_Router($this->request->getResourceUri()); $this->settings = array_merge(self::getDefaultSettings(), $userSettings); $this->middleware = array($this); $this->add(new Slim_Middleware_Flash()); $this->add(new Slim_Middleware_MethodOverride()); //Determine application mode $this->getMode(); //Setup view $this->view($this->config('view')); //Make default if first instance if (is_null(self::getInstance())) { $this->setName('default'); } //Set default logger that writes to stderr (may be overridden with middleware) $logWriter = $this->config('log.writer'); if (!$logWriter) { $logWriter = new Slim_LogWriter($this->environment['slim.errors']); } $log = new Slim_Log($logWriter); $log->setEnabled($this->config('log.enabled')); $log->setLevel($this->config('log.level')); $this->environment['slim.log'] = $log; //Set global error handler set_error_handler(array('Slim', 'handleErrors')); }
/** * Test pass when there is not a subsequent fallback route */ public function testPassWithoutSubsequentRoute() { Slim_Environment::mock(array('SCRIPT_NAME' => '/foo', 'PATH_INFO' => '/name/Frank')); $s = new Slim(); $s->get('/name/Frank', function () use($s) { echo "Fail"; //<-- Should not be in response body! $s->pass(); }); $s->call(); $this->assertEquals(404, $s->response()->status()); }
/** * Test get current route */ public function testGetCurrentRoute() { Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/foo')); $app = new Slim(); $route1 = $app->get('/bar', function () { echo "Bar"; }); $route2 = $app->get('/foo', function () { echo "Foo"; }); $app->call(); $this->assertSame($route2, $app->router()->getCurrentRoute()); }
/** * Test parses request body based on media-type only, disregarding * any extra content-type header parameters */ public function testParsesRequestBodyWithMediaType() { Slim_Environment::mock(array('REQUEST_METHOD' => 'POST', 'CONTENT_TYPE' => 'application/json; charset=ISO-8859-4', 'CONENT_LENGTH' => 13, 'slim.input' => '{"foo":"bar"}')); $s = new Slim(); $s->add(new Slim_Middleware_ContentTypes()); $s->run(); $body = $s->request()->getBody(); $this->assertTrue(is_array($body)); $this->assertEquals('bar', $body['foo']); }
/** * Test overrides method with X-Http-Method-Override header */ public function testOverrideMethodAsHeader() { Slim_Environment::mock(array('REQUEST_METHOD' => 'POST', 'CONTENT_TYPE' => 'application/json', 'CONENT_LENGTH' => 0, 'slim.input' => '', 'X_HTTP_METHOD_OVERRIDE' => 'DELETE')); $app = new CustomAppMethod(); $mw = new Slim_Middleware_MethodOverride(); $mw->setApplication($app); $mw->setNextMiddleware($app); $mw->call(); $env =& $app->environment(); $this->assertEquals('DELETE', $env['REQUEST_METHOD']); $this->assertTrue(isset($env['slim.method_override.original_method'])); $this->assertEquals('POST', $env['slim.method_override.original_method']); }
/** * Test that router returns matched routes based on URI only, not * based on the HTTP method. */ public function testRouterMatchesRoutesByUriOnly() { Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/foo', 'QUERY_STRING' => 'one=1&two=2&three=3', 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim')); $this->env = Slim_Environment::getInstance(); $this->req = new Slim_Http_Request($this->env); $this->res = new Slim_Http_Response(); $router = new Slim_Router($this->req, $this->res); $router->map('/foo', function () { })->via('GET'); $router->map('/foo', function () { })->via('POST'); $router->map('/foo', function () { })->via('PUT'); $router->map('/foo/bar/xyz', function () { })->via('DELETE'); $this->assertEquals(3, count($router->getMatchedRoutes())); }
public function testDispatchWithoutCallable() { Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/hello/josh', 'QUERY_STRING' => 'one=1&two=2&three=3', 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim')); $env = Slim_Environment::getInstance(); $req = new Slim_Http_Request($env); $res = new Slim_Http_Response(); $router = new Slim_Router($req, $res); $route = new Slim_Route('/hello/:name', 'foo'); $route->setRouter($router); $route->matches($req->getResourceUri()); //<-- Extracts params from resource URI $this->assertFalse($route->dispatch()); }
/** * Test get refererer */ public function testGetUserAgentWhenNotExists() { $env = Slim_Environment::getInstance(); $req = new Slim_Http_Request($env); $this->assertNull($req->getUserAgent()); }