Skip to content

Commit 75a489a

Browse files
authored
Update cache control (#94)
1 parent cb63f60 commit 75a489a

File tree

9 files changed

+72
-4
lines changed

9 files changed

+72
-4
lines changed

app/Http/Controllers/Api/V1/ListBreweries.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public function __invoke(BreweryFilterRequest $request)
2222
->paginate(perPage: $request->integer('per_page', 50));
2323

2424
return response()->json(
25-
BreweryResource::collection($breweries),
26-
Response::HTTP_OK,
25+
data: BreweryResource::collection($breweries),
26+
status: Response::HTTP_OK,
27+
headers: ['Cache-Control' => 'public; max-age=300; etag'],
2728
);
2829
}
2930
}

app/Http/Controllers/Api/V1/RandomBrewery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function __invoke(Request $request, int $size = 1)
3131
return response()->json(
3232
data: BreweryResource::collection($breweries),
3333
status: Response::HTTP_OK,
34+
// No caching for random events
3435
);
3536
}
3637
}

app/Http/Controllers/Api/V1/SearchBreweries.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function __invoke(Request $request)
2828
return response()->json(
2929
data: BreweryResource::collection($breweries),
3030
status: Response::HTTP_OK,
31+
headers: ['Cache-Control' => 'public; max-age=300; etag'],
3132
);
3233
}
3334
}

run-tests-with-coverage.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
mkdir -p coverage
55

66
# Run Pest with coverage
7-
XDEBUG_MODE=coverage ./vendor/bin/pest --coverage --coverage-html coverage/html --coverage-clover coverage/clover.xml
7+
XDEBUG_MODE=coverage ./vendor/bin/sail artisan test --coverage --coverage-html coverage/html --coverage-clover coverage/clover.xml

tests/Feature/Api/V1/GetBreweries/BasicTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@
4848
test('returns cache control headers', function () {
4949
createBreweries(1);
5050
$response = $this->getJson('/v1/breweries');
51-
$response->assertOk()->assertHeader('Cache-Control', 'max-age=300, public');
51+
$response->assertOk();
52+
53+
// Check that the Cache-Control header contains the expected values
54+
$cacheControl = $response->headers->get('Cache-Control');
55+
expect($cacheControl)->toContain('public');
56+
expect($cacheControl)->toContain('max-age=');
57+
expect($cacheControl)->toContain('etag');
5258
});
5359

5460
test('returns HTTP error 422 with invalid params', function () {

tests/Feature/Api/V1/GetBreweriesMetaTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,17 @@
268268
$response->assertStatus(422)
269269
->assertJsonValidationErrors(['page', 'per_page']);
270270
});
271+
272+
test('meta endpoint returns cache control headers', function () {
273+
createBreweries(3);
274+
275+
$response = $this->getJson('/v1/breweries/meta');
276+
277+
$response->assertOk();
278+
279+
// Check that the Cache-Control header contains the expected values
280+
$cacheControl = $response->headers->get('Cache-Control');
281+
expect($cacheControl)->toContain('public');
282+
expect($cacheControl)->toContain('max-age=');
283+
expect($cacheControl)->toContain('etag');
284+
});

tests/Feature/Api/V1/GetBreweryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,17 @@
9191
$response = $this->getJson('/v1/breweries/invalid-id');
9292
$response->assertNotFound();
9393
});
94+
95+
test('brewery endpoint returns cache control headers', function () {
96+
$brewery = createBrewery();
97+
98+
$response = $this->getJson("/v1/breweries/{$brewery->id}");
99+
100+
$response->assertOk();
101+
102+
// Check that the Cache-Control header contains the expected values
103+
$cacheControl = $response->headers->get('Cache-Control');
104+
expect($cacheControl)->toContain('public');
105+
expect($cacheControl)->toContain('max-age=');
106+
expect($cacheControl)->toContain('etag');
107+
});

tests/Feature/Api/V1/RandomBreweryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,20 @@
9999
'street',
100100
]]);
101101
});
102+
103+
test('random endpoint does not have cache control headers', function () {
104+
createBreweries(3);
105+
106+
$response = $this->getJson('/v1/breweries/random');
107+
108+
$response->assertOk();
109+
110+
// Check that the Cache-Control header doesn't contain caching directives
111+
$cacheControl = $response->headers->get('Cache-Control');
112+
113+
// If Cache-Control header exists, it shouldn't contain public or max-age directives
114+
if ($cacheControl) {
115+
expect($cacheControl)->not->toContain('public; max-age=');
116+
expect($cacheControl)->not->toContain('etag');
117+
}
118+
});

tests/Feature/Api/V1/SearchBreweriesTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,17 @@
140140
$response->assertOk()
141141
->assertJsonCount(3);
142142
});
143+
144+
test('search endpoint returns cache control headers', function () {
145+
createBrewery(['name' => 'Test Brewery']);
146+
147+
$response = $this->getJson('/v1/breweries/search?query=Test');
148+
149+
$response->assertOk();
150+
151+
// Check that the Cache-Control header contains the expected values
152+
$cacheControl = $response->headers->get('Cache-Control');
153+
expect($cacheControl)->toContain('public');
154+
expect($cacheControl)->toContain('max-age=');
155+
expect($cacheControl)->toContain('etag');
156+
});

0 commit comments

Comments
 (0)