ChromePHPHandlerTest.php 4.7 KB

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