public function testHttpResponseCode() { $currentDi = Di::getDefault(); // ensure we have a clean DI component Di::setDefault(new Di()); $this->assertEquals(200, \Vectorface\SnappyRouter\http_response_code()); Di::setDefault($currentDi); }
/** * Test the HTTPBasicAuthenticationPlugin; All in one test! */ public function testBasicHTTPAuth() { $ignored = new ControllerHandler(array()); /* Configure DI */ $di = new Di(array('MyCustomAuth' => false)); Di::setDefault($di); /* Direct testing. */ $plugin = new HttpBasicAuthenticationPlugin(array('AuthMechanism' => 'MyCustomAuth', 'realm' => 'Authentication Test')); try { $plugin->afterHandlerSelected($ignored); $this->fail("An invalid authenticator should yield an internal error"); } catch (InternalErrorException $e) { $this->assertEquals(500, $e->getAssociatedStatusCode()); /* HTTP 500 ISE */ } /* From here on out, use the "Do whatever I say" authenticator. :) */ $bool = false; $auth = new CallbackAuthenticator(function () use(&$bool) { return $bool; }); $di->set('MyCustomAuth', $auth); $_SERVER['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_PW'] = null; try { $plugin->afterHandlerSelected($ignored); $this->fail("No username and password are available. UnauthorizedException expected."); } catch (UnauthorizedException $e) { $this->assertEquals(401, $e->getAssociatedStatusCode()); /* HTTP 401 Unauthorized */ } $_SERVER['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_PW'] = 'ignored'; try { $plugin->afterHandlerSelected($ignored); $this->fail("Callback expected to return false auth result. UnauthorizedException expected."); } catch (UnauthorizedException $e) { // we expect the exception to be thrown } /* With a true result, preInvoke should pass through. */ $bool = true; $this->assertTrue($bool); $plugin->afterHandlerSelected($ignored); }
/** * Parses the config, sets up the default DI and registers the config * in the DI. */ private function parseConfig() { // setup the DI layer $diClass = $this->config->get(Config::KEY_DI); if (class_exists($diClass)) { $di = new $diClass(); if ($di instanceof DiInterface) { Di::setDefault($di); } } Di::getDefault()->set(self::KEY_CONFIG, $this->config); $this->setupHandlers($this->config->get(Config::KEY_HANDLERS, array())); }