|
21 | 21 | use Cake\Http\CallbackStream; |
22 | 22 | use Cake\Http\Response; |
23 | 23 | use Cake\Http\ServerRequest; |
| 24 | +use Cake\Routing\Router; |
24 | 25 | use Cake\TestSuite\TestCase; |
25 | 26 | use DebugKit\Middleware\DebugKitMiddleware; |
26 | 27 | use Psr\Http\Server\RequestHandlerInterface; |
@@ -53,7 +54,7 @@ public function setUp(): void |
53 | 54 | parent::setUp(); |
54 | 55 |
|
55 | 56 | $connection = ConnectionManager::get('test'); |
56 | | - $this->skipIf($connection->getDriver() instanceof Sqlite, 'Schema insertion/removal breaks SQLite'); |
| 57 | + $this->skipIf($connection->getDriver() instanceof Sqlite, 'This test fails in CI with sqlite'); |
57 | 58 | $this->oldConfig = Configure::read('DebugKit'); |
58 | 59 | $this->restore = $GLOBALS['FORCE_DEBUGKIT_TOOLBAR']; |
59 | 60 | $GLOBALS['FORCE_DEBUGKIT_TOOLBAR'] = true; |
@@ -135,6 +136,39 @@ public function testInvokeSaveData() |
135 | 136 | $this->assertTextEquals($expected, $body); |
136 | 137 | } |
137 | 138 |
|
| 139 | + /** |
| 140 | + * Ensure data is saved for HTML requests |
| 141 | + * |
| 142 | + * @return void |
| 143 | + */ |
| 144 | + public function testInvokeInjectCspNonce() |
| 145 | + { |
| 146 | + $request = new ServerRequest([ |
| 147 | + 'url' => '/articles', |
| 148 | + 'environment' => ['REQUEST_METHOD' => 'GET'], |
| 149 | + ]); |
| 150 | + $request = $request->withAttribute('cspScriptNonce', 'csp-nonce'); |
| 151 | + Router::setRequest($request); |
| 152 | + |
| 153 | + $response = new Response([ |
| 154 | + 'statusCode' => 200, |
| 155 | + 'type' => 'text/html', |
| 156 | + 'body' => '<html><title>test</title><body><p>some text</p></body>', |
| 157 | + ]); |
| 158 | + |
| 159 | + $handler = $this->handler(); |
| 160 | + $handler->expects($this->once()) |
| 161 | + ->method('handle') |
| 162 | + ->willReturn($response); |
| 163 | + |
| 164 | + $middleware = new DebugKitMiddleware(); |
| 165 | + $response = $middleware->process($request, $handler); |
| 166 | + $this->assertInstanceOf(Response::class, $response, 'Should return the response'); |
| 167 | + |
| 168 | + $body = (string)$response->getBody(); |
| 169 | + $this->assertStringContainsString('nonce="csp-nonce"', $body); |
| 170 | + } |
| 171 | + |
138 | 172 | /** |
139 | 173 | * Ensure that streaming results are tracked, but not modified. |
140 | 174 | * |
|
0 commit comments