Skip to content

Commit e6ec8d9

Browse files
committed
:octocat:
1 parent c47ecf5 commit e6ec8d9

File tree

6 files changed

+304
-167
lines changed

6 files changed

+304
-167
lines changed

tests/Psr17/FactoryHelpersTest.php

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
/**
3+
* Class FactoryHelpersTest
4+
*
5+
* @filesource FactoryHelpersTest.php
6+
* @created 31.01.2019
7+
* @package chillerlan\HTTPTest\Psr17
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2019 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\HTTPTest\Psr17;
14+
15+
use chillerlan\HTTP\Psr17;
16+
use chillerlan\HTTP\Psr7\{UploadedFile, Uri};
17+
use PHPUnit\Framework\TestCase;
18+
use Psr\Http\Message\StreamInterface;
19+
20+
class FactoryHelpersTest extends TestCase{
21+
22+
public function dataGetUriFromGlobals(){
23+
24+
$server = [
25+
'REQUEST_URI' => '/blog/article.php?id=10&user=foo',
26+
'SERVER_PORT' => '443',
27+
'SERVER_ADDR' => '217.112.82.20',
28+
'SERVER_NAME' => 'www.example.org',
29+
'SERVER_PROTOCOL' => 'HTTP/1.1',
30+
'REQUEST_METHOD' => 'POST',
31+
'QUERY_STRING' => 'id=10&user=foo',
32+
'DOCUMENT_ROOT' => '/path/to/your/server/root/',
33+
'HTTP_HOST' => 'www.example.org',
34+
'HTTPS' => 'on',
35+
'REMOTE_ADDR' => '193.60.168.69',
36+
'REMOTE_PORT' => '5390',
37+
'SCRIPT_NAME' => '/blog/article.php',
38+
'SCRIPT_FILENAME' => '/path/to/your/server/root/blog/article.php',
39+
'PHP_SELF' => '/blog/article.php',
40+
'REQUEST_TIME' => time(), // phpunit fix
41+
];
42+
43+
return [
44+
'HTTPS request' => [
45+
'https://www.example.org/blog/article.php?id=10&user=foo',
46+
$server,
47+
],
48+
'HTTPS request with different on value' => [
49+
'https://www.example.org/blog/article.php?id=10&user=foo',
50+
array_merge($server, ['HTTPS' => '1']),
51+
],
52+
'HTTP request' => [
53+
'http://www.example.org/blog/article.php?id=10&user=foo',
54+
array_merge($server, ['HTTPS' => 'off', 'SERVER_PORT' => '80']),
55+
],
56+
'HTTP_HOST missing -> fallback to SERVER_NAME' => [
57+
'https://www.example.org/blog/article.php?id=10&user=foo',
58+
array_merge($server, ['HTTP_HOST' => null]),
59+
],
60+
'HTTP_HOST and SERVER_NAME missing -> fallback to SERVER_ADDR' => [
61+
'https://217.112.82.20/blog/article.php?id=10&user=foo',
62+
array_merge($server, ['HTTP_HOST' => null, 'SERVER_NAME' => null]),
63+
],
64+
'No query String' => [
65+
'https://www.example.org/blog/article.php',
66+
array_merge($server, ['REQUEST_URI' => '/blog/article.php', 'QUERY_STRING' => '']),
67+
],
68+
'Host header with port' => [
69+
'https://www.example.org:8324/blog/article.php?id=10&user=foo',
70+
array_merge($server, ['HTTP_HOST' => 'www.example.org:8324']),
71+
],
72+
'Different port with SERVER_PORT' => [
73+
'https://www.example.org:8324/blog/article.php?id=10&user=foo',
74+
array_merge($server, ['SERVER_PORT' => '8324']),
75+
],
76+
'REQUEST_URI missing query string' => [
77+
'https://www.example.org/blog/article.php?id=10&user=foo',
78+
array_merge($server, ['REQUEST_URI' => '/blog/article.php']),
79+
],
80+
'Empty server variable' => [
81+
'http://localhost',
82+
['REQUEST_TIME' => time(), 'SCRIPT_NAME' => '/blog/article.php'], // phpunit fix
83+
],
84+
];
85+
}
86+
87+
/**
88+
* @dataProvider dataGetUriFromGlobals
89+
*
90+
* @param string $expected
91+
* @param array $serverParams
92+
*/
93+
public function testCreateUriFromGlobals(string $expected, array $serverParams){
94+
$_SERVER = $serverParams;
95+
96+
$this->assertEquals(new Uri($expected), Psr17\create_uri_from_globals());
97+
}
98+
99+
public function testCreateServerRequestFromGlobals(){
100+
101+
$_SERVER = [
102+
'REQUEST_URI' => '/blog/article.php?id=10&user=foo',
103+
'SERVER_PORT' => '443',
104+
'SERVER_ADDR' => '217.112.82.20',
105+
'SERVER_NAME' => 'www.example.org',
106+
'SERVER_PROTOCOL' => 'HTTP/1.1',
107+
'REQUEST_METHOD' => 'POST',
108+
'QUERY_STRING' => 'id=10&user=foo',
109+
'DOCUMENT_ROOT' => '/path/to/your/server/root/',
110+
'HTTP_HOST' => 'www.example.org',
111+
'HTTPS' => 'on',
112+
'REMOTE_ADDR' => '193.60.168.69',
113+
'REMOTE_PORT' => '5390',
114+
'SCRIPT_NAME' => '/blog/article.php',
115+
'SCRIPT_FILENAME' => '/path/to/your/server/root/blog/article.php',
116+
'PHP_SELF' => '/blog/article.php',
117+
'REQUEST_TIME' => time(), // phpunit fix
118+
];
119+
120+
$_COOKIE = [
121+
'logged-in' => 'yes!'
122+
];
123+
124+
$_POST = [
125+
'name' => 'Pesho',
126+
'email' => 'pesho@example.com',
127+
];
128+
129+
$_GET = [
130+
'id' => 10,
131+
'user' => 'foo',
132+
];
133+
134+
$_FILES = [
135+
'file' => [
136+
'name' => 'MyFile.txt',
137+
'type' => 'text/plain',
138+
'tmp_name' => '/tmp/php/php1h4j1o',
139+
'error' => UPLOAD_ERR_OK,
140+
'size' => 123,
141+
]
142+
];
143+
144+
$server = Psr17\create_server_request_from_globals();
145+
146+
$this->assertSame('POST', $server->getMethod());
147+
$this->assertEquals(['Host' => ['www.example.org']], $server->getHeaders());
148+
$this->assertSame('', (string) $server->getBody());
149+
$this->assertSame('1.1', $server->getProtocolVersion());
150+
$this->assertEquals($_COOKIE, $server->getCookieParams());
151+
$this->assertEquals($_POST, $server->getParsedBody());
152+
$this->assertEquals($_GET, $server->getQueryParams());
153+
154+
$this->assertEquals(
155+
new Uri('https://www.example.org/blog/article.php?id=10&user=foo'),
156+
$server->getUri()
157+
);
158+
159+
$expectedFiles = [
160+
'file' => new UploadedFile(
161+
'/tmp/php/php1h4j1o',
162+
123,
163+
UPLOAD_ERR_OK,
164+
'MyFile.txt',
165+
'text/plain'
166+
),
167+
];
168+
169+
$this->assertEquals($expectedFiles, $server->getUploadedFiles());
170+
}
171+
172+
public function testCreateStream(){
173+
$stream = Psr17\create_stream('test');
174+
175+
$this->assertInstanceOf(Streaminterface::class, $stream);
176+
$this->assertSame('test', $stream->getContents());
177+
}
178+
179+
public function streamInputProvider(){
180+
181+
$fh = fopen('php://temp', 'r+');
182+
fwrite($fh, 'resourcetest');
183+
fseek($fh, 0);
184+
185+
$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><root><foo>bar</foo></root>');
186+
187+
return [
188+
'string' => ['stringtest', 'stringtest'],
189+
'file' => [__DIR__.'/streaminput.txt', 'filetest'."\r\n"],
190+
'resource' => [$fh, 'resourcetest'],
191+
'streaminterface' => [Psr17\create_stream('streaminterfacetest'), 'streaminterfacetest'],
192+
'tostring' => [$xml->foo, 'bar'],
193+
];
194+
}
195+
196+
/**
197+
* @dataProvider streamInputProvider
198+
*
199+
* @param $input
200+
* @param string $content
201+
*/
202+
public function testCreateStreamFromInput($input, string $content){
203+
$this->assertSame($content, Psr17\create_stream_from_input($input)->getContents());
204+
}
205+
206+
/**
207+
* @expectedException \InvalidArgumentException
208+
* @expectedExceptionMessage Invalid resource type: object
209+
*/
210+
public function testCreateStreamFromInputException(){
211+
Psr17\create_stream_from_input(new \stdClass());
212+
}
213+
214+
}

