-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathAccessLogHandlerTest.php
More file actions
134 lines (103 loc) · 6.26 KB
/
AccessLogHandlerTest.php
File metadata and controls
134 lines (103 loc) · 6.26 KB
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
<?php
namespace FrameworkX\Tests;
use FrameworkX\AccessLogHandler;
use PHPUnit\Framework\TestCase;
use React\Http\Message\Response;
use React\Http\Message\ServerRequest;
use function React\Promise\resolve;
class AccessLogHandlerTest extends TestCase
{
public function testInvokePrintsRequestLogWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$response = new Response(200, [], "Hello\n");
// 2021-01-29 12:22:01.717 127.0.0.1 "GET /users HTTP/1.1" 200 6\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"GET \/users HTTP\/1\.1\" 200 6" . PHP_EOL . "$/");
$handler($request, function () use ($response) { return $response; });
}
public function testInvokePrintsRequestWithQueryParametersLogWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('GET', 'http://localhost:8080/?a=1&b=hello wörld', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$response = new Response(200, [], "Hello\n");
// 2021-01-29 12:22:01.717 127.0.0.1 "GET /?a=1&b=hello%20w%C3%B6rld HTTP/1.1" 200 6\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"GET \/\?a=1&b=hello%20w%C3%B6rld HTTP\/1\.1\" 200 6" . PHP_EOL . "$/");
$handler($request, function () use ($response) { return $response; });
}
public function testInvokePrintsRequestWithEscapedSpecialCharactersInRequestMethodAndTargetWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('GE"T', 'http://localhost:8080/wörld', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$request = $request->withRequestTarget('/wörld');
$response = new Response(200, [], "Hello\n");
// 2021-01-29 12:22:01.717 127.0.0.1 "GE\x22T /w\xC3\xB6rld HTTP/1.1" 200 6\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"GE\\\\x22T \/w\\\\xC3\\\\xB6rld HTTP\/1\.1\" 200 6" . PHP_EOL . "$/");
$handler($request, function () use ($response) { return $response; });
}
public function testInvokePrintsPlainProxyRequestLogWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$request = $request->withRequestTarget('http://localhost:8080/users');
$response = new Response(400, [], "");
// 2021-01-29 12:22:01.717 127.0.0.1 "GET http://localhost:8080/users HTTP/1.1" 400 0\n
$this->expectOutputRegex("#^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"GET http://localhost:8080/users HTTP/1\.1\" 400 0" . PHP_EOL . "$#");
$handler($request, function () use ($response) { return $response; });
}
public function testInvokePrintsConnectProxyRequestLogWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('CONNECT', 'example.com:8080', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$request = $request->withRequestTarget('example.com:8080');
$response = new Response(400, [], "");
// 2021-01-29 12:22:01.717 127.0.0.1 "CONNECT example.com:8080 HTTP/1.1" 400 0\n
$this->expectOutputRegex("#^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"CONNECT example.com:8080 HTTP/1\.1\" 400 0" . PHP_EOL . "$#");
$handler($request, function () use ($response) { return $response; });
}
public function testInvokePrintsOptionsAsteriskLogWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('OPTIONS', 'http://example.com:8080', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$request = $request->withRequestTarget('*');
$response = new Response(400, [], "");
// 2021-01-29 12:22:01.717 127.0.0.1 "OPTIONS * HTTP/1.1" 400 0\n
$this->expectOutputRegex("#^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"OPTIONS \* HTTP/1\.1\" 400 0" . PHP_EOL . "$#");
$handler($request, function () use ($response) { return $response; });
}
public function testInvokeWithDeferredNextPrintsRequestLogWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$response = new Response(200, [], "Hello\n");
// 2021-01-29 12:22:01.717 127.0.0.1 "GET /users HTTP/1.1" 200 6\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"GET \/users HTTP\/1\.1\" 200 6" . PHP_EOL . "$/");
$handler($request, function () use ($response) { return resolve($response); });
}
public function testInvokeWithCoroutineNextPrintsRequestLogWithCurrentDateAndTime()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$response = new Response(200, [], "Hello\n");
$generator = $handler($request, function () use ($response) {
if (false) {
yield;
}
return $response;
});
/** @var \Generator $generator */
$this->assertInstanceOf(\Generator::class, $generator);
// 2021-01-29 12:22:01.717 127.0.0.1 "GET /users HTTP/1.1" 200 6\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} 127\.0\.0\.1 \"GET \/users HTTP\/1\.1\" 200 6" . PHP_EOL . "$/");
$generator->next();
}
public function testInvokeWithoutRemoteAddressPrintsRequestLogWithDashAsPlaceholder()
{
$handler = new AccessLogHandler();
$request = new ServerRequest('GET', 'http://localhost:8080/users');
$response = new Response(200, [], "Hello\n");
// 2021-01-29 12:22:01.717 - "GET /users HTTP/1.1" 200 6\n
$this->expectOutputRegex("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} - \"GET \/users HTTP\/1\.1\" 200 6" . PHP_EOL . "$/");
$handler($request, function () use ($response) { return $response; });
}
}