RequestTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Test\Message;
  9. use JonnyW\PhantomJs\Message\Request;
  10. /**
  11. * PHP PhantomJs
  12. *
  13. * @author Jon Wenmoth <contact@jonnyw.me>
  14. */
  15. class RequestTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * Test invalid URL's throw exception
  19. *
  20. * @dataProvider provideInvalidHosts
  21. *
  22. * @param string $url
  23. * @return void
  24. */
  25. public function testInvalidUrl($url)
  26. {
  27. $this->setExpectedException('JonnyW\\PhantomJs\\Exception\\InvalidUrlException');
  28. $request = new Request();
  29. $request->setUrl($url);
  30. }
  31. /**
  32. * Invalid host data providers
  33. *
  34. * @return array
  35. */
  36. public function provideInvalidHosts()
  37. {
  38. return array(
  39. array('invalid_url'),
  40. array('invalid_url.test')
  41. );
  42. }
  43. /**
  44. * Test invalid methods throw exception
  45. *
  46. * @dataProvider provideInvalidMethods
  47. *
  48. * @param string $method
  49. * @return void
  50. */
  51. public function testInvalidMethod($method)
  52. {
  53. $this->setExpectedException('JonnyW\\PhantomJs\\Exception\\InvalidMethodException');
  54. $request = new Request();
  55. $request->setMethod($method);
  56. }
  57. /**
  58. * Invalid method data providers
  59. *
  60. * @return array
  61. */
  62. public function provideInvalidMethods()
  63. {
  64. return array(
  65. array('GOT'),
  66. array('FIND')
  67. );
  68. }
  69. /**
  70. * Test invalid methods throw exception
  71. *
  72. * @return void
  73. */
  74. public function testGetRequestBody()
  75. {
  76. $data = array('name' => 'jonnyw', 'email' => 'contact@jonnyw.me');
  77. $request = new Request('GET', 'http://jonnyw.me');
  78. $request->setRequestData($data);
  79. $this->assertEmpty($request->getBody());
  80. }
  81. /**
  82. * Test GET URL parameters when URL
  83. * does not have existing parameters
  84. *
  85. * @return void
  86. */
  87. public function testGetUrlQuery()
  88. {
  89. $data = array('name' => 'jonnyw', 'email' => 'contact@jonnyw.me');
  90. $request = new Request('GET', 'http://jonnyw.me?query=true');
  91. $request->setRequestData($data);
  92. $this->assertEquals($request->getUrl(), 'http://jonnyw.me?query=true&' . urldecode(http_build_query($data)));
  93. }
  94. /**
  95. * Test GET URL parameters when URL
  96. * does not have existing parameters
  97. *
  98. * @return void
  99. */
  100. public function testGetUrlQueryClean()
  101. {
  102. $data = array('name' => 'jonnyw', 'email' => 'contact@jonnyw.me');
  103. $request = new Request('GET', 'http://jonnyw.me');
  104. $request->setRequestData($data);
  105. $this->assertEquals($request->getUrl(), 'http://jonnyw.me?' . urldecode(http_build_query($data)));
  106. }
  107. /**
  108. * Test invalid methods throw exception
  109. *
  110. * @return void
  111. */
  112. public function testPostRequestBody()
  113. {
  114. $data = array('name' => 'jonnyw', 'email' => 'contact@jonnyw.me');
  115. $request = new Request('POST', 'http://jonnyw.me');
  116. $request->setRequestData($data);
  117. $this->assertEquals($request->getBody(), urldecode(http_build_query($data)));
  118. }
  119. }