RequestTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. class RequestTest extends PHPUnit_Framework_TestCase {
  3. public function setUp()
  4. {
  5. $_POST = array();
  6. $_SERVER = array();
  7. Laravel\Request::$uri = null;
  8. }
  9. /**
  10. * @dataProvider requestUriProvider
  11. */
  12. public function test_correct_uri_is_returned_when_request_uri_is_used($uri, $expectation)
  13. {
  14. $_SERVER['REQUEST_URI'] = $uri;
  15. $this->assertEquals($expectation, Laravel\Request::uri()->get());
  16. }
  17. public function test_request_method_returns_spoofed_method_if_uri_is_spoofed()
  18. {
  19. $_POST = array(Laravel\Request::spoofer => 'something');
  20. $this->assertEquals('something', Laravel\Request::method());
  21. }
  22. public function test_request_method_returns_request_method_from_server_array()
  23. {
  24. $_SERVER['REQUEST_METHOD'] = 'PUT';
  25. $this->assertEquals('PUT', Laravel\Request::method());
  26. }
  27. public function test_format_method_returns_extension()
  28. {
  29. $this->assertEquals('html', Laravel\Request::format('user'));
  30. $this->assertEquals('json', Laravel\Request::format('user.json'));
  31. }
  32. public function test_server_method_returns_from_the_server_array()
  33. {
  34. $_SERVER = array('TEST' => 'something', 'USER' => array('NAME' => 'taylor'));
  35. $this->assertEquals('something', Laravel\Request::server('test'));
  36. $this->assertEquals('taylor', Laravel\Request::server('user.name'));
  37. }
  38. public function test_spoofed_returns_true_when_request_is_spoofed()
  39. {
  40. $_POST[Laravel\Request::spoofer] = 'something';
  41. $this->assertTrue(Laravel\Request::spoofed());
  42. }
  43. public function test_spoofed_returns_false_when_request_isnt_spoofed()
  44. {
  45. $this->assertFalse(Laravel\Request::spoofed());
  46. }
  47. public function test_ip_method_returns_client_ip_address()
  48. {
  49. $_SERVER['REMOTE_ADDR'] = 'something';
  50. $this->assertEquals('something', Laravel\Request::ip());
  51. $_SERVER['HTTP_CLIENT_IP'] = 'something';
  52. $this->assertEquals('something', Laravel\Request::ip());
  53. $_SERVER['HTTP_X_FORWARDED_FOR'] = 'something';
  54. $this->assertEquals('something', Laravel\Request::ip());
  55. $_SERVER = array();
  56. $this->assertEquals('0.0.0.0', Laravel\Request::ip());
  57. }
  58. public function test_protocol_returns_server_protocol()
  59. {
  60. $_SERVER['SERVER_PROTOCOL'] = 'taylor';
  61. $this->assertEquals('taylor', Laravel\Request::protocol());
  62. unset($_SERVER['SERVER_PROTOCOL']);
  63. $this->assertEquals('HTTP/1.1', Laravel\Request::protocol());
  64. }
  65. public function test_ajax_method_returns_false_when_not_ajax()
  66. {
  67. $this->assertFalse(Laravel\Request::ajax());
  68. }
  69. public function test_ajax_method_returns_true_when_ajax()
  70. {
  71. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
  72. $this->assertTrue(Laravel\Request::ajax());
  73. }
  74. public function requestUriProvider()
  75. {
  76. return array(
  77. array('/index.php', '/'),
  78. array('/index.php/', '/'),
  79. array('http://localhost/user', 'user'),
  80. array('http://localhost/user/', 'user'),
  81. array('http://localhost/index.php', '/'),
  82. array('http://localhost/index.php/', '/'),
  83. array('http://localhost/index.php//', '/'),
  84. array('http://localhost/index.php/user', 'user'),
  85. array('http://localhost/index.php/user/', 'user'),
  86. array('http://localhost/index.php/user/profile', 'user/profile'),
  87. );
  88. }
  89. }