Add HTTP Check to MailmanTypeRequest

This commit is contained in:
philipp lang 2024-10-20 18:29:51 +02:00
parent 2871a90847
commit 85c0727b72
1 changed files with 35 additions and 1 deletions

View File

@ -2,6 +2,7 @@
namespace Tests\RequestFactories; namespace Tests\RequestFactories;
use Illuminate\Support\Facades\Http;
use Worksome\RequestFactories\RequestFactory; use Worksome\RequestFactories\RequestFactory;
class MailmanTypeRequest extends RequestFactory class MailmanTypeRequest extends RequestFactory
@ -15,4 +16,37 @@ class MailmanTypeRequest extends RequestFactory
'owner' => $this->faker->safeEmail(), '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);
});
}
} }