Skip to content

Commit 47767f6

Browse files
peterfoxtimacdonaldtaylorotwell
authored
[10.x] Adds testing helpers for Precognition (#48151)
* adds testing helpers for Precognition * Review changes * Fix comment * fix comment * Add functional test to precog test * Add assertNoContent to response assertion * Fixes * Update MakesHttpRequests.php * formatting --------- Co-authored-by: Tim MacDonald <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 60c1330 commit 47767f6

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,16 @@ public function from(string $url)
309309
return $this->withHeader('referer', $url);
310310
}
311311

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+
312322
/**
313323
* Visit the given URI with a GET request.
314324
*

src/Illuminate/Testing/TestResponse.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ public function assertSuccessful()
9595
return $this;
9696
}
9797

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+
98121
/**
99122
* Assert that the response is a server error.
100123
*

tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Routing\Registrar;
66
use Illuminate\Contracts\Routing\UrlGenerator;
7+
use Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests;
78
use Illuminate\Http\RedirectResponse;
89
use Orchestra\Testbench\TestCase;
910

@@ -186,6 +187,19 @@ public function testFollowingRedirectsTerminatesInExpectedOrder()
186187

187188
$this->assertEquals(['from', 'to'], $callOrder);
188189
}
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+
}
189203
}
190204

191205
class MyMiddleware

tests/Testing/TestResponseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,32 @@ public function testAssertHeaderMissing()
10351035
$response->assertHeaderMissing('Location');
10361036
}
10371037

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+
10381064
public function testAssertJsonWithArray()
10391065
{
10401066
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));

0 commit comments

Comments
 (0)