1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Http\Request; |
|
|
|
|
4
|
|
|
use Krenor\Http2Pusher\Response; |
|
|
|
|
5
|
|
|
use Illuminate\Http\RedirectResponse; |
6
|
|
|
use Krenor\Http2Pusher\Tests\TestCase; |
7
|
|
|
use Krenor\Http2Pusher\Middleware\ServerPush; |
8
|
|
|
|
9
|
|
|
class ServerPushMiddlewareTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ServerPush |
13
|
|
|
*/ |
14
|
|
|
private $middleware; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Bootstrap the test environment. |
18
|
|
|
*/ |
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
parent::setUp(); |
22
|
|
|
|
23
|
|
|
$this->createManifestFile(); |
24
|
|
|
|
25
|
|
|
$this->middleware = new ServerPush($this->builder); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Clean the test environment for the next test. |
30
|
|
|
*/ |
31
|
|
|
public function tearDown() |
32
|
|
|
{ |
33
|
|
|
$manifest = public_path('mix-manifest.json'); |
34
|
|
|
|
35
|
|
|
if (file_exists($manifest)) { |
36
|
|
|
unlink($manifest); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @test */ |
41
|
|
View Code Duplication |
public function it_should_not_push_anything_when_its_a_redirect_response() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$response = function ($request) { |
|
|
|
|
44
|
|
|
return new RedirectResponse('http://laravel.com'); |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
$response = $this->middleware->handle( |
48
|
|
|
$this->request, |
49
|
|
|
$response |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->assertFalse($response->headers->has('Link')); |
53
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @test */ |
57
|
|
View Code Duplication |
public function it_should_not_push_anything_when_its_a_json_request() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->request->headers->set('Content-Type', 'application/json'); |
60
|
|
|
|
61
|
|
|
$response = function ($request) { |
|
|
|
|
62
|
|
|
return new Response; |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
$response = $this->middleware->handle( |
66
|
|
|
$this->request, |
67
|
|
|
$response |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$this->assertFalse($response->headers->has('Link')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @test */ |
74
|
|
View Code Duplication |
public function it_should_not_push_anything_when_its_an_ajax_request() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
77
|
|
|
|
78
|
|
|
$response = function ($request) { |
|
|
|
|
79
|
|
|
return new Response; |
80
|
|
|
}; |
81
|
|
|
|
82
|
|
|
$response = $this->middleware->handle( |
83
|
|
|
$this->request, |
84
|
|
|
$response |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$this->assertFalse($response->headers->has('Link')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @test */ |
91
|
|
View Code Duplication |
public function it_should_crawl_for_scripts_and_push_them() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$includes = [ |
94
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js', |
95
|
|
|
'/js/manifest.js', |
96
|
|
|
'/js/vendor.js', |
97
|
|
|
'/js/app.js', |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
$response = $this->middleware->handle( |
101
|
|
|
$this->request, |
102
|
|
|
$this->getResponse('with-scripts') |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->assertTrue($response->headers->has('Link')); |
106
|
|
|
|
107
|
|
|
$link = $response->headers->get('Link'); |
108
|
|
|
|
109
|
|
|
foreach ($includes as $include) { |
110
|
|
|
$this->assertContains($include, $link); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->assertCount(count($includes), explode(',', $link)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** @test */ |
117
|
|
View Code Duplication |
public function it_should_crawl_for_style_sheets_and_push_them() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$includes = [ |
120
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap-grid.min.css', |
121
|
|
|
'/css/app.css', |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
$response = $this->middleware->handle( |
125
|
|
|
$this->request, |
126
|
|
|
$this->getResponse('with-styles') |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$this->assertTrue($response->headers->has('Link')); |
130
|
|
|
|
131
|
|
|
$link = $response->headers->get('Link'); |
132
|
|
|
|
133
|
|
|
foreach ($includes as $include) { |
134
|
|
|
$this->assertContains($include, $link); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->assertCount(count($includes), explode(',', $link)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** @test */ |
141
|
|
View Code Duplication |
public function it_should_crawl_for_images_and_push_them() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$includes = [ |
144
|
|
|
'http://stylecampaign.com/blog/blogimages/SVG/fox-1.svg', |
145
|
|
|
'https://laravel.com/assets/img/laravel-logo-white.png', |
146
|
|
|
'/images/laravel.jpg', |
147
|
|
|
'/images/chrome.svg', |
148
|
|
|
'/images/github.png', |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
$response = $this->middleware->handle( |
152
|
|
|
$this->request, |
153
|
|
|
$this->getResponse('with-images') |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$this->assertTrue($response->headers->has('Link')); |
157
|
|
|
|
158
|
|
|
$link = $response->headers->get('Link'); |
159
|
|
|
|
160
|
|
|
foreach ($includes as $include) { |
161
|
|
|
$this->assertContains($include, $link); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->assertCount(count($includes), explode(',', $link)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $page |
169
|
|
|
* |
170
|
|
|
* @return Closure |
171
|
|
|
*/ |
172
|
|
|
private function getResponse($page) |
173
|
|
|
{ |
174
|
|
|
return function ($request) use ($page) { |
|
|
|
|
175
|
|
|
return new Response( |
176
|
|
|
file_get_contents(__DIR__ . "/fixtures/pages/{$page}.html") |
177
|
|
|
); |
178
|
|
|
}; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Create the manifest file manually for tests regarding said file. |
183
|
|
|
* |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
private function createManifestFile() |
187
|
|
|
{ |
188
|
|
|
$content = '{ |
189
|
|
|
"/js/vendor.js": "/js/vendor.js?id=911084212bac1b5ea2a5", |
190
|
|
|
"/js/app.js": "/js/app.js?id=8e38929b2d5501e6808e", |
191
|
|
|
"/css/app.css": "/css/app.css?id=516707d9f36d4fb7d866", |
192
|
|
|
"/js/manifest.js": "/js/manifest.js?id=ac5def271276f7bf7ec1" |
193
|
|
|
}'; |
194
|
|
|
|
195
|
|
|
file_put_contents(public_path('mix-manifest.json'), $content); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: