Skip to content

Commit d6d9da1

Browse files
committed
test for Protocols\Http
1 parent d6d730f commit d6d9da1

File tree

1 file changed

+100
-2
lines changed

1 file changed

+100
-2
lines changed

tests/Unit/Protocols/HttpTest.php

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
use Workerman\Connection\TcpConnection;
44
use Workerman\Protocols\Http;
5+
use Workerman\Protocols\Http\Request;
6+
use Workerman\Protocols\Http\Response;
57

68
it('customizes request class', function () {
79
//backup old request class
810
$oldRequestClass = Http::requestClass();
911

1012
//actual test
11-
$class = new class{
13+
$class = new class {
1214
};
1315
Http::requestClass($class::class);
1416
expect(Http::requestClass())->toBe($class::class);
@@ -17,11 +19,107 @@
1719
Http::requestClass($oldRequestClass);
1820
});
1921

20-
it('tests ::encode', function () {
22+
it('tests ::input', function () {
23+
$testWithConnectionClose = function (Closure $closure, string $dataContains = null) {
24+
$tcpConnection = Mockery::spy(TcpConnection::class);
25+
$closure($tcpConnection);
26+
if ($dataContains) {
27+
$tcpConnection->shouldHaveReceived('close', function ($actual) use ($dataContains) {
28+
return str_contains($actual, $dataContains);
29+
});
30+
} else {
31+
$tcpConnection->shouldHaveReceived('close');
32+
}
33+
};
34+
35+
//test 413 payload too large
36+
$testWithConnectionClose(function (TcpConnection $tcpConnection) {
37+
expect(Http::input(str_repeat('jhdxr', 3333), $tcpConnection))
38+
->toBe(0);
39+
}, '413 Payload Too Large');
40+
41+
//example request from ChatGPT :)
42+
$buffer = "POST /path/to/resource HTTP/1.1\r\n" .
43+
"Host: example.com\r\n" .
44+
"Content-Type: application/json\r\n" .
45+
"Content-Length: 27\r\n" .
46+
"\r\n" .
47+
'{"key": "value", "foo": "bar"}';
48+
49+
//unrecognized method
50+
$testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
51+
expect(Http::input(str_replace('POST', 'MIAOWU', $buffer), $tcpConnection))
52+
->toBe(0);
53+
}, '400 Bad Request');
54+
55+
//HTTP 1.1 without Host header
56+
$testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
57+
expect(Http::input(str_replace("Host: ", 'NotHost: ', $buffer), $tcpConnection))
58+
->toBe(0);
59+
}, '400 Bad Request');
60+
61+
//content-length exceeds connection max package size
62+
$testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
63+
$tcpConnection->maxPackageSize = 10;
64+
expect(Http::input($buffer, $tcpConnection))
65+
->toBe(0);
66+
}, '413 Payload Too Large');
67+
});
68+
69+
it('tests ::encode for non-object response', function () {
2170
$tcpConnection = Mockery::mock(TcpConnection::class);
71+
$tcpConnection->headers = [
72+
'foo' => 'bar',
73+
'jhdxr' => ['a', 'b'],
74+
];
75+
$extHeader = "foo: bar\r\n" .
76+
"jhdxr: a\r\n" .
77+
"jhdxr: b\r\n";
2278

79+
expect(Http::encode('xiami', $tcpConnection))
80+
->toBe("HTTP/1.1 200 OK\r\n" .
81+
"Server: workerman\r\n" .
82+
"{$extHeader}Connection: keep-alive\r\n" .
83+
"Content-Type: text/html;charset=utf-8\r\n" .
84+
"Content-Length: 5\r\n\r\nxiami");
85+
});
86+
87+
it('tests ::encode for ' . Response::class, function () {
88+
$tcpConnection = Mockery::mock(TcpConnection::class);
89+
$tcpConnection->headers = [
90+
'foo' => 'bar',
91+
'jhdxr' => ['a', 'b'],
92+
];
93+
$extHeader = "foo: bar\r\n" .
94+
"jhdxr: a\r\n" .
95+
"jhdxr: b\r\n";
96+
97+
$response = new Response(body: 'xiami');
98+
99+
expect(Http::encode($response, $tcpConnection))
100+
->toBe("HTTP/1.1 200 OK\r\n" .
101+
"Server: workerman\r\n" .
102+
"{$extHeader}Connection: keep-alive\r\n" .
103+
"Content-Type: text/html;charset=utf-8\r\n" .
104+
"Content-Length: 5\r\n\r\nxiami");
23105
});
24106

25107
it('tests ::decode', function () {
108+
$tcpConnection = Mockery::mock(TcpConnection::class);
109+
110+
//example request from ChatGPT :)
111+
$buffer = "POST /path/to/resource HTTP/1.1\r\n" .
112+
"Host: example.com\r\n" .
113+
"Content-Type: application/json\r\n" .
114+
"Content-Length: 27\r\n" .
115+
"\r\n" .
116+
'{"key": "value", "foo": "bar"}';
117+
118+
$value = expect(Http::decode($buffer, $tcpConnection))
119+
->toBeInstanceOf(Request::class)
120+
->value;
26121

122+
//test cache
123+
expect($value == Http::decode($buffer, $tcpConnection))
124+
->toBeTrue();
27125
});

0 commit comments

Comments
 (0)