-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SymfonyHttpClient new ReCaptcha request method
- Loading branch information
Showing
9 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Karser\Recaptcha3Bundle\RequestMethod; | ||
|
||
use ReCaptcha\ReCaptcha; | ||
use ReCaptcha\RequestMethod; | ||
use ReCaptcha\RequestParameters; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final class SymfonyHttpClient implements RequestMethod | ||
{ | ||
/** | ||
* @var HttpClientInterface | ||
*/ | ||
private $httpClient; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $siteVerifyUrl; | ||
|
||
public function __construct(HttpClientInterface $httpClient, ?string $siteVerifyUrl = null) | ||
{ | ||
$this->httpClient = $httpClient; | ||
$this->siteVerifyUrl = $siteVerifyUrl ?? ReCaptcha::SITE_VERIFY_URL; | ||
} | ||
|
||
public function submit(RequestParameters $params): string | ||
{ | ||
$response = $this->httpClient->request('POST', $this->siteVerifyUrl, [ | ||
'body' => $params->toArray(), | ||
]); | ||
|
||
try { | ||
$statusCode = $response->getStatusCode(); | ||
} catch (TransportExceptionInterface $e) { | ||
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; | ||
} | ||
|
||
if ($statusCode !== 200) { | ||
return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}'; | ||
} | ||
|
||
return $response->getContent(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RequestMethod; | ||
|
||
use Karser\Recaptcha3Bundle\RequestMethod\SymfonyHttpClient; | ||
use PHPUnit\Framework\TestCase; | ||
use ReCaptcha\RequestParameters; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
|
||
final class SymfonyHttpClientTest extends TestCase | ||
{ | ||
public function testSubmit(): void | ||
{ | ||
$httpClient = new MockHttpClient(function (string $method, string $url, array $options) { | ||
self::assertSame('POST', $method); | ||
self::assertSame('https://www.google.com/recaptcha/api/siteverify', $url); | ||
self::assertSame('secret=secret&response=response', $options['body']); | ||
|
||
return new MockResponse('RESPONSEBODY'); | ||
}); | ||
|
||
$method = new SymfonyHttpClient($httpClient); | ||
$response = $method->submit(new RequestParameters('secret', 'response')); | ||
|
||
self::assertSame('RESPONSEBODY', $response); | ||
} | ||
|
||
public function testOverrideSiteVerifyUrl() | ||
{ | ||
$httpClient = new MockHttpClient(function (string $method, string $url, array $options) { | ||
self::assertSame('POST', $method); | ||
self::assertSame('http://override/', $url); | ||
self::assertSame('secret=secret&response=response', $options['body']); | ||
|
||
return new MockResponse('RESPONSEBODY'); | ||
}); | ||
|
||
$method = new SymfonyHttpClient($httpClient, 'http://override/'); | ||
$response = $method->submit(new RequestParameters('secret', 'response')); | ||
|
||
self::assertSame('RESPONSEBODY', $response); | ||
} | ||
|
||
public function testResponseError() | ||
{ | ||
$httpClient = new MockHttpClient(function (string $method, string $url, array $options) { | ||
self::assertSame('POST', $method); | ||
self::assertSame('https://www.google.com/recaptcha/api/siteverify', $url); | ||
self::assertSame('secret=secret&response=response', $options['body']); | ||
|
||
return new MockResponse('fail', ['http_code' => 400]); | ||
}); | ||
|
||
$method = new SymfonyHttpClient($httpClient); | ||
$response = $method->submit(new RequestParameters('secret', 'response')); | ||
|
||
self::assertSame('{"success": false, "error-codes": ["bad-response"]}', $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Karser\Recaptcha3Bundle\Tests\fixtures; | ||
|
||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class MockHttpClientCallback | ||
{ | ||
public function __invoke(string $method, string $url, array $options = []): ResponseInterface | ||
{ | ||
return new MockResponse('{"success": true, "score": 1}'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
framework: | ||
secret: ThisIsNotReallyASecretSoPleaseChangeIt | ||
test: true | ||
form: ~ | ||
http_client: | ||
mock_response_factory: Karser\Recaptcha3Bundle\Tests\fixtures\MockHttpClientCallback | ||
|
||
services: | ||
form.factory.public: | ||
alias: form.factory | ||
public: true | ||
twig.public: | ||
alias: twig | ||
public: true | ||
karser_recaptcha3.google.recaptcha.public: | ||
alias: karser_recaptcha3.google.recaptcha | ||
public: true | ||
Karser\Recaptcha3Bundle\Tests\fixtures\MockHttpClientCallback: ~ | ||
|
||
karser_recaptcha3: | ||
site_key: 'key' | ||
secret_key: 'secret' | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters