diff --git a/src/Middleware/AddHttp2ServerPush.php b/src/Middleware/AddHttp2ServerPush.php index 108e599..2a4cef2 100644 --- a/src/Middleware/AddHttp2ServerPush.php +++ b/src/Middleware/AddHttp2ServerPush.php @@ -9,7 +9,6 @@ class AddHttp2ServerPush { - /** * The DomCrawler instance. * @@ -116,7 +115,6 @@ private function buildLinkHeaderString($url) }); return is_null($type) ? null : "<{$url}>; rel=preload; as={$type}"; - } /** @@ -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); } - } diff --git a/tests/AddHttp2ServerPushTest.php b/tests/AddHttp2ServerPushTest.php index 6e6e912..d0ec4b3 100644 --- a/tests/AddHttp2ServerPushTest.php +++ b/tests/AddHttp2ServerPushTest.php @@ -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', '; rel="alternate"; hreflang="en"'); + return $response; + }); + + $this->assertTrue($this->isServerPushResponse($response)); + $this->assertStringStartsWith('; rel="alternate"; hreflang="en",', $response->headers->get('link')); + $this->assertStringEndsWith("as=style", $response->headers->get('link')); + } + /** * @param string $pageName * @@ -116,7 +134,6 @@ protected function getNext($pageName) $response = (new \Illuminate\Http\Response($html)); return function ($request) use ($response) { - return $response; }; }