GlobalMiddlewareTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Barryvdh\Cors\Tests;
  3. class GlobalMiddlewareTest extends TestCase
  4. {
  5. /**
  6. * Define environment setup.
  7. *
  8. * @param Illuminate\Foundation\Application $app
  9. *
  10. * @return void
  11. */
  12. protected function getEnvironmentSetUp($app)
  13. {
  14. // Add the middleware
  15. $kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class);
  16. $kernel->prependMiddleware(\Barryvdh\Cors\HandleCors::class);
  17. parent::getEnvironmentSetUp($app);
  18. }
  19. public function testShouldReturnNullOnHeaderAssessControlAllowOriginBecauseDontHaveHttpOriginOnRequest()
  20. {
  21. $crawler = $this->call('OPTIONS', 'api/ping', [], [], [], [
  22. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  23. ]);
  24. $this->assertNull($crawler->headers->get('Access-Control-Allow-Origin'));
  25. $this->assertEquals(200, $crawler->getStatusCode());
  26. }
  27. public function testOptionsAllowOriginAllowed()
  28. {
  29. $crawler = $this->call('OPTIONS', 'api/ping', [], [], [], [
  30. 'HTTP_ORIGIN' => 'localhost',
  31. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  32. ]);
  33. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  34. $this->assertEquals(200, $crawler->getStatusCode());
  35. }
  36. public function testOptionsAllowOriginAllowedNonExistingRoute()
  37. {
  38. $crawler = $this->call('OPTIONS', 'api/pang', [], [], [], [
  39. 'HTTP_ORIGIN' => 'localhost',
  40. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  41. ]);
  42. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  43. $this->assertEquals(200, $crawler->getStatusCode());
  44. }
  45. public function testOptionsAllowOriginNotAllowed()
  46. {
  47. $crawler = $this->call('OPTIONS', 'api/ping', [], [], [], [
  48. 'HTTP_ORIGIN' => 'otherhost',
  49. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  50. ]);
  51. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Origin'));
  52. $this->assertEquals(403, $crawler->getStatusCode());
  53. }
  54. public function testAllowOriginAllowed()
  55. {
  56. $crawler = $this->call('POST', 'web/ping', [], [], [], [
  57. 'HTTP_ORIGIN' => 'localhost',
  58. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  59. ]);
  60. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  61. $this->assertEquals(200, $crawler->getStatusCode());
  62. $this->assertEquals('PONG', $crawler->getContent());
  63. }
  64. public function testAllowOriginNotAllowed()
  65. {
  66. $crawler = $this->call('POST', 'web/ping', [], [], [], [
  67. 'HTTP_ORIGIN' => 'otherhost',
  68. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  69. ]);
  70. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Origin'));
  71. $this->assertEquals(403, $crawler->getStatusCode());
  72. }
  73. public function testAllowMethodAllowed()
  74. {
  75. $crawler = $this->call('POST', 'web/ping', [], [], [], [
  76. 'HTTP_ORIGIN' => 'localhost',
  77. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  78. ]);
  79. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Methods'));
  80. $this->assertEquals(200, $crawler->getStatusCode());
  81. $this->assertEquals('PONG', $crawler->getContent());
  82. }
  83. public function testAllowMethodNotAllowed()
  84. {
  85. $crawler = $this->call('POST', 'web/ping', [], [], [], [
  86. 'HTTP_ORIGIN' => 'localhost',
  87. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'PUT',
  88. ]);
  89. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Methods'));
  90. $this->assertEquals(200, $crawler->getStatusCode());
  91. }
  92. public function testAllowHeaderAllowed()
  93. {
  94. $crawler = $this->call('POST', 'web/ping', [], [], [], [
  95. 'HTTP_ORIGIN' => 'localhost',
  96. 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' => 'x-custom-1, x-custom-2',
  97. ]);
  98. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Headers'));
  99. $this->assertEquals(200, $crawler->getStatusCode());
  100. $this->assertEquals('PONG', $crawler->getContent());
  101. }
  102. public function testAllowHeaderNotAllowed()
  103. {
  104. $crawler = $this->call('POST', 'web/ping', [], [], [], [
  105. 'HTTP_ORIGIN' => 'localhost',
  106. 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' => 'x-custom-3',
  107. ]);
  108. $this->assertEquals(null, $crawler->headers->get('Access-Control-Allow-Headers'));
  109. $this->assertEquals(200, $crawler->getStatusCode());
  110. }
  111. public function testError()
  112. {
  113. if ($this->checkVersion('5.3', '<')) {
  114. $this->markTestSkipped('Catching exceptions is not possible on Laravel 5.1');
  115. }
  116. $crawler = $this->call('POST', 'web/error', [], [], [], [
  117. 'HTTP_ORIGIN' => 'localhost',
  118. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  119. ]);
  120. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  121. $this->assertEquals(500, $crawler->getStatusCode());
  122. }
  123. public function testValidationException()
  124. {
  125. if ($this->checkVersion('5.3', '<')) {
  126. $this->markTestSkipped('Catching exceptions is not possible on Laravel 5.1');
  127. }
  128. $crawler = $this->call('POST', 'web/validation', [], [], [], [
  129. 'HTTP_ORIGIN' => 'localhost',
  130. 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST',
  131. ]);
  132. $this->assertEquals('localhost', $crawler->headers->get('Access-Control-Allow-Origin'));
  133. $this->assertEquals(302, $crawler->getStatusCode());
  134. }
  135. }