ChromePHPHandlerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Test\TestCase;
  12. use Monolog\Level;
  13. use PHPUnit\Framework\Attributes\DataProvider;
  14. /**
  15. * @covers Monolog\Handler\ChromePHPHandler
  16. */
  17. class ChromePHPHandlerTest extends TestCase
  18. {
  19. protected function setUp(): void
  20. {
  21. TestChromePHPHandler::resetStatic();
  22. $_SERVER['HTTP_USER_AGENT'] = 'Monolog Test; Chrome/1.0';
  23. }
  24. #[DataProvider('agentsProvider')]
  25. public function testHeaders($agent)
  26. {
  27. $_SERVER['HTTP_USER_AGENT'] = $agent;
  28. $handler = new TestChromePHPHandler();
  29. $handler->setFormatter($this->getIdentityFormatter());
  30. $handler->handle($this->getRecord(Level::Debug));
  31. $handler->handle($this->getRecord(Level::Warning));
  32. $expected = [
  33. 'X-ChromeLogger-Data' => base64_encode(json_encode([
  34. 'version' => '4.0',
  35. 'columns' => ['label', 'log', 'backtrace', 'type'],
  36. 'rows' => [
  37. 'test',
  38. 'test',
  39. ],
  40. 'request_uri' => '',
  41. ])),
  42. ];
  43. $this->assertEquals($expected, $handler->getHeaders());
  44. }
  45. public static function agentsProvider()
  46. {
  47. return [
  48. ['Monolog Test; Chrome/1.0'],
  49. ['Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'],
  50. ['Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/56.0.2924.76 Chrome/56.0.2924.76 Safari/537.36'],
  51. ['Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome Safari/537.36'],
  52. ];
  53. }
  54. public function testHeadersOverflow()
  55. {
  56. $handler = new TestChromePHPHandler();
  57. $handler->handle($this->getRecord(Level::Debug));
  58. $handler->handle($this->getRecord(Level::Warning, str_repeat('a', 2 * 1024)));
  59. // overflow chrome headers limit
  60. $handler->handle($this->getRecord(Level::Warning, str_repeat('b', 2 * 1024)));
  61. $expected = [
  62. 'X-ChromeLogger-Data' => base64_encode(json_encode([
  63. 'version' => '4.0',
  64. 'columns' => ['label', 'log', 'backtrace', 'type'],
  65. 'rows' => [
  66. [
  67. 'test',
  68. 'test',
  69. 'unknown',
  70. 'log',
  71. ],
  72. [
  73. 'test',
  74. str_repeat('a', 2 * 1024),
  75. 'unknown',
  76. 'warn',
  77. ],
  78. [
  79. 'monolog',
  80. 'Incomplete logs, chrome header size limit reached',
  81. 'unknown',
  82. 'warn',
  83. ],
  84. ],
  85. 'request_uri' => '',
  86. ])),
  87. ];
  88. $this->assertEquals($expected, $handler->getHeaders());
  89. }
  90. public function testConcurrentHandlers()
  91. {
  92. $handler = new TestChromePHPHandler();
  93. $handler->setFormatter($this->getIdentityFormatter());
  94. $handler->handle($this->getRecord(Level::Debug));
  95. $handler->handle($this->getRecord(Level::Warning));
  96. $handler2 = new TestChromePHPHandler();
  97. $handler2->setFormatter($this->getIdentityFormatter());
  98. $handler2->handle($this->getRecord(Level::Debug));
  99. $handler2->handle($this->getRecord(Level::Warning));
  100. $expected = [
  101. 'X-ChromeLogger-Data' => base64_encode(json_encode([
  102. 'version' => '4.0',
  103. 'columns' => ['label', 'log', 'backtrace', 'type'],
  104. 'rows' => [
  105. 'test',
  106. 'test',
  107. 'test',
  108. 'test',
  109. ],
  110. 'request_uri' => '',
  111. ])),
  112. ];
  113. $this->assertEquals($expected, $handler2->getHeaders());
  114. }
  115. }
  116. class TestChromePHPHandler extends ChromePHPHandler
  117. {
  118. protected array $headers = [];
  119. public static function resetStatic(): void
  120. {
  121. self::$initialized = false;
  122. self::$overflowed = false;
  123. self::$sendHeaders = true;
  124. self::$json['rows'] = [];
  125. }
  126. protected function sendHeader(string $header, string $content): void
  127. {
  128. $this->headers[$header] = $content;
  129. }
  130. public function getHeaders(): array
  131. {
  132. return $this->headers;
  133. }
  134. protected function isWebRequest(): bool
  135. {
  136. return true;
  137. }
  138. }