tests/Psr17/streaminput.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
filetest

tests/Psr7/MessageHelpersTest.php

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
namespace chillerlan\HTTPTest\Psr7;
1414

15+
use chillerlan\HTTP\{Psr7, Psr7\Request, Psr7\Response, Psr17};
1516
use PHPUnit\Framework\TestCase;
16-
use chillerlan\HTTP\Psr7;
17+
use Psr\Http\Message\{MessageInterface, ResponseInterface};
1718

1819
class MessageHelpersTest extends TestCase{
1920

@@ -24,7 +25,8 @@ public function headerDataProvider():array {
2425
'UPPERCASEKEY' => [['UPPERCASEKEY' => 'UPPERCASEVALUE'], ['uppercasekey' => 'UPPERCASEVALUE']],
2526
'mIxEdCaSeKey' => [['mIxEdCaSeKey' => 'MiXeDcAsEvAlUe'], ['mixedcasekey' => 'MiXeDcAsEvAlUe']],
2627
'31i71casekey' => [['31i71casekey' => '31i71casevalue'], ['31i71casekey' => '31i71casevalue']],
27-
'numericvalue' => [[1 => 'numericvalue:1'], ['numericvalue' => '1']],
28+
'numericvalue' => [['numericvalue:1'], ['numericvalue' => '1']],
29+
'arrayvalue' => [[['foo' => 'bar']], ['foo' => 'bar']],
2830
'invalid: 2' => [[2 => 2], []],
2931
'invalid: what' => [['what'], []],
3032
];
@@ -40,20 +42,31 @@ public function testNormalizeHeaders(array $header, array $normalized){
4042
$this->assertSame($normalized, Psr7\normalize_request_headers($header));
4143
}
4244

43-
public function testCheckParams(){
44-
$data = ['foo' => 'bar', 'whatever' => null, 'nope' => '', 'true' => true, 'false' => false];
45+
public function queryParamDataProvider(){
46+
return [
47+
// don't remove empty values
48+
'BOOLEANS_AS_BOOL (clean)' => [['whatever' => null, 'nope' => '', 'true' => true, 'false' => false, 'array' => ['value' => false]], Psr7\BOOLEANS_AS_BOOL, false],
49+
// bool cast to types
50+
'BOOLEANS_AS_BOOL' => [['true' => true, 'false' => false, 'array' => ['value' => false]], Psr7\BOOLEANS_AS_BOOL, true],
51+
'BOOLEANS_AS_INT' => [['true' => 1, 'false' => 0, 'array' => ['value' => 0]], Psr7\BOOLEANS_AS_INT, true],
52+
'BOOLEANS_AS_INT_STRING' => [['true' => '1', 'false' => '0', 'array' => ['value' => '0']], Psr7\BOOLEANS_AS_INT_STRING, true],
53+
'BOOLEANS_AS_STRING' => [['true' => 'true', 'false' => 'false', 'array' => ['value' => 'false']], Psr7\BOOLEANS_AS_STRING, true],
54+
];
55+
}
4556

46-
// don't remove empty values
47-
$this->assertSame(['foo' => 'bar', 'whatever' => null, 'nope' => '', 'true' => true, 'false' => false], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_BOOL, false));
57+
/**
58+
* @dataProvider queryParamDataProvider
59+
*
60+
* @param array $expected
61+
* @param int $bool_cast
62+
* @param bool $remove_empty
63+
*/
64+
public function testCleanQueryParams(array $expected, int $bool_cast, bool $remove_empty){
65+
$data = ['whatever' => null, 'nope' => '', 'true' => true, 'false' => false, 'array' => ['value' => false]];
4866

49-
// bool cast to types
50-
$this->assertSame(['foo' => 'bar', 'true' => true, 'false' => false], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_BOOL));
51-
$this->assertSame(['foo' => 'bar', 'true' => 1, 'false' => 0], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_INT));
52-
$this->assertSame(['foo' => 'bar', 'true' => '1', 'false' => '0'], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_INT_STRING));
53-
$this->assertSame(['foo' => 'bar', 'true' => 'true', 'false' => 'false'], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_STRING));
67+
$this->assertSame($expected, Psr7\clean_query_params($data, $bool_cast, $remove_empty));
5468
}
5569

