コード例 #1
0
ファイル: CakeRequest.php プロジェクト: mrbadao/api-official
 /**
  * Allow only certain HTTP request methods. If the request method does not match
  * a 405 error will be shown and the required "Allow" response header will be set.
  *
  * Example:
  *
  * $this->request->allowMethod('post', 'delete');
  * or
  * $this->request->allowMethod(array('post', 'delete'));
  *
  * If the request would be GET, response header "Allow: POST, DELETE" will be set
  * and a 405 error will be returned.
  *
  * @param string|array $methods Allowed HTTP request methods.
  *
  * @return bool true
  * @throws MethodNotAllowedException
  */
 public function allowMethod($methods)
 {
     if (!is_array($methods)) {
         $methods = func_get_args();
     }
     foreach ($methods as $method) {
         if ($this->is($method)) {
             return TRUE;
         }
     }
     $allowed = strtoupper(implode(', ', $methods));
     $e = new MethodNotAllowedException();
     $e->responseHeader('Allow', $allowed);
     throw $e;
 }
コード例 #2
0
 /**
  * testExceptionResponseHeader method
  *
  * @return void
  */
 public function testExceptionResponseHeader()
 {
     $exception = new MethodNotAllowedException('Only allowing POST and DELETE');
     $exception->responseHeader(array('Allow: POST, DELETE'));
     $ExceptionRenderer = new ExceptionRenderer($exception);
     //Replace response object with mocked object add back the original headers which had been set in ExceptionRenderer constructor
     $headers = $ExceptionRenderer->controller->response->header();
     $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
     $ExceptionRenderer->controller->response->header($headers);
     $ExceptionRenderer->controller->response->expects($this->at(1))->method('_sendHeader')->with('Allow', 'POST, DELETE');
     ob_start();
     $ExceptionRenderer->render();
     ob_get_clean();
 }