2021-05-23 19:58:09 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Test\src\Util;
|
|
|
|
|
|
|
|
use Friendica\Test\MockedTest;
|
|
|
|
use Friendica\Test\Util\HTTPInputDataDouble;
|
|
|
|
use Friendica\Util\HTTPInputData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Testing HTTPInputData
|
2021-05-23 22:40:41 +02:00
|
|
|
*
|
2021-05-23 19:58:09 +02:00
|
|
|
* @see HTTPInputData
|
|
|
|
*/
|
|
|
|
class HTTPInputDataTest extends MockedTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns the data stream for the unit test
|
|
|
|
* Each array element of the first hierarchy represents one test run
|
|
|
|
* Each array element of the second hierarchy represents the parameters, passed to the test function
|
2021-05-23 22:40:41 +02:00
|
|
|
*
|
2021-05-23 19:58:09 +02:00
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function dataStream()
|
|
|
|
{
|
|
|
|
return [
|
2021-05-24 04:33:42 +00:00
|
|
|
'multipart' => [
|
2021-05-23 19:50:02 +00:00
|
|
|
'contenttype' => 'multipart/form-data;boundary=43395968-f65c-437e-b536-5b33e3e3c7e5;charset=utf8',
|
2021-05-24 04:33:42 +00:00
|
|
|
'input' => file_get_contents(__DIR__ . '/../../datasets/http/multipart.httpinput'),
|
2021-05-23 19:50:02 +00:00
|
|
|
'expected' => [
|
2021-05-23 19:58:09 +02:00
|
|
|
'variables' => [
|
2021-05-23 19:37:31 +00:00
|
|
|
'display_name' => 'User Name',
|
|
|
|
'note' => 'About me',
|
2021-05-23 19:59:51 +00:00
|
|
|
'locked' => 'false',
|
2021-05-23 19:34:28 +00:00
|
|
|
'fields_attributes' => [
|
|
|
|
0 => [
|
2021-05-23 19:37:31 +00:00
|
|
|
'name' => 'variable 1',
|
2021-05-23 19:34:28 +00:00
|
|
|
'value' => 'value 1',
|
|
|
|
],
|
|
|
|
1 => [
|
2021-05-23 19:37:31 +00:00
|
|
|
'name' => 'variable 2',
|
2021-05-23 19:34:28 +00:00
|
|
|
'value' => 'value 2',
|
|
|
|
]
|
|
|
|
]
|
2021-05-23 19:58:09 +02:00
|
|
|
],
|
|
|
|
'files' => []
|
|
|
|
]
|
2021-05-24 04:33:42 +00:00
|
|
|
],
|
|
|
|
'form-urlencoded' => [
|
|
|
|
'contenttype' => 'application/x-www-form-urlencoded;charset=utf8',
|
|
|
|
'input' => file_get_contents(__DIR__ . '/../../datasets/http/form-urlencoded.httpinput'),
|
|
|
|
'expected' => [
|
|
|
|
'variables' => [
|
|
|
|
'title' => 'Test2',
|
|
|
|
],
|
|
|
|
'files' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'form-urlencoded-json' => [
|
|
|
|
'contenttype' => 'application/x-www-form-urlencoded;charset=utf8',
|
|
|
|
'input' => file_get_contents(__DIR__ . '/../../datasets/http/form-urlencoded-json.httpinput'),
|
|
|
|
'expected' => [
|
|
|
|
'variables' => [
|
2021-05-24 04:45:24 +00:00
|
|
|
'media_ids' => [],
|
2021-05-24 04:36:30 +00:00
|
|
|
'sensitive' => false,
|
2021-05-24 04:33:42 +00:00
|
|
|
'status' => 'Test Status',
|
|
|
|
'visibility' => 'private',
|
|
|
|
'spoiler_text' => 'Title'
|
|
|
|
],
|
|
|
|
'files' => []
|
|
|
|
]
|
2021-05-23 19:58:09 +02:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the HTTPInputData::process() method
|
2021-05-23 22:40:41 +02:00
|
|
|
*
|
|
|
|
* @param string $contentType The content typer of the transmitted data
|
2021-05-23 19:50:02 +00:00
|
|
|
* @param string $input The input, we got from the data stream
|
|
|
|
* @param array $expected The expected output
|
2021-05-23 22:40:41 +02:00
|
|
|
*
|
2021-05-23 19:58:09 +02:00
|
|
|
* @dataProvider dataStream
|
2021-05-23 22:40:41 +02:00
|
|
|
* @see HTTPInputData::process()
|
2021-05-23 19:58:09 +02:00
|
|
|
*/
|
2021-05-23 22:40:41 +02:00
|
|
|
public function testHttpInput(string $contentType, string $input, array $expected)
|
2021-05-23 19:58:09 +02:00
|
|
|
{
|
2021-05-23 22:40:41 +02:00
|
|
|
HTTPInputDataDouble::setPhpInputContentType($contentType);
|
2021-05-23 19:58:09 +02:00
|
|
|
HTTPInputDataDouble::setPhpInputContent($input);
|
|
|
|
$stream = fopen('php://memory', 'r+');
|
|
|
|
fwrite($stream, $input);
|
|
|
|
rewind($stream);
|
|
|
|
|
|
|
|
HTTPInputDataDouble::setPhpInputStream($stream);
|
|
|
|
$output = HTTPInputDataDouble::process();
|
2021-05-23 20:30:27 +02:00
|
|
|
$this->assertEqualsCanonicalizing($expected, $output);
|
2021-05-23 19:58:09 +02:00
|
|
|
}
|
|
|
|
}
|