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); }
function http_response_code($code = null) { if (function_exists('\\http_response_code')) { // @codeCoverageIgnoreStart return \http_response_code($code); } // @codeCoverageIgnoreEnd $diKey = 'currentResponseCode'; if (null === $code) { try { return Di::getDefault()->get($diKey); } catch (Exception $e) { return Di::getDefault()->set($diKey, AbstractResponse::RESPONSE_OK)->get($diKey); } } $http_status_codes = array(100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-Status', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => '(Unused)', 307 => 'Temporary Redirect', 308 => 'Permanent Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 418 => 'I\'m a teapot', 419 => 'Authentication Timeout', 420 => 'Enhance Your Calm', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 424 => 'Method Failure', 425 => 'Unordered Collection', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 444 => 'No Response', 449 => 'Retry With', 450 => 'Blocked by Windows Parental Controls', 451 => 'Unavailable For Legal Reasons', 494 => 'Request Header Too Large', 495 => 'Cert Error', 496 => 'No Cert', 497 => 'HTTP to HTTPS', 499 => 'Client Closed Request', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 508 => 'Loop Detected', 509 => 'Bandwidth Limit Exceeded', 510 => 'Not Extended', 511 => 'Network Authentication Required', 598 => 'Network read timeout error', 599 => 'Network connect timeout error'); $code = intval($code); if (!isset($http_status_codes[$code])) { throw new Exception('Invalid response code: ' . $code); } $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; @header(sprintf('%s %s %s', $protocol, $code, $http_status_codes[$code])); Di::getDefault()->set($diKey, $code); }
/** * 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())); }
/** * Sets an element in the DI container for the specified key. * @param string $key The DI key. * @param mixed $element The DI element to store. * @return Di Returns the Di instance. */ public function set($key, $element) { return Di::getDefault()->set($key, $element); }