Skip to content

Commit

Permalink
Limit to Links
Browse files Browse the repository at this point in the history
* Limit to links

* Added test case

* int type declaration removed

* int type declaration removed

* test fix

* extra condition removed

* typo

* modify tests
  • Loading branch information
JacobBennett authored Jun 7, 2017
1 parent 84f06a2 commit cfd263f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Middleware/AddHttp2ServerPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class AddHttp2ServerPush
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
public function handle(Request $request, Closure $next, $limit = null)
{
$response = $next($request);

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

$this->generateAndAttachLinkHeaders($response);
$this->generateAndAttachLinkHeaders($response, $limit);

return $response;
}
Expand All @@ -43,13 +43,15 @@ public function handle(Request $request, Closure $next)
*
* @return $this
*/
protected function generateAndAttachLinkHeaders(Response $response)
protected function generateAndAttachLinkHeaders(Response $response, $limit = null)
{
$headers = $this->fetchLinkableNodes($response)
->flatten(1)
->map(function ($url) {
return $this->buildLinkHeaderString($url);
})->filter()
})
->filter()
->take($limit)
->implode(',');

if (!empty(trim($headers))) {
Expand Down
11 changes: 11 additions & 0 deletions tests/AddHttp2ServerPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ public function it_will_not_return_a_push_header_for_inline_js()
$this->assertFalse($this->isServerPushResponse($response));
}

/** @test */
public function it_will_return_limit_count_of_links()
{
$request = new Request();
$limit = 2;

$response = $this->middleware->handle($request, $this->getNext('pageWithImages'), $limit);

$this->assertCount($limit, explode(",", $response->headers->get('link')));
}

/**
* @param string $pageName
*
Expand Down

0 comments on commit cfd263f

Please sign in to comment.