From 8077f36724ddbddcec81666b8b32a05786356710 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 20 Oct 2024 18:29:51 +0200 Subject: [PATCH] Add HTTP Check to MailmanTypeRequest --- tests/RequestFactories/MailmanTypeRequest.php | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/RequestFactories/MailmanTypeRequest.php b/tests/RequestFactories/MailmanTypeRequest.php index 9a96bd82..e8bba0dc 100644 --- a/tests/RequestFactories/MailmanTypeRequest.php +++ b/tests/RequestFactories/MailmanTypeRequest.php @@ -2,6 +2,7 @@ namespace Tests\RequestFactories; +use Illuminate\Support\Facades\Http; use Worksome\RequestFactories\RequestFactory; class MailmanTypeRequest extends RequestFactory @@ -9,10 +10,43 @@ class MailmanTypeRequest extends RequestFactory public function definition(): array { return [ - 'url' => 'https://'.$this->faker->domainName(), + 'url' => 'https://' . $this->faker->domainName(), 'user' => $this->faker->firstName(), 'password' => $this->faker->password(), 'owner' => $this->faker->safeEmail(), ]; } + + public function succeeds($overwrite = []): self + { + return $this->afterCreating(fn ($model) => $this->fakeCheck([...$model, ...$overwrite], true)); + } + + public function fails($overwrite = []): self + { + return $this->afterCreating(fn ($model) => $this->fakeCheck([...$model, ...$overwrite], false)); + } + + protected function fakeCheck($model, bool $check): void + { + Http::fake(function ($request) use ($model, $check) { + if (!$request->hasHeader('Authorization')) { + return; + } + + [$user, $password] = explode(':', base64_decode(str($request->header('Authorization')[0])->replace('Basic ', '')->toString())); + + if ($user !== $model['user'] || $password !== $model['password']) { + return; + } + + if ($request->url() !== $model['url'] . '/system/versions') { + return null; + } + + return $check + ? Http::response(['version' => '2.0.0'], 200) + : Http::response([], 401); + }); + } }