Skip to content

Commit

Permalink
Merge pull request #12 from chrisrhymes/feature/custom-user-agent
Browse files Browse the repository at this point in the history
Ability to set custom user agent
  • Loading branch information
chrisrhymes committed Aug 18, 2023
2 parents 8f6c11d + 507c438 commit 93c224f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A package that will check for broken links in the HTML of a specified model's fi
- [Getting Started](#getting-started)
- [Usage](#usage)
- [Rate Limiting](#rate-limiting)
- [User Agent](#user-agent)
- [Verify SSL](#verify-ssl)
- [Tests](#tests)

Expand Down Expand Up @@ -103,6 +104,12 @@ The configuration file allows you to set the `rate_limit` to set how many reques

The configuration file also allows you to set the `retry_until` so the job will be retried until the time limit (in munites) is reached.

## User Agent

To set a custom user agent for requests sent by the link checker, set the `user_agent` in the configuration file. For example `'user_agent' => 'my-user-agent',`

The default value is `link-checker`.

## Verify SSL

To disable verifying the SSL certificate of the link you are checking, [publish the package configuration](#publish-the-config-optional) and then set `'verify' => false,`.
Expand Down
5 changes: 5 additions & 0 deletions config/link-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
'retry_until' => 10,

/**
* Set a custom user agent
*/
'user_agent' => 'link-checker',

/**
* Describes the SSL certificate verification behavior of a request
*/
Expand Down
7 changes: 4 additions & 3 deletions src/Jobs/CheckLinkFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ public function handle()
}

try {
$response = Http::withOptions([
'verify' => config('link-checker.verify', true),
])
$response = Http::withUserAgent(config('link-checker.user_agent', 'link-checker'))
->withOptions([
'verify' => config('link-checker.verify', true),
])
->timeout(config('link-checker.timeout', 10))
->get($this->link->url);

Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/CustomUserAgentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function () {
Http::fake();

$this->post = Post::factory()
->create([
'content' => ' <a href="https://this-is-broken.com">Broken link</a>',
]);
});

it('sets a default user agent for requests', function () {
CheckModelForBrokenLinks::dispatch($this->post, ['content']);

Http::assertSent(function (Request $request) {
return $request->hasHeader('User-Agent', 'link-checker');
});
});

it('sets a custom user agent for requests', function () {
Config::set('link-checker.user_agent', 'custom-user-agent');

CheckModelForBrokenLinks::dispatch($this->post, ['content']);

Http::assertSent(function (Request $request) {
return $request->hasHeader('User-Agent', 'custom-user-agent');
});
});

0 comments on commit 93c224f

Please sign in to comment.