diff --git a/src/ReCaptcha/RequestMethod/SocketPost.php b/src/ReCaptcha/RequestMethod/SocketPost.php index c45d744..955a49d 100644 --- a/src/ReCaptcha/RequestMethod/SocketPost.php +++ b/src/ReCaptcha/RequestMethod/SocketPost.php @@ -107,7 +107,7 @@ public function submit(RequestParameters $params): string $response = ''; } - if (0 !== strpos($response, 'HTTP/1.0 200 OK')) { + if (1 !== preg_match('#^HTTP/1\.[01] 200 OK#', $response)) { return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}'; } diff --git a/tests/ReCaptcha/RequestMethod/SocketPostTest.php b/tests/ReCaptcha/RequestMethod/SocketPostTest.php index ef45a78..ec6193e 100644 --- a/tests/ReCaptcha/RequestMethod/SocketPostTest.php +++ b/tests/ReCaptcha/RequestMethod/SocketPostTest.php @@ -182,6 +182,22 @@ public function testOverrideSiteVerifyUrl(): void $this->assertTrue(SocketPostGlobalState::$fcloseCalled); } + public function testSubmitReturnsResponseWhenHttp11(): void + { + SocketPostGlobalState::$fgetsResponses = [ + "HTTP/1.1 200 OK\r\n", + "Content-Type: application/json\r\n", + "\r\n", + 'RESPONSEBODY', + ]; + + $sp = new SocketPost(); + $response = $sp->submit(new RequestParameters('secret', 'response')); + + $this->assertEquals('RESPONSEBODY', $response); + $this->assertTrue(SocketPostGlobalState::$fcloseCalled); + } + public function testStreamTimeoutFailureReturnsError(): void { SocketPostGlobalState::$streamSetTimeoutSuccess = false; @@ -272,4 +288,33 @@ public function testStreamGetContentsReturnsFalse(): void $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}', $response); } + + public function testBadResponseReturnsErrorWhenHttp11(): void + { + SocketPostGlobalState::$fgetsResponses = [ + "HTTP/1.1 500 Internal Server Error\r\n", + "\r\n", + 'FAIL', + ]; + + $sp = new SocketPost(); + $response = $sp->submit(new RequestParameters('secret', 'response')); + + $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}', $response); + } + + public function testBadResponseReturnsErrorWhenHttp2(): void + { + SocketPostGlobalState::$fgetsResponses = [ + "HTTP/2.0 200 OK\r\n", + "Content-Type: application/json\r\n", + "\r\n", + 'RESPONSEBODY', + ]; + + $sp = new SocketPost(); + $response = $sp->submit(new RequestParameters('secret', 'response')); + + $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}', $response); + } }