Skip to content

Commit ca427ee

Browse files
fix: send headers with requests (#2)
1 parent adf08d7 commit ca427ee

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/QueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function whereIn(string $property, array $values): static
7878

7979
public function find(mixed $id): Model|null
8080
{
81-
$response = Http::get($this->model->baseUrl().$this->toQuery($id))->throwUnlessStatus(404);
81+
$response = Http::withHeaders($this->model->headers())->get($this->model->baseUrl().$this->toQuery($id))->throwUnlessStatus(404);
8282

8383
if ($response->failed()) {
8484
return null;
@@ -183,7 +183,7 @@ public function toQuery(string|int|null $id = null): string
183183

184184
public function performCollectionQuery(): Response
185185
{
186-
return Http::get($this->model->baseUrl().$this->toQuery());
186+
return Http::withHeaders($this->model->headers())->get($this->model->baseUrl().$this->toQuery());
187187
}
188188

189189
/**

tests/FindTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Illuminate\Http\Client\Request;
6+
use Illuminate\Support\Arr;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\Http;
89
use Intermax\EloquentJsonApiClient\Tests\Team;
@@ -11,8 +12,7 @@
1112
uses(TestCase::class);
1213

1314
it('fetches and hydrates a team', function () {
14-
Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindResponse.json'))
15-
);
15+
Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindResponse.json')));
1616

1717
$team = Team::find(1);
1818

@@ -24,17 +24,27 @@
2424
});
2525

2626
it('fetches and hydrates a team with its relations', function () {
27-
Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindRelationResponse.json'))
28-
);
27+
Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindRelationResponse.json')));
2928

3029
$team = Team::query()->with(['members'])->find(1);
3130

3231
assert($team instanceof Team);
3332

34-
Http::assertSent(fn (Request $request) => str_contains($request->url(), 'teams/1') && str_contains($request->url(), 'include=members')
33+
Http::assertSent(
34+
fn (Request $request) => str_contains($request->url(), 'teams/1') && str_contains($request->url(), 'include=members')
3535
);
3636

3737
expect($team)->toBeInstanceOf(Team::class)
3838
->and($team->members)->toBeInstanceOf(Collection::class)
3939
->and($team->members->first()->id)->toBe('5');
4040
});
41+
42+
it('sends headers with the request', function () {
43+
Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindResponse.json')));
44+
45+
Team::find(1);
46+
47+
Http::assertSent(
48+
fn (Request $request) => Arr::first($request->header('api-key')) === 'test'
49+
);
50+
});

tests/GetTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Illuminate\Http\Client\Request;
6+
use Illuminate\Support\Arr;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\Http;
89
use Intermax\EloquentJsonApiClient\Tests\Team;
@@ -11,8 +12,7 @@
1112
uses(TestCase::class);
1213

1314
it('fetches teams', function () {
14-
Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamGetResponse.json'))
15-
);
15+
Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamGetResponse.json')));
1616

1717
$teams = Team::query()->get();
1818

@@ -22,3 +22,13 @@
2222
->and($teams->count())->toBe(4)
2323
->and($teams->first())->toBeInstanceOf(Team::class);
2424
});
25+
26+
it('sends headers with the request', function () {
27+
Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamGetResponse.json')));
28+
29+
Team::query()->get();
30+
31+
Http::assertSent(
32+
fn (Request $request) => Arr::first($request->header('api-key')) === 'test'
33+
);
34+
});

tests/Team.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ public function baseUrl(): string
3838
{
3939
return 'https://localhost';
4040
}
41+
42+
public function headers(): array
43+
{
44+
return ['api-key' => 'test'];
45+
}
4146
}

0 commit comments

Comments
 (0)