PostTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * This is a PHP library that handles calling reCAPTCHA.
  4. *
  5. * BSD 3-Clause License
  6. * @copyright (c) 2019, Google Inc.
  7. * @link https://www.google.com/recaptcha
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. Neither the name of the copyright holder nor the names of its
  20. * contributors may be used to endorse or promote products derived from
  21. * this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. namespace ReCaptcha\RequestMethod;
  35. use \ReCaptcha\ReCaptcha;
  36. use ReCaptcha\RequestParameters;
  37. use PHPUnit\Framework\TestCase;
  38. class PostTest extends TestCase
  39. {
  40. public static $assert = null;
  41. protected $parameters = null;
  42. protected $runcount = 0;
  43. public function setUp()
  44. {
  45. $this->parameters = new RequestParameters('secret', 'response', 'remoteip', 'version');
  46. }
  47. public function tearDown()
  48. {
  49. self::$assert = null;
  50. }
  51. public function testHTTPContextOptions()
  52. {
  53. $req = new Post();
  54. self::$assert = array($this, 'httpContextOptionsCallback');
  55. $req->submit($this->parameters);
  56. $this->assertEquals(1, $this->runcount, 'The assertion was ran');
  57. }
  58. public function testSSLContextOptions()
  59. {
  60. $req = new Post();
  61. self::$assert = array($this, 'sslContextOptionsCallback');
  62. $req->submit($this->parameters);
  63. $this->assertEquals(1, $this->runcount, 'The assertion was ran');
  64. }
  65. public function testOverrideVerifyUrl()
  66. {
  67. $req = new Post('https://over.ride/some/path');
  68. self::$assert = array($this, 'overrideUrlOptions');
  69. $req->submit($this->parameters);
  70. $this->assertEquals(1, $this->runcount, 'The assertion was ran');
  71. }
  72. public function testConnectionFailureReturnsError()
  73. {
  74. $req = new Post('https://bad.connection/');
  75. self::$assert = array($this, 'connectionFailureResponse');
  76. $response = $req->submit($this->parameters);
  77. $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}', $response);
  78. }
  79. public function connectionFailureResponse()
  80. {
  81. return false;
  82. }
  83. public function overrideUrlOptions(array $args)
  84. {
  85. $this->runcount++;
  86. $this->assertEquals('https://over.ride/some/path', $args[0]);
  87. }
  88. public function httpContextOptionsCallback(array $args)
  89. {
  90. $this->runcount++;
  91. $this->assertCommonOptions($args);
  92. $options = stream_context_get_options($args[2]);
  93. $this->assertArrayHasKey('http', $options);
  94. $this->assertArrayHasKey('method', $options['http']);
  95. $this->assertEquals('POST', $options['http']['method']);
  96. $this->assertArrayHasKey('content', $options['http']);
  97. $this->assertEquals($this->parameters->toQueryString(), $options['http']['content']);
  98. $this->assertArrayHasKey('header', $options['http']);
  99. $headers = array(
  100. 'Content-type: application/x-www-form-urlencoded',
  101. );
  102. foreach ($headers as $header) {
  103. $this->assertContains($header, $options['http']['header']);
  104. }
  105. }
  106. public function sslContextOptionsCallback(array $args)
  107. {
  108. $this->runcount++;
  109. $this->assertCommonOptions($args);
  110. $options = stream_context_get_options($args[2]);
  111. $this->assertArrayHasKey('http', $options);
  112. $this->assertArrayHasKey('verify_peer', $options['http']);
  113. $this->assertTrue($options['http']['verify_peer']);
  114. }
  115. protected function assertCommonOptions(array $args)
  116. {
  117. $this->assertCount(3, $args);
  118. $this->assertStringStartsWith('https://www.google.com/', $args[0]);
  119. $this->assertFalse($args[1]);
  120. $this->assertTrue(is_resource($args[2]), 'The context options should be a resource');
  121. }
  122. }
  123. function file_get_contents()
  124. {
  125. if (PostTest::$assert) {
  126. return call_user_func(PostTest::$assert, func_get_args());
  127. }
  128. // Since we can't represent maxlen in userland...
  129. return call_user_func_array('file_get_contents', func_get_args());
  130. }