Skip to content

Commit cfd263f

Browse files
authored
Limit to Links
* Limit to links * Added test case * int type declaration removed * int type declaration removed * test fix * extra condition removed * typo * modify tests
1 parent 84f06a2 commit cfd263f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Middleware/AddHttp2ServerPush.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ class AddHttp2ServerPush
2525
*
2626
* @return mixed
2727
*/
28-
public function handle(Request $request, Closure $next)
28+
public function handle(Request $request, Closure $next, $limit = null)
2929
{
3030
$response = $next($request);
3131

3232
if ($response->isRedirection() || !$response instanceof Response || $request->isJson()) {
3333
return $response;
3434
}
3535

36-
$this->generateAndAttachLinkHeaders($response);
36+
$this->generateAndAttachLinkHeaders($response, $limit);
3737

3838
return $response;
3939
}
@@ -43,13 +43,15 @@ public function handle(Request $request, Closure $next)
4343
*
4444
* @return $this
4545
*/
46-
protected function generateAndAttachLinkHeaders(Response $response)
46+
protected function generateAndAttachLinkHeaders(Response $response, $limit = null)
4747
{
4848
$headers = $this->fetchLinkableNodes($response)
4949
->flatten(1)
5050
->map(function ($url) {
5151
return $this->buildLinkHeaderString($url);
52-
})->filter()
52+
})
53+
->filter()
54+
->take($limit)
5355
->implode(',');
5456

5557
if (!empty(trim($headers))) {

tests/AddHttp2ServerPushTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ public function it_will_not_return_a_push_header_for_inline_js()
9393
$this->assertFalse($this->isServerPushResponse($response));
9494
}
9595

96+
/** @test */
97+
public function it_will_return_limit_count_of_links()
98+
{
99+
$request = new Request();
100+
$limit = 2;
101+
102+
$response = $this->middleware->handle($request, $this->getNext('pageWithImages'), $limit);
103+
104+
$this->assertCount($limit, explode(",", $response->headers->get('link')));
105+
}
106+
96107
/**
97108
* @param string $pageName
98109
*

0 commit comments

Comments
 (0)