Skip to content
Permalink
Browse files

Renamed CakeRequest::onlyAllow() to CakeRequest::allowMethod().

Existing name is unintuitive and it's not easily apparent what
the method does. Closes #2803
  • Loading branch information...
ADmad committed Feb 10, 2014
1 parent be8ebfc commit bea30e62cbca0a8f143fcc4932c3f2d1da036bca
Showing with 34 additions and 12 deletions.
  1. +22 −6 lib/Cake/Network/CakeRequest.php
  2. +12 −6 lib/Cake/Test/Case/Network/CakeRequestTest.php
@@ -909,23 +909,23 @@ public function input($callback = null) {
}
/**
* Only allow certain HTTP request methods, if the request method does not match
* 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->onlyAllow('post', 'delete');
* $this->request->allowMethod('post', 'delete');
* or
* $this->request->onlyAllow(array('post', 'delete'));
* $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
* and a 405 error will be returned.
*
* @param string|array $methods Allowed HTTP request methods
* @param string|array $methods Allowed HTTP request methods.
* @return boolean true
* @throws MethodNotAllowedException
*/
public function onlyAllow($methods) {
public function allowMethod($methods) {
if (!is_array($methods)) {
$methods = func_get_args();
}
@@ -940,6 +940,22 @@ public function onlyAllow($methods) {
throw $e;
}
/**
* Alias of CakeRequest::allowMethod() for backwards compatibility.
*
* @see CakeRequest::allowMethod()
* @deprecated 2.5 Use CakeRequest::allowMethod() instead.
* @param string|array $methods Allowed HTTP request methods.
* @return boolean true
* @throws MethodNotAllowedException
*/
public function onlyAllow($methods) {
if (!is_array($methods)) {
$methods = func_get_args();
}
return $this->allowMethod($methods);
}
/**
* Read data from php://input, mocked in tests.
*
@@ -2221,38 +2221,44 @@ public function testIsRequested() {
}
/**
* Test onlyAllow method
* Test allowMethod method
*
* @return void
*/
public function testOnlyAllow() {
public function testAllowMethod() {
$_SERVER['REQUEST_METHOD'] = 'PUT';
$request = new CakeRequest('/posts/edit/1');
$this->assertTrue($request->allowMethod(array('put')));
// BC check
$this->assertTrue($request->onlyAllow(array('put')));
$_SERVER['REQUEST_METHOD'] = 'DELETE';
$this->assertTrue($request->allowMethod('post', 'delete'));
// BC check
$this->assertTrue($request->onlyAllow('post', 'delete'));
}
/**
* Test onlyAllow throwing exception
* Test allowMethod throwing exception
*
* @return void
*/
public function testOnlyAllowException() {
public function testAllowMethodException() {
$_SERVER['REQUEST_METHOD'] = 'PUT';
$request = new CakeRequest('/posts/edit/1');
try {
$request->onlyAllow('POST', 'DELETE');
$request->allowMethod('POST', 'DELETE');
$this->fail('An expected exception has not been raised.');
} catch (MethodNotAllowedException $e) {
$this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
}
$this->setExpectedException('MethodNotAllowedException');
$request->onlyAllow('POST');
$request->allowMethod('POST');
}
/**

0 comments on commit bea30e6

Please sign in to comment.
You can’t perform that action at this time.