|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtocolsTests\Http; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use CommonTestClass; |
|
7
|
|
|
use kalanis\RemoteRequest\Connection; |
|
8
|
|
|
use kalanis\RemoteRequest\Protocols\Http; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class QueryMock extends Http\Query |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Overwrite because random string in testing does not work |
|
15
|
|
|
* @return string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function generateBoundary(): string |
|
18
|
|
|
{ |
|
19
|
|
|
return '--PHPFSock--'; |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class QueryAuthBasicMock extends Http\Query\AuthBasic |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Overwrite because random string in testing does not work |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function generateBoundary(): string |
|
31
|
|
|
{ |
|
32
|
|
|
return '--PHPFSock--'; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
class QueryAuthDigestMock extends Http\Query\AuthDigest |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* Overwrite because random string in testing does not work |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function generateBoundary(): string |
|
44
|
|
|
{ |
|
45
|
|
|
return '--PHPFSock--'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function getRandomString(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return '0a4f113b'; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
class QueryTest extends CommonTestClass |
|
56
|
|
|
{ |
|
57
|
|
|
public function testQuerySimple(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$lib = $this->prepareSimple(); |
|
60
|
|
|
$lib->setHost('somewhere.example'); |
|
61
|
|
|
$lib->setPort(80); |
|
62
|
|
|
$this->assertEquals("GET /example HTTP/1.1\r\nHost: somewhere.example\r\n\r\n" |
|
63
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
64
|
|
|
$lib->setPort(444); |
|
65
|
|
|
$this->assertEquals("GET /example HTTP/1.1\r\nHost: somewhere.example:444\r\n\r\n" |
|
66
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
67
|
|
|
$lib->setRequestSettings($this->prepareProtocolSchema('disable.example', 60)); |
|
68
|
|
|
$this->assertEquals("GET /example HTTP/1.1\r\nHost: disable.example:60\r\n\r\n" |
|
69
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
70
|
|
|
$this->assertEquals("PUT", $lib->setMethod('put')->getMethod()); |
|
71
|
|
|
$this->assertEquals("PUT", $lib->setMethod('unknown')->getMethod()); |
|
72
|
|
|
|
|
73
|
|
|
$lib->addHeader('some', 'value'); |
|
74
|
|
|
$this->assertNotEquals(false, strpos(stream_get_contents($lib->getData(), -1, 0), 'some: value')); |
|
75
|
|
|
$lib->removeHeader('some'); |
|
76
|
|
|
$this->assertFalse(strpos(stream_get_contents($lib->getData(), -1, 0), 'some: value')); // not found |
|
77
|
|
|
$sett = new Connection\Params\Ssl(); |
|
78
|
|
|
$sett->setTarget('elsewhere.example', 2121); |
|
79
|
|
|
$lib->setRequestSettings($sett); |
|
80
|
|
|
$this->assertEquals('elsewhere.example', $lib->getHost()); |
|
81
|
|
|
$this->assertEquals(2121, $lib->getPort()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testQueryWithInline(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$lib = $this->prepareSimple(); |
|
87
|
|
|
$lib->setHost('somewhere.example'); |
|
88
|
|
|
$lib->setPort(80); |
|
89
|
|
|
$lib->addValue('foo', 'bar'); |
|
90
|
|
|
$lib->setInline(true); |
|
91
|
|
|
$this->assertEquals("GET /example?foo=bar HTTP/1.1\r\nHost: somewhere.example\r\n\r\n" |
|
92
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
93
|
|
|
$lib->setPath('/example?baz=abc'); |
|
94
|
|
|
$this->assertEquals("GET /example?baz=abc&foo=bar HTTP/1.1\r\nHost: somewhere.example\r\n\r\n" |
|
95
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testQueryWithContent(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$lib = $this->prepareSimple(); |
|
101
|
|
|
$lib->setInline(false); |
|
102
|
|
|
$lib->setHost('somewhere.example'); |
|
103
|
|
|
$lib->setPort(80); |
|
104
|
|
|
$lib->addValue('foo', 'bar'); |
|
105
|
|
|
$this->assertEquals( |
|
106
|
|
|
"GET /example HTTP/1.1\r\nHost: somewhere.example\r\n" |
|
107
|
|
|
. "Content-Length: 7\r\n\r\nfoo=bar" |
|
108
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
109
|
|
|
$lib->setPath('/example?baz=abc'); |
|
110
|
|
|
$this->assertEquals( |
|
111
|
|
|
"GET /example?baz=abc HTTP/1.1\r\nHost: somewhere.example\r\n" |
|
112
|
|
|
. "Content-Length: 7\r\n\r\nfoo=bar" |
|
113
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
114
|
|
|
$lib->addValues(['bar' => 'def', 'bay' => 'ghi']); |
|
115
|
|
|
$this->assertEquals( |
|
116
|
|
|
"GET /example?baz=abc HTTP/1.1\r\nHost: somewhere.example\r\n" |
|
117
|
|
|
. "Content-Length: 23\r\n\r\nfoo=bar&bar=def&bay=ghi" |
|
118
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testQueryWithContentPost(): void |
|
122
|
|
|
{ |
|
123
|
|
|
$lib = $this->prepareSimple(); |
|
124
|
|
|
$lib->setMethod('post'); |
|
125
|
|
|
$lib->setHost('somewhere.example'); |
|
126
|
|
|
$lib->setPort(80); |
|
127
|
|
|
$lib->addValue('foo', 'bar'); |
|
128
|
|
|
$this->assertEquals( |
|
129
|
|
|
"POST /example HTTP/1.1\r\nHost: somewhere.example\r\n" |
|
130
|
|
|
. "Content-Length: 7\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n" |
|
131
|
|
|
. "foo=bar" |
|
132
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
133
|
|
|
$lib->setPath('/example?baz=abc'); |
|
134
|
|
|
$this->assertEquals( |
|
135
|
|
|
"POST /example?baz=abc HTTP/1.1\r\nHost: somewhere.example\r\n" |
|
136
|
|
|
. "Content-Length: 7\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n" |
|
137
|
|
|
. "foo=bar" |
|
138
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
139
|
|
|
$lib->addValue('bar', 'def'); |
|
140
|
|
|
$this->assertEquals( |
|
141
|
|
|
"POST /example?baz=abc HTTP/1.1\r\nHost: somewhere.example\r\n" |
|
142
|
|
|
. "Content-Length: 15\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n" |
|
143
|
|
|
. "foo=bar&bar=def" |
|
144
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
145
|
|
|
$lib->removeValue('bar'); |
|
146
|
|
|
$this->assertEquals( |
|
147
|
|
|
"POST /example?baz=abc HTTP/1.1\r\nHost: somewhere.example\r\n" |
|
148
|
|
|
. "Content-Length: 7\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n" |
|
149
|
|
|
. "foo=bar" |
|
150
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testQueryWithContentFiles(): void |
|
154
|
|
|
{ |
|
155
|
|
|
$lib = $this->prepareSimple(); |
|
156
|
|
|
$lib->setRequestSettings($this->prepareProtocolSchema('somewhere.example', 512)) |
|
157
|
|
|
->addValues(['foo' => 'bar', 'up' => $this->prepareTestFile('mnbvcx')]); |
|
158
|
|
|
$this->assertEquals( |
|
159
|
|
|
"GET /example HTTP/1.1\r\nHost: somewhere.example:512\r\nContent-Length: 202\r\n\r\n" |
|
160
|
|
|
. "----PHPFSock--\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n" |
|
161
|
|
|
. "----PHPFSock--\r\nContent-Disposition: form-data; name=\"up\"; filename=\"dummy.txt\"\r\nContent-Type: text/plain\r\n\r\nmnbvcx\r\n" |
|
162
|
|
|
. "----PHPFSock----\r\n" |
|
163
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testQueryWithAuth(): void |
|
167
|
|
|
{ |
|
168
|
|
|
$lib = $this->prepareAuthBasic(); |
|
169
|
|
|
$lib->setRequestSettings($this->prepareProtocolSchema('somewhere.example', 126)) |
|
170
|
|
|
->addValues(['foo' => 'bar']); |
|
171
|
|
|
$lib->setInline(true); |
|
172
|
|
|
$this->assertEquals( |
|
173
|
|
|
"GET /example?foo=bar HTTP/1.1\r\nHost: somewhere.example:126\r\n" |
|
174
|
|
|
. "Authorization: Basic Zm9vZGlkOmJ1dWdnZWU=\r\n\r\n" |
|
175
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testQueryWithDigest(): void |
|
179
|
|
|
{ |
|
180
|
|
|
$lib = $this->prepareAuthDigest(); |
|
181
|
|
|
$lib->setRequestSettings($this->prepareProtocolSchema('localhost')); |
|
182
|
|
|
$this->assertEquals( |
|
183
|
|
|
"GET /dir/index.html HTTP/1.1\r\nHost: localhost\r\nAuthorization: Digest username=\"Mufasa\", realm=\"[email protected]\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", uri=\"/dir/index.html\", qop=\"auth\", nc=\"00000001\", cnonce=\"0a4f113b\", response=\"6629fae49393a05397450978507c4ef1\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"\r\n\r\n" |
|
184
|
|
|
, stream_get_contents($lib->getData(), -1, 0)); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
protected function prepareSimple(): Http\Query |
|
188
|
|
|
{ |
|
189
|
|
|
$lib = new QueryMock(); |
|
190
|
|
|
$lib->setMethod('get'); |
|
191
|
|
|
$lib->setPath('/example'); |
|
192
|
|
|
$lib->setInline(false); |
|
193
|
|
|
$lib->removeHeader('Accept'); |
|
194
|
|
|
$lib->removeHeader('User-Agent'); |
|
195
|
|
|
$lib->removeHeader('Connection'); |
|
196
|
|
|
return $lib; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
protected function prepareAuthBasic(): Http\Query |
|
200
|
|
|
{ |
|
201
|
|
|
$lib = new QueryAuthBasicMock(); |
|
202
|
|
|
$lib->setMethod('get'); |
|
203
|
|
|
$lib->setPath('/example'); |
|
204
|
|
|
$lib->setInline(false); |
|
205
|
|
|
$lib->setCredentials('foodid', 'buuggee'); |
|
206
|
|
|
$lib->removeHeader('Accept'); |
|
207
|
|
|
$lib->removeHeader('User-Agent'); |
|
208
|
|
|
$lib->removeHeader('Connection'); |
|
209
|
|
|
return $lib; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
protected function prepareAuthDigest(): Http\Query |
|
213
|
|
|
{ |
|
214
|
|
|
$lib = new QueryAuthDigestMock(); |
|
215
|
|
|
$lib->setMethod('get'); |
|
216
|
|
|
$lib->setPath('/dir/index.html'); |
|
217
|
|
|
$lib->setInline(false); |
|
218
|
|
|
$lib->setCredentials('Mufasa', 'Circle Of Life', '[email protected]'); |
|
219
|
|
|
$lib->setProperties('dcd98b7102dd2f0e8b11d0f600bfb0c093', '5ccc069c403ebaf9f0171e9517f40e41', 'auth'); |
|
220
|
|
|
$lib->removeHeader('Accept'); |
|
221
|
|
|
$lib->removeHeader('User-Agent'); |
|
222
|
|
|
$lib->removeHeader('Connection'); |
|
223
|
|
|
return $lib; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
protected function prepareTestFile($content): Http\Query\File |
|
227
|
|
|
{ |
|
228
|
|
|
$libValue = new Http\Query\File($content); |
|
229
|
|
|
$libValue->filename = 'dummy.txt'; |
|
230
|
|
|
$libValue->mime = 'text/plain'; |
|
231
|
|
|
return $libValue; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
protected function prepareProtocolSchema(string $host = 'unable.example', int $port = 80): Connection\Params\Tcp |
|
235
|
|
|
{ |
|
236
|
|
|
$request = new Connection\Params\Tcp(); |
|
237
|
|
|
return $request->setTarget($host, $port); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|