Permalink
Browse files

Added option to allowed headers

  • Loading branch information...
1 parent 5d9d62b commit dae756c84a03d7c45505a892ac72758317a52a65 @jrbasso jrbasso committed Jan 21, 2014
Showing with 39 additions and 31 deletions.
  1. +3 −1 lib/Cake/Network/CakeResponse.php
  2. +36 −30 lib/Cake/Test/Case/Network/CakeResponseTest.php
View
4 lib/Cake/Network/CakeResponse.php
@@ -1263,9 +1263,10 @@ public function cookie($options = null) {
* @param CakeRequest $request Request object
* @param string|array $allowedDomains List of allowed domains, see method description for more details
* @param string|array $allowedMethods List of HTTP verbs allowed
+ * @param string|array $allowedHeaders List of HTTP headers allowed
* @return void
*/
- public function cors(CakeRequest $request, $allowedDomains, $allowedMethods = array()) {
+ public function cors(CakeRequest $request, $allowedDomains, $allowedMethods = array(), $allowedHeaders = array()) {
$origin = $request->header('Origin');
if (!$origin) {
return;
@@ -1278,6 +1279,7 @@ public function cors(CakeRequest $request, $allowedDomains, $allowedMethods = ar
}
$this->header('Access-Control-Allow-Origin', $domain['original'] === '*' ? '*' : $origin);
$allowedMethods && $this->header('Access-Control-Allow-Methods', implode(', ', (array)$allowedMethods));
+ $allowedHeaders && $this->header('Access-Control-Allow-Headers', implode(', ', (array)$allowedHeaders));
break;
}
}
View
66 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -1069,31 +1069,33 @@ public function testCookieSettings() {
* @param string $origin
* @param string|array $domains
* @param string|array $methods
+ * @param string|array $headers
* @param string|boolean $expectedOrigin
* @param string|boolean $expectedMethods
+ * @param string|boolean $expectedHeaders
* @return void
*/
- public function testCors($request, $origin, $domains, $methods, $expectedOrigin, $expectedMethods) {
+ public function testCors($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods = false, $expectedHeaders = false) {
$_SERVER['HTTP_ORIGIN'] = $origin;
$response = $this->getMock('CakeResponse', array('header'));
- if ($expectedOrigin === false) {
- $response->expects($this->never())
- ->method('header');
- } elseif ($expectedMethods === false) {
- $response->expects($this->once())
- ->method('header')
- ->with('Access-Control-Allow-Origin', $expectedOrigin);
- } else {
- $response->expects($this->at(0))
+
+ $method = $response->expects(!$expectedOrigin ? $this->never() : $this->at(0))->method('header');
+ $expectedOrigin && $method->with('Access-Control-Allow-Origin', $expectedOrigin ? $expectedOrigin : $this->anything());
+
+ $i = 1;
+ if ($expectedMethods) {
+ $response->expects($this->at($i++))
->method('header')
- ->with('Access-Control-Allow-Origin', $expectedOrigin);
- $response->expects($this->at(1))
+ ->with('Access-Control-Allow-Methods', $expectedMethods ? $expectedMethods : $this->anything());
+ }
+ if ($expectedHeaders) {
+ $response->expects($this->at($i++))
->method('header')
- ->with('Access-Control-Allow-Methods', $expectedMethods);
+ ->with('Access-Control-Allow-Headers', $expectedHeaders ? $expectedHeaders : $this->anything());
}
- $response->cors($request, $domains, $methods);
+ $response->cors($request, $domains, $methods, $headers);
unset($_SERVER['HTTP_ORIGIN']);
}
@@ -1112,22 +1114,26 @@ public function corsData() {
->will($this->returnValue(true));
return array(
- array($fooRequest, null, '*', '', false, false),
- array($fooRequest, 'http://www.foo.com', '*', '', '*', false),
- array($fooRequest, 'http://www.foo.com', 'www.foo.com', '', 'http://www.foo.com', false),
- array($fooRequest, 'http://www.foo.com', '*.foo.com', '', 'http://www.foo.com', false),
- array($fooRequest, 'http://www.foo.com', 'http://*.foo.com', '', 'http://www.foo.com', false),
- array($fooRequest, 'http://www.foo.com', 'https://www.foo.com', '', false, false),
- array($fooRequest, 'http://www.foo.com', 'https://*.foo.com', '', false, false),
- array($fooRequest, 'http://www.foo.com', array('*.bar.com', '*.foo.com'), '', 'http://www.foo.com', false),
-
- array($secureRequest, 'https://www.bar.com', 'www.bar.com', '', 'https://www.bar.com', false),
- array($secureRequest, 'https://www.bar.com', 'http://www.bar.com', '', false, false),
- array($secureRequest, 'https://www.bar.com', '*.bar.com', '', 'https://www.bar.com', false),
-
- array($fooRequest, 'http://www.foo.com', '*', 'GET', '*', 'GET'),
- array($fooRequest, 'http://www.foo.com', '*.foo.com', 'GET', 'http://www.foo.com', 'GET'),
- array($fooRequest, 'http://www.foo.com', '*.foo.com', array('GET', 'POST'), 'http://www.foo.com', 'GET, POST'),
+ array($fooRequest, null, '*', '', '', false, false),
+ array($fooRequest, 'http://www.foo.com', '*', '', '', '*', false),
+ array($fooRequest, 'http://www.foo.com', 'www.foo.com', '', '', 'http://www.foo.com', false),
+ array($fooRequest, 'http://www.foo.com', '*.foo.com', '', '', 'http://www.foo.com', false),
+ array($fooRequest, 'http://www.foo.com', 'http://*.foo.com', '', '', 'http://www.foo.com', false),
+ array($fooRequest, 'http://www.foo.com', 'https://www.foo.com', '', '', false, false),
+ array($fooRequest, 'http://www.foo.com', 'https://*.foo.com', '', '', false, false),
+ array($fooRequest, 'http://www.foo.com', array('*.bar.com', '*.foo.com'), '', '', 'http://www.foo.com', false),
+
+ array($secureRequest, 'https://www.bar.com', 'www.bar.com', '', '', 'https://www.bar.com', false),
+ array($secureRequest, 'https://www.bar.com', 'http://www.bar.com', '', '', false, false),
+ array($secureRequest, 'https://www.bar.com', '*.bar.com', '', '', 'https://www.bar.com', false),
+
+ array($fooRequest, 'http://www.foo.com', '*', 'GET', '', '*', 'GET'),
+ array($fooRequest, 'http://www.foo.com', '*.foo.com', 'GET', '', 'http://www.foo.com', 'GET'),
+ array($fooRequest, 'http://www.foo.com', '*.foo.com', array('GET', 'POST'), '', 'http://www.foo.com', 'GET, POST'),
+
+ array($fooRequest, 'http://www.foo.com', '*', '', 'X-CakePHP', '*', false, 'X-CakePHP'),
+ array($fooRequest, 'http://www.foo.com', '*', '', array('X-CakePHP', 'X-MyApp'), '*', false, 'X-CakePHP, X-MyApp'),
+ array($fooRequest, 'http://www.foo.com', '*', array('GET', 'OPTIONS'), array('X-CakePHP', 'X-MyApp'), '*', 'GET, OPTIONS', 'X-CakePHP, X-MyApp'),
);
}

0 comments on commit dae756c

Please sign in to comment.