File tree Expand file tree Collapse file tree 4 files changed +73
-0
lines changed
Foundation/Testing/Concerns
Foundation/Testing/Concerns Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -309,6 +309,16 @@ public function from(string $url)
309
309
return $ this ->withHeader ('referer ' , $ url );
310
310
}
311
311
312
+ /**
313
+ * Set the Precognition header to "true".
314
+ *
315
+ * @return $this
316
+ */
317
+ public function withPrecognition ()
318
+ {
319
+ return $ this ->withHeader ('Precognition ' , 'true ' );
320
+ }
321
+
312
322
/**
313
323
* Visit the given URI with a GET request.
314
324
*
Original file line number Diff line number Diff line change @@ -95,6 +95,29 @@ public function assertSuccessful()
95
95
return $ this ;
96
96
}
97
97
98
+ /**
99
+ * Assert that the Precognition request was successful.
100
+ *
101
+ * @return $this
102
+ */
103
+ public function assertSuccessfulPrecognition ()
104
+ {
105
+ $ this ->assertNoContent ();
106
+
107
+ PHPUnit::assertTrue (
108
+ $ this ->headers ->has ('Precognition-Success ' ),
109
+ 'Header [Precognition-Success] not present on response. '
110
+ );
111
+
112
+ PHPUnit::assertSame (
113
+ 'true ' ,
114
+ $ this ->headers ->get ('Precognition-Success ' ),
115
+ 'The Precognition-Success header was found, but the value is not `true`. '
116
+ );
117
+
118
+ return $ this ;
119
+ }
120
+
98
121
/**
99
122
* Assert that the response is a server error.
100
123
*
Original file line number Diff line number Diff line change 4
4
5
5
use Illuminate \Contracts \Routing \Registrar ;
6
6
use Illuminate \Contracts \Routing \UrlGenerator ;
7
+ use Illuminate \Foundation \Http \Middleware \HandlePrecognitiveRequests ;
7
8
use Illuminate \Http \RedirectResponse ;
8
9
use Orchestra \Testbench \TestCase ;
9
10
@@ -186,6 +187,19 @@ public function testFollowingRedirectsTerminatesInExpectedOrder()
186
187
187
188
$ this ->assertEquals (['from ' , 'to ' ], $ callOrder );
188
189
}
190
+
191
+ public function testWithPrecognition ()
192
+ {
193
+ $ this ->withPrecognition ();
194
+ $ this ->assertSame ('true ' , $ this ->defaultHeaders ['Precognition ' ]);
195
+
196
+ $ this ->app ->make (Registrar::class)
197
+ ->get ('test-route ' , fn () => 'ok ' )->middleware (HandlePrecognitiveRequests::class);
198
+ $ this ->get ('test-route ' )
199
+ ->assertStatus (204 )
200
+ ->assertHeader ('Precognition ' , 'true ' )
201
+ ->assertHeader ('Precognition-Success ' , 'true ' );
202
+ }
189
203
}
190
204
191
205
class MyMiddleware
Original file line number Diff line number Diff line change @@ -1035,6 +1035,32 @@ public function testAssertHeaderMissing()
1035
1035
$ response ->assertHeaderMissing ('Location ' );
1036
1036
}
1037
1037
1038
+ public function testAssertPrecognitionSuccessfulWithMissingHeader ()
1039
+ {
1040
+ $ this ->expectException (AssertionFailedError::class);
1041
+ $ this ->expectExceptionMessage ('Header [Precognition-Success] not present on response. ' );
1042
+
1043
+ $ baseResponse = new Response ('' , 204 );
1044
+
1045
+ $ response = TestResponse::fromBaseResponse ($ baseResponse );
1046
+
1047
+ $ response ->assertSuccessfulPrecognition ();
1048
+ }
1049
+
1050
+ public function testAssertPrecognitionSuccessfulWithIncorrectValue ()
1051
+ {
1052
+ $ this ->expectException (AssertionFailedError::class);
1053
+ $ this ->expectExceptionMessage ('The Precognition-Success header was found, but the value is not `true`. ' );
1054
+
1055
+ $ baseResponse = tap (new Response ('' , 204 ), function ($ response ) {
1056
+ $ response ->header ('Precognition-Success ' , '' );
1057
+ });
1058
+
1059
+ $ response = TestResponse::fromBaseResponse ($ baseResponse );
1060
+
1061
+ $ response ->assertSuccessfulPrecognition ();
1062
+ }
1063
+
1038
1064
public function testAssertJsonWithArray ()
1039
1065
{
1040
1066
$ response = TestResponse::fromBaseResponse (new Response (new JsonSerializableSingleResourceStub ));
You can’t perform that action at this time.
0 commit comments