request.test.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;
  3. class SessionPayloadTokenStub {
  4. public function token() { return 'Taylor'; }
  5. }
  6. class RequestTest extends PHPUnit_Framework_TestCase {
  7. /**
  8. * Tear down the test environment.
  9. */
  10. public function tearDown()
  11. {
  12. $_POST = array();
  13. $_SERVER = array();
  14. Request::$route = null;
  15. Session::$instance = null;
  16. }
  17. /**
  18. * Set one of the $_SERVER variables.
  19. *
  20. * @param string $key
  21. * @param string $value
  22. */
  23. protected function setServerVar($key, $value)
  24. {
  25. $_SERVER[$key] = $value;
  26. $this->restartRequest();
  27. }
  28. /**
  29. * Set one of the $_POST variables.
  30. *
  31. * @param string $key
  32. * @param string $value
  33. */
  34. protected function setPostVar($key, $value)
  35. {
  36. $_POST[$key] = $value;
  37. $this->restartRequest();
  38. }
  39. /**
  40. * Reinitialize the global request.
  41. *
  42. * @return void
  43. */
  44. protected function restartRequest()
  45. {
  46. // FIXME: Ugly hack, but old contents from previous requests seem to
  47. // trip up the Foundation class.
  48. $_FILES = array();
  49. Request::$foundation = RequestFoundation::createFromGlobals();
  50. }
  51. /**
  52. * Test the Request::method method.
  53. *
  54. * @group laravel
  55. */
  56. public function testMethodReturnsTheHTTPRequestMethod()
  57. {
  58. $this->setServerVar('REQUEST_METHOD', 'POST');
  59. $this->assertEquals('POST', Request::method());
  60. $this->setPostVar(Request::spoofer, 'PUT');
  61. $this->assertEquals('PUT', Request::method());
  62. }
  63. /**
  64. * Test the Request::server method.
  65. *
  66. * @group laravel
  67. */
  68. public function testServerMethodReturnsFromServerArray()
  69. {
  70. $this->setServerVar('TEST', 'something');
  71. $this->setServerVar('USER', array('NAME' => 'taylor'));
  72. $this->assertEquals('something', Request::server('test'));
  73. $this->assertEquals('taylor', Request::server('user.name'));
  74. }
  75. /**
  76. * Test the Request::ip method.
  77. *
  78. * @group laravel
  79. */
  80. public function testIPMethodReturnsClientIPAddress()
  81. {
  82. $this->setServerVar('REMOTE_ADDR', 'something');
  83. $this->assertEquals('something', Request::ip());
  84. $this->setServerVar('HTTP_CLIENT_IP', 'something');
  85. $this->assertEquals('something', Request::ip());
  86. $this->setServerVar('HTTP_CLIENT_IP', 'something');
  87. $this->assertEquals('something', Request::ip());
  88. $_SERVER = array();
  89. $this->restartRequest();
  90. $this->assertEquals('0.0.0.0', Request::ip());
  91. }
  92. /**
  93. * Test the Request::secure method.
  94. *
  95. * @group laravel
  96. */
  97. public function testSecureMethodsIndicatesIfHTTPS()
  98. {
  99. $this->setServerVar('HTTPS', 'on');
  100. $this->assertTrue(Request::secure());
  101. $this->setServerVar('HTTPS', 'off');
  102. $this->assertFalse(Request::secure());
  103. }
  104. /**
  105. * Test the Request::ajax method.
  106. *
  107. * @group laravel
  108. */
  109. public function testAjaxMethodIndicatesWhenAjax()
  110. {
  111. $this->assertFalse(Request::ajax());
  112. $this->setServerVar('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
  113. $this->assertTrue(Request::ajax());
  114. }
  115. /**
  116. * Test the Request::forged method.
  117. *
  118. * @group laravel
  119. */
  120. public function testForgedMethodIndicatesIfRequestWasForged()
  121. {
  122. Session::$instance = new SessionPayloadTokenStub;
  123. $input = array(Session::csrf_token => 'Foo');
  124. Request::foundation()->request->add($input);
  125. $this->assertTrue(Request::forged());
  126. $input = array(Session::csrf_token => 'Taylor');
  127. Request::foundation()->request->add($input);
  128. $this->assertFalse(Request::forged());
  129. }
  130. /**
  131. * Test the Request::route method.
  132. *
  133. * @group laravel
  134. */
  135. public function testRouteMethodReturnsStaticRoute()
  136. {
  137. Request::$route = 'Taylor';
  138. $this->assertEquals('Taylor', Request::route());
  139. }
  140. }