/** * 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\Network\Exception\MethodNotAllowedException */ public function allowMethod($methods) { $methods = (array) $methods; foreach ($methods as $method) { if ($this->is($method)) { return true; } } $allowed = strtoupper(implode(', ', $methods)); $e = new MethodNotAllowedException(); $e->responseHeader('Allow', $allowed); throw $e; }
/** * testExceptionResponseHeader method * * @return void */ public function testExceptionResponseHeader() { $exception = new MethodNotAllowedException('Only allowing POST and DELETE'); $exception->responseHeader(['Allow: POST, DELETE']); $ExceptionRenderer = new ExceptionRenderer($exception); $result = $ExceptionRenderer->render(); $headers = $result->header(); $this->assertArrayHasKey('Allow', $headers); $this->assertEquals('POST, DELETE', $headers['Allow']); }