Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/Http/Controllers/Api/V1/ListBreweries.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public function __invoke(BreweryFilterRequest $request)
->paginate(perPage: $request->integer('per_page', 50));

return response()->json(
BreweryResource::collection($breweries),
Response::HTTP_OK,
data: BreweryResource::collection($breweries),
status: Response::HTTP_OK,
headers: ['Cache-Control' => 'public; max-age=300; etag'],
);
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/V1/RandomBrewery.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __invoke(Request $request, int $size = 1)
return response()->json(
data: BreweryResource::collection($breweries),
status: Response::HTTP_OK,
// No caching for random events
);
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/V1/SearchBreweries.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __invoke(Request $request)
return response()->json(
data: BreweryResource::collection($breweries),
status: Response::HTTP_OK,
headers: ['Cache-Control' => 'public; max-age=300; etag'],
);
}
}
2 changes: 1 addition & 1 deletion run-tests-with-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
mkdir -p coverage

# Run Pest with coverage
XDEBUG_MODE=coverage ./vendor/bin/pest --coverage --coverage-html coverage/html --coverage-clover coverage/clover.xml
XDEBUG_MODE=coverage ./vendor/bin/sail artisan test --coverage --coverage-html coverage/html --coverage-clover coverage/clover.xml
8 changes: 7 additions & 1 deletion tests/Feature/Api/V1/GetBreweries/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@
test('returns cache control headers', function () {
createBreweries(1);
$response = $this->getJson('/v1/breweries');
$response->assertOk()->assertHeader('Cache-Control', 'max-age=300, public');
$response->assertOk();

// Check that the Cache-Control header contains the expected values
$cacheControl = $response->headers->get('Cache-Control');
expect($cacheControl)->toContain('public');
expect($cacheControl)->toContain('max-age=');
expect($cacheControl)->toContain('etag');
});

test('returns HTTP error 422 with invalid params', function () {
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/Api/V1/GetBreweriesMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,17 @@
$response->assertStatus(422)
->assertJsonValidationErrors(['page', 'per_page']);
});

test('meta endpoint returns cache control headers', function () {
createBreweries(3);

$response = $this->getJson('/v1/breweries/meta');

$response->assertOk();

// Check that the Cache-Control header contains the expected values
$cacheControl = $response->headers->get('Cache-Control');
expect($cacheControl)->toContain('public');
expect($cacheControl)->toContain('max-age=');
expect($cacheControl)->toContain('etag');
});
14 changes: 14 additions & 0 deletions tests/Feature/Api/V1/GetBreweryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,17 @@
$response = $this->getJson('/v1/breweries/invalid-id');
$response->assertNotFound();
});

test('brewery endpoint returns cache control headers', function () {
$brewery = createBrewery();

$response = $this->getJson("/v1/breweries/{$brewery->id}");

$response->assertOk();

// Check that the Cache-Control header contains the expected values
$cacheControl = $response->headers->get('Cache-Control');
expect($cacheControl)->toContain('public');
expect($cacheControl)->toContain('max-age=');
expect($cacheControl)->toContain('etag');
});
17 changes: 17 additions & 0 deletions tests/Feature/Api/V1/RandomBreweryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,20 @@
'street',
]]);
});

test('random endpoint does not have cache control headers', function () {
createBreweries(3);

$response = $this->getJson('/v1/breweries/random');

$response->assertOk();

// Check that the Cache-Control header doesn't contain caching directives
$cacheControl = $response->headers->get('Cache-Control');

// If Cache-Control header exists, it shouldn't contain public or max-age directives
if ($cacheControl) {
expect($cacheControl)->not->toContain('public; max-age=');
expect($cacheControl)->not->toContain('etag');
}
});
14 changes: 14 additions & 0 deletions tests/Feature/Api/V1/SearchBreweriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,17 @@
$response->assertOk()
->assertJsonCount(3);
});

test('search endpoint returns cache control headers', function () {
createBrewery(['name' => 'Test Brewery']);

$response = $this->getJson('/v1/breweries/search?query=Test');

$response->assertOk();

// Check that the Cache-Control header contains the expected values
$cacheControl = $response->headers->get('Cache-Control');
expect($cacheControl)->toContain('public');
expect($cacheControl)->toContain('max-age=');
expect($cacheControl)->toContain('etag');
});
Loading