Example #1
0
 /**
  * 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');
  * or
  * $this->request->allowMethod(['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 \Cake\Error\MethodNotAllowedException
  */
 public function allowMethod($methods)
 {
     $methods = (array) $methods;
     foreach ($methods as $method) {
         if ($this->is($method)) {
             return true;
         }
     }
     $allowed = strtoupper(implode(', ', $methods));
     $e = new Error\MethodNotAllowedException();
     $e->responseHeader('Allow', $allowed);
     throw $e;
 }
 /**
  * testExceptionResponseHeader method
  *
  * @return void
  */
 public function testExceptionResponseHeader()
 {
     $exception = new Error\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('Cake\\Network\\Response', 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();
 }