Migrate outbound HTTP calls from Guzzle to the Http facade
Replaces every raw GuzzleHttp\Client usage (Nominatim, web.archive.org, webmention fetch/discover/send, Bridgy syndication, IndieAuth client_id lookup, contact avatar/h-card fetch, profile image download, and the CloudConvert screenshot pipeline) with Illuminate\Support\Facades\Http, and enables Http::preventStrayRequests() globally in tests so any un-faked outbound call now fails loudly instead of silently hitting the network. The CloudConvert retry-until-finished middleware in AppServiceProvider is replaced by a plain polling loop in SaveScreenshot, which also fixes a latent bug where the old middleware decoded a response object instead of its body. Bridgy syndication jobs keep their tries=1/no-retry semantics unchanged to avoid duplicate publishes. Guzzle's PSR-7 helpers (Header, UriResolver, Utils) stay in SendWebMentions since Http has no equivalent for them. Every affected test file's Guzzle MockHandler/HandlerStack boilerplate is replaced with Http::fake()/Http::sequence(). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P8j7cJhCiYDsGUgB7obKNQ
This commit is contained in:
parent
c420e19f08
commit
e08763c526
26 changed files with 337 additions and 630 deletions
|
|
@ -5,12 +5,9 @@ namespace Tests\Unit\Jobs;
|
|||
use App\Jobs\SyndicateNoteToMastodon;
|
||||
use App\Models\Note;
|
||||
use Faker\Factory;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Middleware;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Client\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
|
|
@ -24,19 +21,17 @@ class SyndicateNoteToMastodonJobTest extends TestCase
|
|||
$faker = Factory::create();
|
||||
$randomNumber = $faker->randomNumber();
|
||||
$mastodonUrl = 'https://mastodon.example/@jonny/'.$randomNumber;
|
||||
$mock = new MockHandler([
|
||||
new Response(201, ['Content-Type' => 'application/json'], json_encode([
|
||||
Http::fake([
|
||||
'brid.gy/*' => Http::response([
|
||||
'url' => $mastodonUrl,
|
||||
'id' => (string) $randomNumber,
|
||||
'type' => ['h-entry'],
|
||||
])),
|
||||
], 201),
|
||||
]);
|
||||
$handler = HandlerStack::create($mock);
|
||||
$client = new Client(['handler' => $handler]);
|
||||
|
||||
$note = Note::factory()->create();
|
||||
$job = new SyndicateNoteToMastodon($note);
|
||||
$job->handle($client);
|
||||
$job->handle();
|
||||
|
||||
$this->assertDatabaseHas('notes', [
|
||||
'mastodon_url' => $mastodonUrl,
|
||||
|
|
@ -46,39 +41,31 @@ class SyndicateNoteToMastodonJobTest extends TestCase
|
|||
#[Test]
|
||||
public function we_post_the_correct_source_and_target(): void
|
||||
{
|
||||
$container = [];
|
||||
$history = Middleware::history($container);
|
||||
$mock = new MockHandler([
|
||||
new Response(201, ['Content-Type' => 'application/json'], json_encode([
|
||||
Http::fake([
|
||||
'brid.gy/*' => Http::response([
|
||||
'url' => 'https://mastodon.example/@jonny/1',
|
||||
])),
|
||||
], 201),
|
||||
]);
|
||||
$handler = HandlerStack::create($mock);
|
||||
$handler->push($history);
|
||||
$client = new Client(['handler' => $handler]);
|
||||
|
||||
$note = Note::factory()->create(['note' => 'This is a **test**']);
|
||||
$job = new SyndicateNoteToMastodon($note);
|
||||
$job->handle($client);
|
||||
$job->handle();
|
||||
|
||||
$request = $container[0]['request'];
|
||||
$body = [];
|
||||
parse_str((string) $request->getBody(), $body);
|
||||
|
||||
$this->assertSame('https://brid.gy/publish/webmention', (string) $request->getUri());
|
||||
$this->assertSame($note->uri, $body['source']);
|
||||
$this->assertSame('https://brid.gy/publish/mastodon', $body['target']);
|
||||
Http::assertSent(function (Request $request) use ($note) {
|
||||
return $request->url() === 'https://brid.gy/publish/webmention'
|
||||
&& $request['source'] === $note->uri
|
||||
&& $request['target'] === 'https://brid.gy/publish/mastodon';
|
||||
});
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function a_bridgy_failure_throws_and_does_not_set_mastodon_url(): void
|
||||
{
|
||||
$mock = new MockHandler([
|
||||
new Response(400, ['Content-Type' => 'application/json'], json_encode([
|
||||
Http::fake([
|
||||
'brid.gy/*' => Http::response([
|
||||
'error' => 'Could not find target link',
|
||||
])),
|
||||
], 400),
|
||||
]);
|
||||
$client = new Client(['handler' => HandlerStack::create($mock)]);
|
||||
|
||||
$note = Note::factory()->create();
|
||||
$job = new SyndicateNoteToMastodon($note);
|
||||
|
|
@ -86,7 +73,7 @@ class SyndicateNoteToMastodonJobTest extends TestCase
|
|||
$this->expectException(\RuntimeException::class);
|
||||
|
||||
try {
|
||||
$job->handle($client);
|
||||
$job->handle();
|
||||
} finally {
|
||||
$this->assertDatabaseHas('notes', [
|
||||
'id' => $note->id,
|
||||
|
|
|
|||
Loading…
Reference in a new issue