/** * Test mock object */ public function testMockEnvironment() { $mock = array('REQUEST_METHOD' => 'PUT', 'SCRIPT_NAME' => '/foo', 'PATH_INFO' => '/bar', 'QUERY_STRING' => '', 'SERVER_NAME' => 'test.com', 'SERVER_PORT' => 8080, 'HTTP_HOST' => 'test.com', 'slim.url_scheme' => 'http', 'slim.input' => 'foo', 'slim.errors' => fopen('php://stderr', 'w')); Slim_Environment::mock($mock); $env = Slim_Environment::getInstance(); $this->assertEquals($mock, $env); }
/** * Test mock environment * * This should return the custom values where specified * and the default values otherwise. */ public function testMockEnvironment() { $env = Slim_Environment::mock(array('REQUEST_METHOD' => 'PUT')); $env2 = Slim_Environment::getInstance(); $this->assertSame($env, $env2); $this->assertInstanceOf('Slim_Environment', $env); $this->assertEquals('PUT', $env['REQUEST_METHOD']); $this->assertEquals(80, $env['SERVER_PORT']); $this->assertNull($env['foo']); }
/** * 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()); }
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 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']); }
/** * 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())); }
$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 { }
/** * 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()); }
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 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 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); }
/** * Test get refererer */ public function testGetUserAgent() { Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '/foo/index.php', 'PATH_INFO' => '/bar/xyz', '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_USER_AGENT' => 'user-agent-string')); $env = Slim_Environment::getInstance(); $req = new Slim_Http_Request($env); $this->assertEquals('user-agent-string', $req->getUserAgent()); }