2
2
3
3
use Workerman \Connection \TcpConnection ;
4
4
use Workerman \Protocols \Http ;
5
+ use Workerman \Protocols \Http \Request ;
6
+ use Workerman \Protocols \Http \Response ;
5
7
6
8
it ('customizes request class ' , function () {
7
9
//backup old request class
8
10
$ oldRequestClass = Http::requestClass ();
9
11
10
12
//actual test
11
- $ class = new class {
13
+ $ class = new class {
12
14
};
13
15
Http::requestClass ($ class ::class);
14
16
expect (Http::requestClass ())->toBe ($ class ::class);
17
19
Http::requestClass ($ oldRequestClass );
18
20
});
19
21
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 () {
21
70
$ 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" ;
22
78
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 " );
23
105
});
24
106
25
107
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 ;
26
121
122
+ //test cache
123
+ expect ($ value == Http::decode ($ buffer , $ tcpConnection ))
124
+ ->toBeTrue ();
27
125
});
0 commit comments