Skip to content

Commit 5a89d2e

Browse files
committed
added more tests
1 parent b0b1dab commit 5a89d2e

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/Core/CurlRequestClient.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ public function post($url, $body, $options = [])
2222
http_build_query($body));
2323

2424
$response = curl_exec($curl);
25+
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
2526

2627
if (false === $response) {
27-
return '{"success": false, "error-codes": ["'.GoogleReCaptchaV3Response::ERROR_TIMEOUT.'"]}';
28+
return '{"success": false, "error-codes": ["Curl Error Code: ' . GoogleReCaptchaV3Response::ERROR_TIMEOUT . '"]}';
2829
}
30+
31+
if ($httpCode !== 200) {
32+
return '{"success": false, "error-codes": ["Curl Error Code: ' . $httpCode . '"]}';
33+
}
34+
2935
curl_close($curl);
3036

3137
return $response;

src/Core/GuzzleRequestClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use GuzzleHttp\Client;
1212
use GuzzleHttp\Exception\ClientException;
13+
use GuzzleHttp\Exception\ConnectException;
1314
use TimeHunter\LaravelGoogleCaptchaV3\Interfaces\RequestClientInterface;
1415

1516
class GuzzleRequestClient implements RequestClientInterface
@@ -24,7 +25,9 @@ public function post($url, $body, $options = [])
2425

2526
return $response->getBody();
2627
} catch (ClientException $e) {
27-
return '{"success": false, "error-codes": ["'.GoogleReCaptchaV3Response::ERROR_TIMEOUT.'"]}';
28+
return '{"success": false, "error-codes": ["Guzzle Client Error Code: ' . $e->getCode() . '"]}';
29+
} catch (ConnectException $e) {
30+
return '{"success": false, "error-codes": ["Guzzle Client Error Code: ' . GoogleReCaptchaV3Response::ERROR_TIMEOUT . '"]}';
2831
}
2932
}
3033
}

src/GoogleReCaptchaV3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function prepareViewData($mappers)
5353
public function render($mappers)
5454
{
5555
if (! $this->config->isServiceEnabled()) {
56-
return;
56+
return null;
5757
}
5858
$data = $this->prepareViewData($mappers);
5959

tests/RequestTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ public function testCurlRequest()
2424
$this->assertEquals(2, count($response->getErrorCodes()));
2525
}
2626

27+
public function testCurlRequest2()
28+
{
29+
$client = new CurlRequestClient();
30+
31+
$response = $client->post('https://www.google.com/recaptcha/api/siteve11rify', [
32+
'secret' => 'test',
33+
'remoteip' => null,
34+
'response' => 'test',
35+
]);
36+
37+
$response = new GoogleReCaptchaV3Response(json_decode($response, 1), null, '');
38+
$this->assertEquals(false, $response->isSuccess());
39+
}
40+
41+
2742
public function testGuzzleRequest()
2843
{
2944
$client = new GuzzleRequestClient();
@@ -38,4 +53,19 @@ public function testGuzzleRequest()
3853
$this->assertEquals(false, $response->toArray()['success']);
3954
$this->assertEquals(2, count($response->getErrorCodes()));
4055
}
56+
57+
58+
public function testGuzzleRequest2()
59+
{
60+
$client = new GuzzleRequestClient();
61+
62+
$response = $client->post('https://www.google.com/recaptcha/api/sitev111erify', [
63+
'secret' => 'test',
64+
'remoteip' => null,
65+
'response' => 'test',
66+
]);
67+
68+
$response = new GoogleReCaptchaV3Response(json_decode($response, 1), null, '');
69+
$this->assertEquals(false, $response->toArray()['success']);
70+
}
4171
}

0 commit comments

Comments
 (0)