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
1 change: 1 addition & 0 deletions src/ReCaptcha/RequestMethod/CurlPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function submit(RequestParameters $params): string
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => 60,
];
curl_setopt_array($handle, $options);

Expand Down
3 changes: 1 addition & 2 deletions src/ReCaptcha/RequestMethod/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public function submit(RequestParameters $params): string
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $params->toQueryString(),
// Force the peer to validate (not needed in 5.6.0+, but still works)
'verify_peer' => true,
'timeout' => 60,
],
];
$context = stream_context_create($options);
Expand Down
4 changes: 4 additions & 0 deletions src/ReCaptcha/RequestMethod/SocketPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function submit(RequestParameters $params): string
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
}

if (false === stream_set_timeout($handle, 60)) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
}

$content = $params->toQueryString();

$request = 'POST '.$urlParsed['path']." HTTP/1.0\r\n";
Expand Down
29 changes: 2 additions & 27 deletions tests/ReCaptcha/RequestMethod/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ public function testHTTPContextOptions(): void
$this->assertEquals(1, $this->runcount, 'The assertion was ran');
}

public function testSSLContextOptions(): void
{
$req = new Post();
self::$assert = [$this, 'sslContextOptionsCallback'];
$req->submit($this->parameters);
$this->assertEquals(1, $this->runcount, 'The assertion was ran');
}

public function testOverrideVerifyUrl(): void
{
$req = new Post('https://over.ride/some/path');
Expand Down Expand Up @@ -138,26 +130,9 @@ public function httpContextOptionsCallback(array $args): void
/** @var string $header */
$header = $httpOptions['header'];
$this->assertStringContainsStringIgnoringCase('Content-type: application/x-www-form-urlencoded', $header);
}

/**
* @param array<int, mixed> $args
*/
public function sslContextOptionsCallback(array $args): void
{
++$this->runcount;
$this->assertCommonOptions($args);

/** @var resource $context */
$context = $args[2];
$options = stream_context_get_options($context);
$this->assertArrayHasKey('http', $options);

/** @var array<string, mixed> $httpOptions */
$httpOptions = $options['http'];

$this->assertArrayHasKey('verify_peer', $httpOptions);
$this->assertTrue($httpOptions['verify_peer']);
$this->assertArrayHasKey('timeout', $httpOptions);
$this->assertEquals(60, $httpOptions['timeout']);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/ReCaptcha/RequestMethod/SocketPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class SocketPostGlobalState
public static array $fgetsResponses = [];
public static int $feofCount = 0;
public static bool $fcloseCalled = false;
public static bool $streamSetTimeoutSuccess = true;
}

/**
Expand Down Expand Up @@ -100,6 +101,14 @@ function feof(\stdClass $handle): bool
return empty(SocketPostGlobalState::$fgetsResponses);
}

/**
* Mock stream_set_timeout in the ReCaptcha\RequestMethod namespace.
*/
function stream_set_timeout(\stdClass $handle, int $seconds, int $microseconds = 0): bool
{
return SocketPostGlobalState::$streamSetTimeoutSuccess;
}

/**
* Mock fclose in the ReCaptcha\RequestMethod namespace.
*/
Expand All @@ -126,6 +135,7 @@ protected function setUp(): void
SocketPostGlobalState::$fwriteData = '';
SocketPostGlobalState::$fgetsResponses = [];
SocketPostGlobalState::$fcloseCalled = false;
SocketPostGlobalState::$streamSetTimeoutSuccess = true;
}

public function testSubmit(): void
Expand All @@ -147,6 +157,15 @@ public function testSubmit(): void
$this->assertTrue(SocketPostGlobalState::$fcloseCalled);
}

public function testStreamTimeoutFailureReturnsError(): void
{
SocketPostGlobalState::$streamSetTimeoutSuccess = false;
$sp = new SocketPost();
$response = $sp->submit(new RequestParameters('secret', 'response'));

$this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}', $response);
}

public function testUrlFailureReturnsError(): void
{
$sp = new SocketPost('invalid_url');
Expand Down
Loading