request.test.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. class SessionPayloadTokenStub {
  3. public function token() { return 'Taylor'; }
  4. }
  5. class RequestTest extends PHPUnit_Framework_TestCase {
  6. /**
  7. * Tear down the test environment.
  8. */
  9. public function tearDown()
  10. {
  11. $_POST = array();
  12. $_SERVER = array();
  13. Request::$route = null;
  14. Session::$instance = null;
  15. }
  16. /**
  17. * Test the Request::method method.
  18. *
  19. * @group laravel
  20. */
  21. public function testMethodReturnsTheHTTPRequestMethod()
  22. {
  23. $_SERVER['REQUEST_METHOD'] = 'POST';
  24. $this->assertEquals('POST', Request::method());
  25. $_POST[Request::spoofer] = 'PUT';
  26. $this->assertEquals('PUT', Request::method());
  27. }
  28. /**
  29. * Test the Request::server method.
  30. *
  31. * @group laravel
  32. */
  33. public function testServerMethodReturnsFromServerArray()
  34. {
  35. $_SERVER = array('TEST' => 'something', 'USER' => array('NAME' => 'taylor'));
  36. $this->assertEquals('something', Request::server('test'));
  37. $this->assertEquals('taylor', Request::server('user.name'));
  38. }
  39. /**
  40. * Test the Request::ip method.
  41. *
  42. * @group laravel
  43. */
  44. public function testIPMethodReturnsClientIPAddress()
  45. {
  46. $_SERVER['REMOTE_ADDR'] = 'something';
  47. $this->assertEquals('something', Request::ip());
  48. $_SERVER['HTTP_CLIENT_IP'] = 'something';
  49. $this->assertEquals('something', Request::ip());
  50. $_SERVER['HTTP_X_FORWARDED_FOR'] = 'something';
  51. $this->assertEquals('something', Request::ip());
  52. $_SERVER = array();
  53. $this->assertEquals('0.0.0.0', Request::ip());
  54. }
  55. /**
  56. * Test the Request::protocol method.
  57. *
  58. * @group laravel
  59. */
  60. public function testProtocolMethodReturnsProtocol()
  61. {
  62. $_SERVER['SERVER_PROTOCOL'] = 'taylor';
  63. $this->assertEquals('taylor', Request::protocol());
  64. unset($_SERVER['SERVER_PROTOCOL']);
  65. $this->assertEquals('HTTP/1.1', Request::protocol());
  66. }
  67. /**
  68. * Test the Request::secure method.
  69. *
  70. * @group laravel
  71. */
  72. public function testSecureMethodsIndicatesIfHTTPS()
  73. {
  74. $_SERVER['HTTPS'] = 'on';
  75. $this->assertTrue(Request::secure());
  76. $_SERVER['HTTPS'] = 'off';
  77. $this->assertFalse(Request::secure());
  78. }
  79. /**
  80. * Test the Request::ajax method.
  81. *
  82. * @group laravel
  83. */
  84. public function testAjaxMethodIndicatesWhenAjax()
  85. {
  86. $this->assertFalse(Request::ajax());
  87. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
  88. $this->assertTrue(Request::ajax());
  89. }
  90. /**
  91. * Test the Request::forged method.
  92. *
  93. * @group laravel
  94. */
  95. public function testForgedMethodIndicatesIfRequestWasForged()
  96. {
  97. Session::$instance = new SessionPayloadTokenStub;
  98. Input::$input = array(Session::csrf_token => 'Foo');
  99. $this->assertTrue(Request::forged());
  100. Input::$input = array(Session::csrf_token => 'Taylor');
  101. $this->assertFalse(Request::forged());
  102. }
  103. /**
  104. * Test the Request::route method.
  105. *
  106. * @group laravel
  107. */
  108. public function testRouteMethodReturnsStaticRoute()
  109. {
  110. Request::$route = 'Taylor';
  111. $this->assertEquals('Taylor', Request::route());
  112. }
  113. }