Skip to content

Commit

Permalink
Append to Link header if it is already present (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
juukie authored and JacobBennett committed Oct 24, 2017
1 parent 89896d9 commit 75d9fd6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/Middleware/AddHttp2ServerPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class AddHttp2ServerPush
{

/**
* The DomCrawler instance.
*
Expand Down Expand Up @@ -116,7 +115,6 @@ private function buildLinkHeaderString($url)
});

return is_null($type) ? null : "<{$url}>; rel=preload; as={$type}";

}

/**
Expand All @@ -128,7 +126,10 @@ private function buildLinkHeaderString($url)
*/
private function addLinkHeader(Response $response, $link)
{
if ($response->headers->get('Link')) {
$link = $response->headers->get('Link') . ',' . $link;
}

$response->header('Link', $link);
}

}
19 changes: 18 additions & 1 deletion tests/AddHttp2ServerPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ public function it_will_return_limit_count_of_links()
$this->assertCount($limit, explode(",", $response->headers->get('link')));
}

/** @test */
public function it_will_append_to_header_if_already_present()
{
$request = new Request();

$next = $this->getNext('pageWithCss');

$response = $this->middleware->handle($request, function ($request) use ($next) {
$response = $next($request);
$response->headers->set('Link', '<https://example.com/en>; rel="alternate"; hreflang="en"');
return $response;
});

$this->assertTrue($this->isServerPushResponse($response));
$this->assertStringStartsWith('<https://example.com/en>; rel="alternate"; hreflang="en",', $response->headers->get('link'));
$this->assertStringEndsWith("as=style", $response->headers->get('link'));
}

/**
* @param string $pageName
*
Expand All @@ -116,7 +134,6 @@ protected function getNext($pageName)
$response = (new \Illuminate\Http\Response($html));

return function ($request) use ($response) {

return $response;
};
}
Expand Down

0 comments on commit 75d9fd6

Please sign in to comment.