56-
5770
public function rawurlencodeDataProvider(){
5871
return [
5972
'string' => ['some test string!', 'some%20test%20string%21'],
@@ -68,7 +81,7 @@ public function rawurlencodeDataProvider(){
6881
* @param $expected
6982
*/
7083
public function testRawurlencode($data, $expected){
71-
$this->assertSame($expected, Psr7\raw_urlencode($data));
84+
$this->assertSame($expected, Psr7\r_rawurlencode($data));
7285
}
7386

7487
public function testBuildHttpQuery(){
@@ -85,4 +98,40 @@ public function testBuildHttpQuery(){
8598
$this->assertSame('florps="nah", florps="nope", florps="nope", foo="bar", whatever?="nope!"', Psr7\build_http_query($data, false, ', ', '"'));
8699
}
87100

101+
public function testGetJSON(){
102+
103+
$r = (new Response)
104+
->withBody(Psr17\create_stream('{"foo":"bar"}'));
105+
106+
$this->assertSame('bar', Psr7\get_json($r)->foo);
107+
}
108+
109+
public function testGetXML(){
110+
111+
$r = (new Response)
112+
->withBody(Psr17\create_stream('<?xml version="1.0" encoding="UTF-8"?><root><foo>bar</foo></root>'));
113+
114+
$this->assertSame('bar', Psr7\get_xml($r)->foo->__toString());
115+
}
116+
117+
public function messageDataProvider(){
118+
return [
119+
'Request' => [new Request('GET', 'https://localhost/foo'), 'GET /foo HTTP/1.1'."\r\n".'Host: localhost'."\r\n".'foo: bar'."\r\n\r\n".'testbody'],
120+
'Response' => [new Response, 'HTTP/1.1 200 OK'."\r\n".'foo: bar'."\r\n\r\n".'testbody'],
121+
];
122+
}
123+
124+
/**
125+
* @dataProvider messageDataProvider
126+
*
127+
* @param \Psr\Http\Message\MessageInterface $message
128+
* @param string $expected
129+
*/
130+
public function testMessageToString(MessageInterface $message, string $expected){
131+
$this->assertSame(
132+
$expected,
133+
Psr7\message_to_string($message->withAddedHeader('foo', 'bar')->withBody(Psr17\create_stream('testbody')))
134+
);
135+
}
136+
88137
}

tests/Psr7/MultipartStreamTest.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,27 @@ public function testCanProvideBoundary(){
2727
$this->assertSame('foo', (new MultipartStream([], 'foo'))->getBoundary());
2828
}
2929

30-
public function testIsNotWritable(){
31-
$this->assertFalse((new MultipartStream)->isWritable());
30+
public function testIsAlwaysReadableNotWritable(){
31+
$s = new MultipartStream;
32+
33+
$this->assertTrue($s->isReadable());
34+
$this->assertFalse($s->isWritable());
35+
}
36+
37+
/**
38+
* @expectedException \RuntimeException
39+
* @expectedExceptionMessage Cannot write to a MultipartStream, use the "addElement" method instead.
40+
*/
41+
public function testWriteError(){
42+
(new MultipartStream)->write('foo');
43+
}
44+
45+
/**
46+
* @expectedException \RuntimeException
47+
* @expectedExceptionMessage Stream already built
48+
*/
49+
public function testAlreadyBuiltError(){
50+
(new MultipartStream)->build()->addElement([]);
3251
}
3352

3453
public function testCanCreateEmptyStream(){
@@ -40,15 +59,17 @@ public function testCanCreateEmptyStream(){
4059

4160
/**
4261
* @expectedException \InvalidArgumentException
62+
* @expectedExceptionMessage A "contents" element is required
4363
*/
44-
public function testValidatesFilesArrayElement(){
64+
public function testEnsureContentsElement(){
4565
new MultipartStream([['foo' => 'bar']]);
4666
}
4767

4868
/**
4969
* @expectedException \InvalidArgumentException
70+
* @expectedExceptionMessage A "name" element is required
5071
*/
51-
public function testEnsuresFileHasName(){
72+
public function testEnsureNameElement(){
5273
new MultipartStream([['contents' => 'bar']]);
5374
}
5475

@@ -61,7 +82,8 @@ public function testSerializesFields(){
6182
$this->assertEquals(
6283
"--boundary\r\nContent-Disposition: form-data; name=\"foo\"\r\nContent-Length: 3\r\n\r\nbar\r\n".
6384
"--boundary\r\nContent-Disposition: form-data; name=\"baz\"\r\nContent-Length: 3\r\n\r\nbam\r\n".
64-
"--boundary--\r\n", (string)$stream
85+
"--boundary--\r\n",
86+
(string)$stream
6587
);
6688
}
6789

0 commit comments

Comments
 (0)