SocketPostTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 SocketPostTest extends TestCase
  39. {
  40. public function testSubmitSuccess()
  41. {
  42. $socket = $this->getMockBuilder(\ReCaptcha\RequestMethod\Socket::class)
  43. ->disableOriginalConstructor()
  44. ->setMethods(array('fsockopen', 'fwrite', 'fgets', 'feof', 'fclose'))
  45. ->getMock();
  46. $socket->expects($this->once())
  47. ->method('fsockopen')
  48. ->willReturn(true);
  49. $socket->expects($this->once())
  50. ->method('fwrite');
  51. $socket->expects($this->once())
  52. ->method('fgets')
  53. ->willReturn("HTTP/1.0 200 OK\n\nRESPONSEBODY");
  54. $socket->expects($this->exactly(2))
  55. ->method('feof')
  56. ->will($this->onConsecutiveCalls(false, true));
  57. $socket->expects($this->once())
  58. ->method('fclose')
  59. ->willReturn(true);
  60. $ps = new SocketPost($socket);
  61. $response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version"));
  62. $this->assertEquals('RESPONSEBODY', $response);
  63. }
  64. public function testOverrideSiteVerifyUrl()
  65. {
  66. $socket = $this->getMockBuilder(\ReCaptcha\RequestMethod\Socket::class)
  67. ->disableOriginalConstructor()
  68. ->setMethods(array('fsockopen', 'fwrite', 'fgets', 'feof', 'fclose'))
  69. ->getMock();
  70. $socket->expects($this->once())
  71. ->method('fsockopen')
  72. ->with('ssl://over.ride', 443, 0, '', 30)
  73. ->willReturn(true);
  74. $socket->expects($this->once())
  75. ->method('fwrite')
  76. ->with($this->matchesRegularExpression('/^POST \/some\/path.*Host: over\.ride/s'));
  77. $socket->expects($this->once())
  78. ->method('fgets')
  79. ->willReturn("HTTP/1.0 200 OK\n\nRESPONSEBODY");
  80. $socket->expects($this->exactly(2))
  81. ->method('feof')
  82. ->will($this->onConsecutiveCalls(false, true));
  83. $socket->expects($this->once())
  84. ->method('fclose')
  85. ->willReturn(true);
  86. $ps = new SocketPost($socket, 'https://over.ride/some/path');
  87. $response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version"));
  88. $this->assertEquals('RESPONSEBODY', $response);
  89. }
  90. public function testSubmitBadResponse()
  91. {
  92. $socket = $this->getMockBuilder(\ReCaptcha\RequestMethod\Socket::class)
  93. ->disableOriginalConstructor()
  94. ->setMethods(array('fsockopen', 'fwrite', 'fgets', 'feof', 'fclose'))
  95. ->getMock();
  96. $socket->expects($this->once())
  97. ->method('fsockopen')
  98. ->willReturn(true);
  99. $socket->expects($this->once())
  100. ->method('fwrite');
  101. $socket->expects($this->once())
  102. ->method('fgets')
  103. ->willReturn("HTTP/1.0 500 NOPEn\\nBOBBINS");
  104. $socket->expects($this->exactly(2))
  105. ->method('feof')
  106. ->will($this->onConsecutiveCalls(false, true));
  107. $socket->expects($this->once())
  108. ->method('fclose')
  109. ->willReturn(true);
  110. $ps = new SocketPost($socket);
  111. $response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version"));
  112. $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}', $response);
  113. }
  114. public function testConnectionFailureReturnsError()
  115. {
  116. $socket = $this->getMockBuilder(\ReCaptcha\RequestMethod\Socket::class)
  117. ->disableOriginalConstructor()
  118. ->setMethods(array('fsockopen'))
  119. ->getMock();
  120. $socket->expects($this->once())
  121. ->method('fsockopen')
  122. ->willReturn(false);
  123. $ps = new SocketPost($socket);
  124. $response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version"));
  125. $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}', $response);
  126. }
  127. }