CorsTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace Asm89\Stack;
  3. use PHPUnit_Framework_TestCase;
  4. use Symfony\Component\HttpKernel\HttpKernelInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. class CorsTest extends PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @test
  11. */
  12. public function it_does_not_modify_on_a_request_without_origin()
  13. {
  14. $app = $this->createStackedApp();
  15. $unmodifiedResponse = new Response();
  16. $response = $app->handle(new Request());
  17. $this->assertEquals($unmodifiedResponse->headers, $response->headers);
  18. }
  19. /**
  20. * @test
  21. */
  22. public function it_returns_403_on_valid_actual_request_with_origin_not_allowed()
  23. {
  24. $app = $this->createStackedApp(array('allowedOrigins' => array('notlocalhost')));
  25. $request = $this->createValidActualRequest();
  26. $response = $app->handle($request);
  27. $this->assertEquals(403, $response->getStatusCode());
  28. }
  29. /**
  30. * @test
  31. */
  32. public function it_returns_allow_origin_header_on_valid_actual_request()
  33. {
  34. $app = $this->createStackedApp();
  35. $request = $this->createValidActualRequest();
  36. $response = $app->handle($request);
  37. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  38. $this->assertEquals('localhost', $response->headers->get('Access-Control-Allow-Origin'));
  39. }
  40. /**
  41. * @test
  42. */
  43. public function it_returns_allow_origin_header_on_allow_all_origin_request()
  44. {
  45. $app = $this->createStackedApp(array('allowedOrigins' => array('*')));
  46. $request = new Request();
  47. $request->headers->set('Origin', 'http://localhost');
  48. $response = $app->handle($request);
  49. $this->assertEquals(200, $response->getStatusCode());
  50. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  51. $this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
  52. }
  53. /**
  54. * @test
  55. */
  56. public function it_returns_allow_headers_header_on_allow_all_headers_request()
  57. {
  58. $app = $this->createStackedApp(array('allowedHeaders' => array('*')));
  59. $request = $this->createValidPreflightRequest();
  60. $request->headers->set('Access-Control-Request-Headers', 'Foo, BAR');
  61. $response = $app->handle($request);
  62. $this->assertEquals(200, $response->getStatusCode());
  63. $this->assertEquals('FOO, BAR', $response->headers->get('Access-Control-Allow-Headers'));
  64. }
  65. /**
  66. * @test
  67. */
  68. public function it_does_not_return_allow_origin_header_on_valid_actual_request_with_origin_not_allowed()
  69. {
  70. $app = $this->createStackedApp(array('allowedOrigins' => array('notlocalhost')));
  71. $request = $this->createValidActualRequest();
  72. $response = $app->handle($request);
  73. $this->assertFalse($response->headers->has('Access-Control-Allow-Origin'));
  74. }
  75. /**
  76. * @test
  77. */
  78. public function it_sets_allow_credentials_header_when_flag_is_set_on_valid_actual_request()
  79. {
  80. $app = $this->createStackedApp(array('supportsCredentials' => true));
  81. $request = $this->createValidActualRequest();
  82. $response = $app->handle($request);
  83. $this->assertTrue($response->headers->has('Access-Control-Allow-Credentials'));
  84. $this->assertEquals('true', $response->headers->get('Access-Control-Allow-Credentials'));
  85. }
  86. /**
  87. * @test
  88. */
  89. public function it_does_not_set_allow_credentials_header_when_flag_is_not_set_on_valid_actual_request()
  90. {
  91. $app = $this->createStackedApp();
  92. $request = $this->createValidActualRequest();
  93. $response = $app->handle($request);
  94. $this->assertFalse($response->headers->has('Access-Control-Allow-Credentials'));
  95. }
  96. /**
  97. * @test
  98. */
  99. public function it_sets_exposed_headers_when_configured_on_actual_request()
  100. {
  101. $app = $this->createStackedApp(array('exposedHeaders' => array('x-exposed-header', 'x-another-exposed-header')));
  102. $request = $this->createValidActualRequest();
  103. $response = $app->handle($request);
  104. $this->assertTrue($response->headers->has('Access-Control-Expose-Headers'));
  105. $this->assertEquals('x-exposed-header, x-another-exposed-header', $response->headers->get('Access-Control-Expose-Headers'));
  106. }
  107. /**
  108. * @test
  109. * @see http://www.w3.org/TR/cors/index.html#resource-implementation
  110. */
  111. public function it_adds_a_vary_header()
  112. {
  113. $app = $this->createStackedApp();
  114. $request = $this->createValidActualRequest();
  115. $response = $app->handle($request);
  116. $this->assertTrue($response->headers->has('Vary'));
  117. $this->assertEquals('Origin', $response->headers->get('Vary'));
  118. }
  119. /**
  120. * @test
  121. * @see http://www.w3.org/TR/cors/index.html#resource-implementation
  122. */
  123. public function it_appends_an_existing_vary_header()
  124. {
  125. $app = $this->createStackedApp(array(), array('Vary' => 'Content-Type'));
  126. $request = $this->createValidActualRequest();
  127. $response = $app->handle($request);
  128. $this->assertTrue($response->headers->has('Vary'));
  129. $this->assertEquals('Content-Type, Origin', $response->headers->get('Vary'));
  130. }
  131. /**
  132. * @test
  133. */
  134. public function it_returns_access_control_headers_on_cors_request()
  135. {
  136. $app = $this->createStackedApp();
  137. $request = new Request();
  138. $request->headers->set('Origin', 'localhost');
  139. $response = $app->handle($request);
  140. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  141. $this->assertEquals('localhost', $response->headers->get('Access-Control-Allow-Origin'));
  142. }
  143. /**
  144. * @test
  145. */
  146. public function it_returns_access_control_headers_on_valid_preflight_request()
  147. {
  148. $app = $this->createStackedApp();
  149. $request = $this->createValidPreflightRequest();
  150. $response = $app->handle($request);
  151. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  152. $this->assertEquals('localhost', $response->headers->get('Access-Control-Allow-Origin'));
  153. }
  154. /**
  155. * @test
  156. */
  157. public function it_returns_403_on_valid_preflight_request_with_origin_not_allowed()
  158. {
  159. $app = $this->createStackedApp(array('allowedOrigins' => array('notlocalhost')));
  160. $request = $this->createValidPreflightRequest();
  161. $response = $app->handle($request);
  162. $this->assertEquals(403, $response->getStatusCode());
  163. }
  164. /**
  165. * @test
  166. */
  167. public function it_does_not_modify_request_with_origin_not_allowed()
  168. {
  169. $passedOptions = array(
  170. 'allowedOrigins' => array('notlocalhost'),
  171. );
  172. $service = new CorsService($passedOptions);
  173. $request = $this->createValidActualRequest();
  174. $response = new Response();
  175. $service->addActualRequestHeaders($response, $request);
  176. $this->assertEquals($response, new Response());
  177. }
  178. /**
  179. * @test
  180. */
  181. public function it_returns_405_on_valid_preflight_request_with_method_not_allowed()
  182. {
  183. $app = $this->createStackedApp(array('allowedMethods' => array('put')));
  184. $request = $this->createValidPreflightRequest();
  185. $response = $app->handle($request);
  186. $this->assertEquals(405, $response->getStatusCode());
  187. }
  188. /**
  189. * @test
  190. */
  191. public function it_allow_methods_on_valid_preflight_request()
  192. {
  193. $app = $this->createStackedApp(array('allowedMethods' => array('get', 'put')));
  194. $request = $this->createValidPreflightRequest();
  195. $response = $app->handle($request);
  196. $this->assertTrue($response->headers->has('Access-Control-Allow-Methods'));
  197. // it will uppercase the methods
  198. $this->assertEquals('GET, PUT', $response->headers->get('Access-Control-Allow-Methods'));
  199. }
  200. /**
  201. * @test
  202. */
  203. public function it_returns_valid_preflight_request_with_allow_methods_all()
  204. {
  205. $app = $this->createStackedApp(array('allowedMethods' => array('*')));
  206. $request = $this->createValidPreflightRequest();
  207. $response = $app->handle($request);
  208. $this->assertTrue($response->headers->has('Access-Control-Allow-Methods'));
  209. // it will return the Access-Control-Request-Method pass in the request
  210. $this->assertEquals('GET', $response->headers->get('Access-Control-Allow-Methods'));
  211. }
  212. /**
  213. * @test
  214. */
  215. public function it_returns_403_on_valid_preflight_request_with_one_of_the_requested_headers_not_allowed()
  216. {
  217. $app = $this->createStackedApp();
  218. $request = $this->createValidPreflightRequest();
  219. $request->headers->set('Access-Control-Request-Headers', 'x-not-allowed-header');
  220. $response = $app->handle($request);
  221. $this->assertEquals(403, $response->getStatusCode());
  222. }
  223. /**
  224. * @test
  225. */
  226. public function it_returns_ok_on_valid_preflight_request_with_requested_headers_allowed()
  227. {
  228. $app = $this->createStackedApp();
  229. $requestHeaders = 'X-Allowed-Header, x-other-allowed-header';
  230. $request = $this->createValidPreflightRequest();
  231. $request->headers->set('Access-Control-Request-Headers', $requestHeaders);
  232. $response = $app->handle($request);
  233. $this->assertEquals(200, $response->getStatusCode());
  234. $this->assertTrue($response->headers->has('Access-Control-Allow-Headers'));
  235. // the response will have the "allowedHeaders" value passed to Cors rather than the request one
  236. $this->assertEquals('x-allowed-header, x-other-allowed-header', $response->headers->get('Access-Control-Allow-Headers'));
  237. }
  238. /**
  239. * @test
  240. */
  241. public function it_sets_allow_credentials_header_when_flag_is_set_on_valid_preflight_request()
  242. {
  243. $app = $this->createStackedApp(array('supportsCredentials' => true));
  244. $request = $this->createValidPreflightRequest();
  245. $response = $app->handle($request);
  246. $this->assertTrue($response->headers->has('Access-Control-Allow-Credentials'));
  247. $this->assertEquals('true', $response->headers->get('Access-Control-Allow-Credentials'));
  248. }
  249. /**
  250. * @test
  251. */
  252. public function it_does_not_set_allow_credentials_header_when_flag_is_not_set_on_valid_preflight_request()
  253. {
  254. $app = $this->createStackedApp();
  255. $request = $this->createValidPreflightRequest();
  256. $response = $app->handle($request);
  257. $this->assertFalse($response->headers->has('Access-Control-Allow-Credentials'));
  258. }
  259. /**
  260. * @test
  261. */
  262. public function it_sets_max_age_when_set()
  263. {
  264. $app = $this->createStackedApp(array('maxAge' => 42));
  265. $request = $this->createValidPreflightRequest();
  266. $response = $app->handle($request);
  267. $this->assertTrue($response->headers->has('Access-Control-Max-Age'));
  268. $this->assertEquals(42, $response->headers->get('Access-Control-Max-Age'));
  269. }
  270. private function createValidActualRequest()
  271. {
  272. $request = new Request();
  273. $request->headers->set('Origin', 'localhost');
  274. return $request;
  275. }
  276. private function createValidPreflightRequest()
  277. {
  278. $request = new Request();
  279. $request->headers->set('Origin', 'localhost');
  280. $request->headers->set('Access-Control-Request-Method', 'get');
  281. $request->setMethod('OPTIONS');
  282. return $request;
  283. }
  284. private function createStackedApp(array $options = array(), array $responseHeaders = array())
  285. {
  286. $passedOptions = array_merge(array(
  287. 'allowedHeaders' => array('x-allowed-header', 'x-other-allowed-header'),
  288. 'allowedMethods' => array('delete', 'get', 'post', 'put'),
  289. 'allowedOrigins' => array('localhost'),
  290. 'exposedHeaders' => false,
  291. 'maxAge' => false,
  292. 'supportsCredentials' => false,
  293. ),
  294. $options
  295. );
  296. return new Cors(new MockApp($responseHeaders), $passedOptions);
  297. }
  298. }
  299. class MockApp implements HttpKernelInterface
  300. {
  301. private $responseHeaders;
  302. public function __construct(array $responseHeaders)
  303. {
  304. $this->responseHeaders = $responseHeaders;
  305. }
  306. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  307. {
  308. $response = new Response();
  309. $response->headers->add($this->responseHeaders);
  310. return $response;
  311. }
  312. }