Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ReCaptcha/RequestMethod/SocketPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'"]}';
}

Expand Down
45 changes: 45 additions & 0 deletions tests/ReCaptcha/RequestMethod/SocketPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading