diff --git a/README.md b/README.md index 941eebf..6158176 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,7 @@ CAPTCHAVEL_MODE=auto #### `auto` -The `auto` option leverages the frontend work to you. Just add the `data-recaptcha="true"` attribute to the forms where you want to check for reCAPTCHA. +The `auto` option leverages the frontend work from you. Just add the `data-recaptcha="true"` attribute to the forms where you want to check for reCAPTCHA. ```blade
@@ -237,7 +237,9 @@ The `auto` option leverages the frontend work to you. Just add the `data-recaptc
``` -Captchavel will inject the Google reCAPTCHA v3 as a deferred script in the head before `` tag, in every response (except JSON, AJAX or anything non-HTML), so it can have more analytics about how users interact with your site. +Captchavel will inject the Google reCAPTCHA v3 as a deferred script before `` tag, in every response (except JSON, AJAX or anything non-HTML), so it can have more analytics about how users interact with your site. + +To override the script that gets injected, take a look in the [editing the script view](#editing-the-script-view) section. #### `manual` @@ -248,8 +250,6 @@ Since the frontend won't have nothing injected, this mode it gives you freedom t * manually include the `recaptcha-inject` middleware only in the routes you want, * or include the `recaptcha::script` blade template in your layouts you want. -Take a look in the [editing the script view](#editing-the-script-view) section. - > The manual mode is very handy if your responses have a lot of data and want better performance, because the middleware won't look into the responses. ### Enable on Local Environment @@ -298,7 +298,7 @@ Route::post('{product]/review') The Google reCAPTCHA library underneath allows to make the request to the reCAPTCHA servers using a custom "Request Method". The `request_method` key accepts the Class you want to instance. -The default `null` value is enough for any normal application, but you're free to, for example, create your own logic or use the classes included in the [ReCaptcha package](https://github.com/google/recaptcha/tree/master/src/ReCaptcha/RequestMethod) (that this package requires). You can mimic this next example, were we will use Guzzle. +The default `null` value is enough for any normal application, but you're free to, for example, create your own logic or use the classes included in the [ReCaptcha package](https://github.com/google/recaptcha/tree/master/src/ReCaptcha/RequestMethod) (that this package requires). ```php app->environment('production') - || $this->app->environment('local') && $this->app->make('config')->get('captchavel.enable_local'); + || ($this->app->environment('local') && $this->app->make('config')->get('captchavel.enable_local')); } /** diff --git a/src/Http/Middleware/InjectRecaptchaScript.php b/src/Http/Middleware/InjectRecaptchaScript.php index 9c676c8..3d91ec9 100644 --- a/src/Http/Middleware/InjectRecaptchaScript.php +++ b/src/Http/Middleware/InjectRecaptchaScript.php @@ -27,8 +27,8 @@ class InjectRecaptchaScript /** * InjectRecaptchaScript constructor. * - * @param \Illuminate\Contracts\Config\Repository $config - * @param \Illuminate\Contracts\View\Factory $view + * @param \Illuminate\Contracts\Config\Repository $config + * @param \Illuminate\Contracts\View\Factory $view */ public function __construct(Config $config, View $view) { @@ -39,8 +39,8 @@ public function __construct(Config $config, View $view) /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next * @return mixed * @throws \Throwable * @throws \\Symfony\Component\HttpKernel\Exception\HttpException @@ -65,13 +65,17 @@ public function handle($request, Closure $next) */ protected function isHtml(Request $request, $response) { - return $response instanceof Response && $request->acceptsHtml() && !$request->ajax() && !$request->pjax(); + return $response instanceof Response + && $request->acceptsHtml() + && ! $request->ajax() + && ! $request->pjax() + && ! $response->exception; } /** * Injects the front-end Scripts * - * @param \Illuminate\Http\Response $response + * @param \Illuminate\Http\Response $response * @return \Illuminate\Http\Response */ protected function injectScript(Response $response) @@ -79,7 +83,7 @@ protected function injectScript(Response $response) // To inject the script automatically, we will do it before the ending // head tag. If it's not found, the response may not be valid HTML, // so we will bail out returning the original untouched content. - if (!$endHeadPosition = stripos($content = $response->content(), '')) { + if (! $endHeadPosition = stripos($content = $response->content(), '')) { return $response; }; diff --git a/tests/Middleware/InjectRecaptchaScriptTest.php b/tests/Middleware/InjectRecaptchaScriptTest.php index db4ff8f..951a401 100644 --- a/tests/Middleware/InjectRecaptchaScriptTest.php +++ b/tests/Middleware/InjectRecaptchaScriptTest.php @@ -2,19 +2,7 @@ namespace DarkGhostHunter\Captchavel\Tests; -use DarkGhostHunter\Captchavel\Exceptions\FailedRecaptchaException; -use DarkGhostHunter\Captchavel\Exceptions\InvalidCaptchavelMiddlewareMethod; -use DarkGhostHunter\Captchavel\Exceptions\InvalidRecaptchaException; -use DarkGhostHunter\Captchavel\Http\Middleware\CheckRecaptcha; -use Illuminate\Contracts\Http\Kernel; -use Illuminate\Http\Request; -use Illuminate\Support\Carbon; -use Illuminate\Support\Str; use Orchestra\Testbench\TestCase; -use ReCaptcha\ReCaptcha; -use ReCaptcha\RequestMethod; -use ReCaptcha\RequestParameters; -use ReCaptcha\Response; class InjectRecaptchaScriptTest extends TestCase { @@ -109,4 +97,12 @@ public function testDoesntInjectsOnAjax() ->assertDontSee('api.js?render=test-key&onload=captchavelCallback'); } + public function testDoesntInjectsOnException() + { + $response = $this->get('route-doesnt-exists-will-trigger-exception'); + + $response->assertDontSee('Start Captchavel Script') + ->assertDontSee('api.js?render=test-key&onload=captchavelCallback'); + } + } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 6f27744..eaf341e 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -132,10 +132,26 @@ public function testDoesntRegisterInjectMiddlewareOnNonAuto() ); } + public function testRegisterMiddlewareOnLocalTrue() + { + $this->app['env'] = 'local'; + $this->app['config']->set('captchavel.enable_local', true); + + /** @var CaptchavelServiceProvider $provider */ + $provider = $this->app->make(CaptchavelServiceProvider::class, ['app' => $this->app]); + + $provider->boot(); + + /** @var \Illuminate\Routing\Router $router */ + $router = $this->app->make('router'); + + $this->assertEquals(CheckRecaptcha::class, $router->getMiddleware()['recaptcha']); + } + public function testPublishesConfigFile() { $this->artisan('vendor:publish', [ - '--provider' => 'DarkGhostHunter\Captchavel\CaptchavelServiceProvider' + '--provider' => CaptchavelServiceProvider::class ]); $this->assertFileExists(config_path('captchavel.php'));