|
3 | 3 | namespace Tests\Unit; |
4 | 4 |
|
5 | 5 | use Cego\RequestLog\Services\RequestLogOptionsService; |
| 6 | +use Illuminate\Http\Request; |
6 | 7 | use Illuminate\Support\Facades\Log; |
7 | 8 | use Monolog\Logger; |
| 9 | +use Symfony\Component\HttpFoundation\Response; |
8 | 10 | use Tests\TestCase; |
9 | 11 | use Cego\RequestLog\Models\RequestLog; |
10 | 12 | use Illuminate\Support\Facades\Config; |
@@ -219,4 +221,109 @@ public function it_masks_response_cookies(): void |
219 | 221 | // Act |
220 | 222 | $this->post('/test', [], $headers); |
221 | 223 | } |
| 224 | + |
| 225 | + /** @test */ |
| 226 | + public function it_doesnt_crash_if_exception_on_response_doesnt_exist(): void |
| 227 | + { |
| 228 | + $loggerMock = $this->createMock(Logger::class); |
| 229 | + Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock); |
| 230 | + Log::partialMock()->shouldNotReceive('error'); |
| 231 | + |
| 232 | + |
| 233 | + $middleware = new LogRequest(); |
| 234 | + |
| 235 | + $request = new Request(); |
| 236 | + |
| 237 | + $response = new Response(); |
| 238 | + |
| 239 | + $middleware->terminate($request, $response); |
| 240 | + } |
| 241 | + |
| 242 | + /** @test */ |
| 243 | + public function it_truncates_very_long_json_bodies(): void |
| 244 | + { |
| 245 | + // Set config request-log.truncateBodyLength to 100 |
| 246 | + Config::set('request-log.truncateBodyLength', 100); |
| 247 | + |
| 248 | + $loggerMock = $this->createMock(Logger::class); |
| 249 | + Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock); |
| 250 | + |
| 251 | + $loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) { |
| 252 | + $this->assertEquals(100, strlen($context['http']['request']['body']['content'])); |
| 253 | + $this->assertEquals(100, strlen($context['http']['response']['body']['content'])); |
| 254 | + }); |
| 255 | + |
| 256 | + |
| 257 | + $middleware = new LogRequest(); |
| 258 | + |
| 259 | + |
| 260 | + $request = new Request(); |
| 261 | + // Set request body to a very long json string |
| 262 | + $request->initialize([], [], [], [], [], [], json_encode(range(0, 10000))); |
| 263 | + |
| 264 | + $response = new Response(); |
| 265 | + // Set response body to a very long json string |
| 266 | + $response->setContent(json_encode(range(0, 10000))); |
| 267 | + |
| 268 | + $middleware->terminate($request, $response); |
| 269 | + } |
| 270 | + |
| 271 | + /** @test */ |
| 272 | + public function it_doesnt_truncate_very_long_json_bodies_if_disabled(): void |
| 273 | + { |
| 274 | + // Set config request-log.truncateBodyLength to 100 |
| 275 | + Config::set('request-log.truncateBodyLength', -1); |
| 276 | + |
| 277 | + $loggerMock = $this->createMock(Logger::class); |
| 278 | + Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock); |
| 279 | + |
| 280 | + $loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) { |
| 281 | + $this->assertEquals(48897, strlen($context['http']['request']['body']['content'])); |
| 282 | + $this->assertEquals(48897, strlen($context['http']['response']['body']['content'])); |
| 283 | + }); |
| 284 | + |
| 285 | + |
| 286 | + $middleware = new LogRequest(); |
| 287 | + |
| 288 | + |
| 289 | + $request = new Request(); |
| 290 | + // Set request body to a very long json string |
| 291 | + $request->initialize([], [], [], [], [], [], json_encode(range(0, 10000))); |
| 292 | + |
| 293 | + $response = new Response(); |
| 294 | + // Set response body to a very long json string |
| 295 | + $response->setContent(json_encode(range(0, 10000))); |
| 296 | + |
| 297 | + $middleware->terminate($request, $response); |
| 298 | + } |
| 299 | + |
| 300 | + /** @test */ |
| 301 | + public function it_doesnt_truncate_bodies_shorter_than_truncate_limit(): void |
| 302 | + { |
| 303 | + // Set config request-log.truncateBodyLength to 100 |
| 304 | + Config::set('request-log.truncateBodyLength', 100); |
| 305 | + |
| 306 | + $loggerMock = $this->createMock(Logger::class); |
| 307 | + Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock); |
| 308 | + |
| 309 | + $loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) { |
| 310 | + $this->assertEquals(3, strlen($context['http']['request']['body']['content'])); |
| 311 | + $this->assertEquals(3, strlen($context['http']['response']['body']['content'])); |
| 312 | + }); |
| 313 | + |
| 314 | + |
| 315 | + $middleware = new LogRequest(); |
| 316 | + |
| 317 | + |
| 318 | + $request = new Request(); |
| 319 | + // Set request body to a very long json string |
| 320 | + $request->initialize([], [], [], [], [], [], "hej"); |
| 321 | + |
| 322 | + $response = new Response(); |
| 323 | + // Set response body to a very long json string |
| 324 | + $response->setContent("hej"); |
| 325 | + |
| 326 | + $middleware->terminate($request, $response); |
| 327 | + } |
| 328 | + |
222 | 329 | } |
0 commit comments