Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanDaDeng committed Dec 25, 2018
1 parent b0b1dab commit 5a89d2e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Core/CurlRequestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ public function post($url, $body, $options = [])
http_build_query($body));

$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if (false === $response) {
return '{"success": false, "error-codes": ["'.GoogleReCaptchaV3Response::ERROR_TIMEOUT.'"]}';
return '{"success": false, "error-codes": ["Curl Error Code: ' . GoogleReCaptchaV3Response::ERROR_TIMEOUT . '"]}';
}

if ($httpCode !== 200) {
return '{"success": false, "error-codes": ["Curl Error Code: ' . $httpCode . '"]}';
}

curl_close($curl);

return $response;
Expand Down
5 changes: 4 additions & 1 deletion src/Core/GuzzleRequestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use TimeHunter\LaravelGoogleCaptchaV3\Interfaces\RequestClientInterface;

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

return $response->getBody();
} catch (ClientException $e) {
return '{"success": false, "error-codes": ["'.GoogleReCaptchaV3Response::ERROR_TIMEOUT.'"]}';
return '{"success": false, "error-codes": ["Guzzle Client Error Code: ' . $e->getCode() . '"]}';
} catch (ConnectException $e) {
return '{"success": false, "error-codes": ["Guzzle Client Error Code: ' . GoogleReCaptchaV3Response::ERROR_TIMEOUT . '"]}';
}
}
}
2 changes: 1 addition & 1 deletion src/GoogleReCaptchaV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function prepareViewData($mappers)
public function render($mappers)
{
if (! $this->config->isServiceEnabled()) {
return;
return null;
}
$data = $this->prepareViewData($mappers);

Expand Down
30 changes: 30 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ public function testCurlRequest()
$this->assertEquals(2, count($response->getErrorCodes()));
}

public function testCurlRequest2()
{
$client = new CurlRequestClient();

$response = $client->post('https://www.google.com/recaptcha/api/siteve11rify', [
'secret' => 'test',
'remoteip' => null,
'response' => 'test',
]);

$response = new GoogleReCaptchaV3Response(json_decode($response, 1), null, '');
$this->assertEquals(false, $response->isSuccess());
}


public function testGuzzleRequest()
{
$client = new GuzzleRequestClient();
Expand All @@ -38,4 +53,19 @@ public function testGuzzleRequest()
$this->assertEquals(false, $response->toArray()['success']);
$this->assertEquals(2, count($response->getErrorCodes()));
}


public function testGuzzleRequest2()
{
$client = new GuzzleRequestClient();

$response = $client->post('https://www.google.com/recaptcha/api/sitev111erify', [
'secret' => 'test',
'remoteip' => null,
'response' => 'test',
]);

$response = new GoogleReCaptchaV3Response(json_decode($response, 1), null, '');
$this->assertEquals(false, $response->toArray()['success']);
}
}

0 comments on commit 5a89d2e

Please sign in to comment.