ChromePHPHandlerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  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\TestCase;
  12. use Monolog\Logger;
  13. /**
  14. * @covers Monolog\Handler\ChromePHPHandler
  15. */
  16. class ChromePHPHandlerTest extends TestCase
  17. {
  18. protected function setUp()
  19. {
  20. TestChromePHPHandler::reset();
  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(Logger::DEBUG));
  32. $handler->handle($this->getRecord(Logger::WARNING));
  33. $expected = array(
  34. 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
  35. 'version' => ChromePHPHandler::VERSION,
  36. 'columns' => array('label', 'log', 'backtrace', 'type'),
  37. 'rows' => array(
  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 array(
  49. array('Monolog Test; Chrome/1.0'),
  50. array('Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'),
  51. array('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. array('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(Logger::DEBUG));
  59. $handler->handle($this->getRecord(Logger::WARNING, str_repeat('a', 150 * 1024)));
  60. // overflow chrome headers limit
  61. $handler->handle($this->getRecord(Logger::WARNING, str_repeat('a', 100 * 1024)));
  62. $expected = array(
  63. 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
  64. 'version' => ChromePHPHandler::VERSION,
  65. 'columns' => array('label', 'log', 'backtrace', 'type'),
  66. 'rows' => array(
  67. array(
  68. 'test',
  69. 'test',
  70. 'unknown',
  71. 'log',
  72. ),
  73. array(
  74. 'test',
  75. str_repeat('a', 150 * 1024),
  76. 'unknown',
  77. 'warn',
  78. ),
  79. array(
  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(Logger::DEBUG));
  96. $handler->handle($this->getRecord(Logger::WARNING));
  97. $handler2 = new TestChromePHPHandler();
  98. $handler2->setFormatter($this->getIdentityFormatter());
  99. $handler2->handle($this->getRecord(Logger::DEBUG));
  100. $handler2->handle($this->getRecord(Logger::WARNING));
  101. $expected = array(
  102. 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
  103. 'version' => ChromePHPHandler::VERSION,
  104. 'columns' => array('label', 'log', 'backtrace', 'type'),
  105. 'rows' => array(
  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 $headers = array();
  120. public static function reset()
  121. {
  122. self::$initialized = false;
  123. self::$overflowed = false;
  124. self::$sendHeaders = true;
  125. self::$json['rows'] = array();
  126. }
  127. protected function sendHeader($header, $content)
  128. {
  129. $this->headers[$header] = $content;
  130. }
  131. public function getHeaders()
  132. {
  133. return $this->headers;
  134. }
  135. }