2
2
3
3
namespace TomKeyte \LaravelHttp2Push \Tests ;
4
4
5
+ use Illuminate \Foundation \Testing \TestResponse ;
5
6
use Illuminate \Http \Request ;
7
+ use Illuminate \Http \Response ;
8
+ use Illuminate \Support \Facades \Log ;
6
9
use TomKeyte \LaravelHttp2Push \Http2PushServiceProvider ;
7
10
use TomKeyte \LaravelHttp2Push \Middleware \AddLinkHeader ;
8
11
use TomKeyte \LaravelHttp2Push \PushCookie ;
9
12
use Orchestra \Testbench \TestCase ;
13
+ use stdClass ;
10
14
11
15
class MiddlewareTest extends TestCase
12
16
{
@@ -26,40 +30,129 @@ protected function setUp(): void
26
30
// have to empty cookie array between runs
27
31
$ _COOKIE = [];
28
32
29
- $ this ->response = $ this -> get ( ' / ' );
30
- $ this ->request = Request:: create ( ' / ' , ' GET ' );
33
+ $ this ->response = new Response ( );
34
+ $ this ->request = new Request ( );
31
35
$ this ->push = $ this ->app ->get ('http2push ' );
32
36
$ this ->push ->add ('/js/app.js ' );
33
37
}
34
38
39
+ /**
40
+ * Converts a base response class into a test response
41
+ */
42
+ private function toTestResponse ($ response )
43
+ {
44
+ return TestResponse::fromBaseResponse ($ response );
45
+ }
46
+
47
+ /**
48
+ * @test
49
+ */
50
+ public function a_response_has_the_link_header ()
51
+ {
52
+ $ this ->toTestResponse (
53
+ (new AddLinkHeader )
54
+ ->handle (
55
+ $ this ->request ,
56
+ function () {
57
+ return $ this ->response ;
58
+ }
59
+ )
60
+ )
61
+ ->assertHeader ('Link ' , $ this ->push ->buildLinkHeader ());
62
+ }
35
63
36
64
/**
37
65
* @test
38
66
*/
39
67
public function a_response_has_the_push_cookie ()
40
68
{
41
- (new AddLinkHeader )
42
- ->handle (
43
- $ this ->request ,
44
- function () {
45
- return $ this ->response ;
46
- }
47
- )
69
+ $ this ->toTestResponse (
70
+ (new AddLinkHeader )
71
+ ->handle (
72
+ $ this ->request ,
73
+ function () {
74
+ return $ this ->response ;
75
+ }
76
+ )
77
+ )
48
78
->assertCookie ((new PushCookie ('/js/app.js ' ))->getName ());
49
79
}
50
80
51
81
/**
52
82
* @test
53
83
*/
54
- public function a_response_has_the_link_header ()
84
+ public function it_does_not_set_link_header_for_json_request ()
55
85
{
86
+ $ jsonRequest = (new Request ());
87
+ $ jsonRequest ->headers ->set ('Content-Type ' , 'application/json ' , true );
88
+
89
+ $ this ->toTestResponse (
90
+ (new AddLinkHeader )
91
+ ->handle (
92
+ $ jsonRequest ,
93
+ function () {
94
+ return $ this ->response ;
95
+ }
96
+ )
97
+ )
98
+ ->assertHeaderMissing ('Link ' );
99
+ }
100
+
101
+ /**
102
+ * @test
103
+ */
104
+ public function it_does_not_set_link_header_for_redirect_response ()
105
+ {
106
+ $ redirectResponse = new Response ('' , 302 );
107
+
108
+ $ this ->toTestResponse (
109
+ (new AddLinkHeader )
110
+ ->handle (
111
+ $ this ->request ,
112
+ function () use ($ redirectResponse ) {
113
+ return $ redirectResponse ;
114
+ }
115
+ )
116
+ )
117
+ ->assertHeaderMissing ('Link ' );
118
+ }
119
+
120
+ /**
121
+ * @test
122
+ */
123
+ public function it_does_not_set_link_header_for_a_binary_response ()
124
+ {
125
+ $ binaryResponse = response ()->streamDownload (
126
+ function () {
127
+ return ['data ' ];
128
+ }
129
+ );
130
+
131
+ $ this ->toTestResponse (
132
+ (new AddLinkHeader )
133
+ ->handle (
134
+ $ this ->request ,
135
+ function () use ($ binaryResponse ) {
136
+ return $ binaryResponse ;
137
+ }
138
+ )
139
+ )
140
+ ->assertHeaderMissing ('Link ' );
141
+ }
142
+
143
+ /**
144
+ * @test
145
+ */
146
+ public function it_logs_an_error_if_middleware_throws_exception ()
147
+ {
148
+ Log::shouldReceive ('error ' );
149
+
56
150
(new AddLinkHeader )
57
151
->handle (
58
- $ this -> request ,
152
+ ( new stdClass ) ,
59
153
function () {
60
154
return $ this ->response ;
61
155
}
62
- )
63
- ->assertHeader ('Link ' , $ this ->push ->buildLinkHeader ());
156
+ );
64
157
}
65
158
}
0 commit comments