Skip to content

Commit 75d9fd6

Browse files
juukieJacobBennett
authored andcommitted
Append to Link header if it is already present (#19)
1 parent 89896d9 commit 75d9fd6

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/Middleware/AddHttp2ServerPush.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
class AddHttp2ServerPush
1111
{
12-
1312
/**
1413
* The DomCrawler instance.
1514
*
@@ -116,7 +115,6 @@ private function buildLinkHeaderString($url)
116115
});
117116

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

122120
/**
@@ -128,7 +126,10 @@ private function buildLinkHeaderString($url)
128126
*/
129127
private function addLinkHeader(Response $response, $link)
130128
{
129+
if ($response->headers->get('Link')) {
130+
$link = $response->headers->get('Link') . ',' . $link;
131+
}
132+
131133
$response->header('Link', $link);
132134
}
133-
134135
}

tests/AddHttp2ServerPushTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ public function it_will_return_limit_count_of_links()
104104
$this->assertCount($limit, explode(",", $response->headers->get('link')));
105105
}
106106

107+
/** @test */
108+
public function it_will_append_to_header_if_already_present()
109+
{
110+
$request = new Request();
111+
112+
$next = $this->getNext('pageWithCss');
113+
114+
$response = $this->middleware->handle($request, function ($request) use ($next) {
115+
$response = $next($request);
116+
$response->headers->set('Link', '<https://example.com/en>; rel="alternate"; hreflang="en"');
117+
return $response;
118+
});
119+
120+
$this->assertTrue($this->isServerPushResponse($response));
121+
$this->assertStringStartsWith('<https://example.com/en>; rel="alternate"; hreflang="en",', $response->headers->get('link'));
122+
$this->assertStringEndsWith("as=style", $response->headers->get('link'));
123+
}
124+
107125
/**
108126
* @param string $pageName
109127
*
@@ -116,7 +134,6 @@ protected function getNext($pageName)
116134
$response = (new \Illuminate\Http\Response($html));
117135

118136
return function ($request) use ($response) {
119-
120137
return $response;
121138
};
122139
}

0 commit comments

Comments
 (0)