GroupMiddlewareTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Barryvdh\Cors\Tests;
  3. class GroupMiddlewareTest extends TestCase
  4. {
  5. public function testOptionsAllowOriginAllowed()
  6. {
  7. $crawler = $this->call('OPTIONS', 'api/ping', [], [], [], [
  8. 'HTTP_ORIGIN' => 'localhost',
  9. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  10. ]);
  11. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  12. $this->assertEquals(200, $crawler->getStatusCode());
  13. }
  14. public function testAllowOriginAllowed()
  15. {
  16. $crawler = $this->call('POST', 'api/ping', [], [], [], [
  17. 'HTTP_ORIGIN' => 'localhost',
  18. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  19. ]);
  20. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  21. $this->assertEquals(200, $crawler->getStatusCode());
  22. $this->assertEquals('PONG', $crawler->getContent());
  23. }
  24. public function testAllowOriginNotAllowed()
  25. {
  26. $crawler = $this->call('POST', 'api/ping', [], [], [], [
  27. 'HTTP_ORIGIN' => 'otherhost',
  28. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  29. ]);
  30. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Origin'));
  31. $this->assertEquals(403, $crawler->getStatusCode());
  32. }
  33. public function testAllowMethodAllowed()
  34. {
  35. $crawler = $this->call('POST', 'api/ping', [], [], [], [
  36. 'HTTP_ORIGIN' => 'localhost',
  37. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  38. ]);
  39. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Methods'));
  40. $this->assertEquals(200, $crawler->getStatusCode());
  41. $this->assertEquals('PONG', $crawler->getContent());
  42. }
  43. public function testAllowMethodNotAllowed()
  44. {
  45. $crawler = $this->call('POST', 'api/ping', [], [], [], [
  46. 'HTTP_ORIGIN' => 'localhost',
  47. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'PUT',
  48. ]);
  49. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Methods'));
  50. $this->assertEquals(200, $crawler->getStatusCode());
  51. }
  52. public function testAllowMethodsForWebNotAllowed()
  53. {
  54. $crawler = $this->call('POST', 'web/ping', [], [], [], [
  55. 'HTTP_ORIGIN' => 'localhost',
  56. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  57. ]);
  58. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Methods'));
  59. $this->assertEquals(200, $crawler->getStatusCode());
  60. }
  61. public function testAllowHeaderAllowed()
  62. {
  63. $crawler = $this->call('POST', 'api/ping', [], [], [], [
  64. 'HTTP_ORIGIN' => 'localhost',
  65. 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' => 'x-custom-1, x-custom-2',
  66. ]);
  67. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Headers'));
  68. $this->assertEquals(200, $crawler->getStatusCode());
  69. $this->assertEquals('PONG', $crawler->getContent());
  70. }
  71. public function testAllowHeaderNotAllowed()
  72. {
  73. $crawler = $this->call('POST', 'api/ping', [], [], [], [
  74. 'HTTP_ORIGIN' => 'localhost',
  75. 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' => 'x-custom-3',
  76. ]);
  77. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Headers'));
  78. $this->assertEquals(200, $crawler->getStatusCode());
  79. }
  80. public function testError()
  81. {
  82. if ($this->checkVersion('5.3', '<')) {
  83. $this->markTestSkipped('Catching exceptions is not possible on Laravel 5.1');
  84. }
  85. $crawler = $this->call('POST', 'api/error', [], [], [], [
  86. 'HTTP_ORIGIN' => 'localhost',
  87. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  88. ]);
  89. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  90. $this->assertEquals(500, $crawler->getStatusCode());
  91. }
  92. public function testValidationException()
  93. {
  94. if ($this->checkVersion('5.3', '<')) {
  95. $this->markTestSkipped('Catching exceptions is not possible on Laravel 5.1');
  96. }
  97. $crawler = $this->call('POST', 'api/validation', [], [], [], [
  98. 'HTTP_ORIGIN' => 'localhost',
  99. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  100. ]);
  101. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  102. $this->assertEquals(302, $crawler->getStatusCode());
  103. }
  104. }