File: ChromePHPHandlerTest.php

package info (click to toggle)
php-monolog 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,456 kB
  • sloc: php: 16,836; makefile: 20
file content (159 lines) | stat: -rw-r--r-- 4,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php declare(strict_types=1);

/*
 * This file is part of the Monolog package.
 *
 * (c) Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Monolog\Handler;

use Monolog\Level;
use PHPUnit\Framework\Attributes\DataProvider;

/**
 * @covers Monolog\Handler\ChromePHPHandler
 */
class ChromePHPHandlerTest extends \Monolog\Test\MonologTestCase
{
    protected function setUp(): void
    {
        TestChromePHPHandler::resetStatic();
        $_SERVER['HTTP_USER_AGENT'] = 'Monolog Test; Chrome/1.0';
    }

    #[DataProvider('agentsProvider')]
    public function testHeaders($agent)
    {
        $_SERVER['HTTP_USER_AGENT'] = $agent;

        $handler = new TestChromePHPHandler();
        $handler->setFormatter($this->getIdentityFormatter());
        $handler->handle($this->getRecord(Level::Debug));
        $handler->handle($this->getRecord(Level::Warning));

        $expected = [
            'X-ChromeLogger-Data'   => base64_encode(json_encode([
                'version' => '4.0',
                'columns' => ['label', 'log', 'backtrace', 'type'],
                'rows' => [
                    'test',
                    'test',
                ],
                'request_uri' => '',
            ])),
        ];

        $this->assertEquals($expected, $handler->getHeaders());
    }

    public static function agentsProvider()
    {
        return [
            ['Monolog Test; Chrome/1.0'],
            ['Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'],
            ['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'],
            ['Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome Safari/537.36'],
        ];
    }

    public function testHeadersOverflow()
    {
        $handler = new TestChromePHPHandler();
        $handler->handle($this->getRecord(Level::Debug));
        $handler->handle($this->getRecord(Level::Warning, str_repeat('a', 2 * 1024)));

        // overflow chrome headers limit
        $handler->handle($this->getRecord(Level::Warning, str_repeat('b', 2 * 1024)));

        $expected = [
            'X-ChromeLogger-Data'   => base64_encode(json_encode([
                'version' => '4.0',
                'columns' => ['label', 'log', 'backtrace', 'type'],
                'rows' => [
                    [
                        'test',
                        'test',
                        'unknown',
                        'log',
                    ],
                    [
                        'test',
                        str_repeat('a', 2 * 1024),
                        'unknown',
                        'warn',
                    ],
                    [
                        'monolog',
                        'Incomplete logs, chrome header size limit reached',
                        'unknown',
                        'warn',
                    ],
                ],
                'request_uri' => '',
            ])),
        ];

        $this->assertEquals($expected, $handler->getHeaders());
    }

    public function testConcurrentHandlers()
    {
        $handler = new TestChromePHPHandler();
        $handler->setFormatter($this->getIdentityFormatter());
        $handler->handle($this->getRecord(Level::Debug));
        $handler->handle($this->getRecord(Level::Warning));

        $handler2 = new TestChromePHPHandler();
        $handler2->setFormatter($this->getIdentityFormatter());
        $handler2->handle($this->getRecord(Level::Debug));
        $handler2->handle($this->getRecord(Level::Warning));

        $expected = [
            'X-ChromeLogger-Data'   => base64_encode(json_encode([
                'version' => '4.0',
                'columns' => ['label', 'log', 'backtrace', 'type'],
                'rows' => [
                    'test',
                    'test',
                    'test',
                    'test',
                ],
                'request_uri' => '',
            ])),
        ];

        $this->assertEquals($expected, $handler2->getHeaders());
    }
}

class TestChromePHPHandler extends ChromePHPHandler
{
    protected array $headers = [];

    public static function resetStatic(): void
    {
        self::$initialized = false;
        self::$overflowed = false;
        self::$sendHeaders = true;
        self::$json['rows'] = [];
    }

    protected function sendHeader(string $header, string $content): void
    {
        $this->headers[$header] = $content;
    }

    public function getHeaders(): array
    {
        return $this->headers;
    }

    protected function isWebRequest(): bool
    {
        return true;
    }
}