3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Protocol\HTTP;
24 use Friendica\Protocol\HTTP\MediaType;
26 class MediaTypeTest extends \PHPUnit\Framework\TestCase
28 public function dataValid(): array
32 'expected' => new MediaType('text', 'html', ['charset' => 'utf-8']),
33 'content-type' => 'text/html; charset=utf-8',
35 'HTML Northern Europe' => [
36 'expected' => new MediaType('text', 'html', ['charset' => 'ISO-8859-4']),
37 'content-type' => 'text/html; charset=ISO-8859-4',
39 'multipart/form-data' => [
40 'expected' => new MediaType('multipart', 'form-data', ['boundary' => '---------------------------974767299852498929531610575']),
41 'content-type' => 'multipart/form-data; boundary=---------------------------974767299852498929531610575',
43 'Multiple parameters' => [
44 'expected' => new MediaType('application', 'octet-stream', ['charset' => 'ISO-8859-4', 'another' => 'parameter']),
45 'content-type' => 'application/octet-stream; charset=ISO-8859-4 ; another=parameter',
48 'expected' => new MediaType('application', 'vnd.adobe.air-application-installer-package+zip'),
49 'content-type' => 'application/vnd.adobe.air-application-installer-package+zip',
51 'No parameters colon' => [
52 'expected' => new MediaType('application', 'vnd.adobe.air-application-installer-package+zip'),
53 'content-type' => 'application/vnd.adobe.air-application-installer-package+zip;',
55 'No parameters space colon' => [
56 'expected' => new MediaType('application', 'vnd.adobe.air-application-installer-package+zip'),
57 'content-type' => 'application/vnd.adobe.air-application-installer-package+zip ;',
59 'No parameters space colon space' => [
60 'expected' => new MediaType('application', 'vnd.adobe.air-application-installer-package+zip'),
61 'content-type' => 'application/vnd.adobe.air-application-installer-package+zip ; ',
63 'Parameter quoted string' => [
64 'expected' => new MediaType('text', 'html', ['parameter' => 'Quoted string with a space and a "double-quote"']),
65 'content-type' => 'text/html; parameter="Quoted string with a space and a \"double-quote\""',
71 * @dataProvider dataValid
73 * @param MediaType $expected
74 * @param string $contentType
77 public function testValid(MediaType $expected, string $contentType)
79 $this->assertEquals($expected, MediaType::fromContentType($contentType));
82 public function dataInvalid(): array
85 'no slash' => ['application'],
86 'two slashes' => ['application/octet/stream'],
87 'parameter no value' => ['application/octet-stream ; parameter'],
88 'parameter too many values' => ['application/octet-stream ; parameter=value1=value2'],
89 'type non token' => ['appli"cation/octet-stream'],
90 'subtype non token' => ['application/octet\-stream'],
91 'parameter name non token' => ['application/octet-stream; para"meter=value'],
92 'parameter value invalid' => ['application/octet-stream; parameter="value"value'],
97 * @dataProvider dataInvalid
99 * @param string $contentType
102 public function testInvalid(string $contentType)
104 $this->expectException(\InvalidArgumentException::class);
106 MediaType::fromContentType($contentType);
109 public function dataToString(): array
113 'content-type' => 'text/html; charset=utf-8',
114 'mediaType' => new MediaType('text', 'html', ['charset' => 'utf-8']),
116 'HTML Northern Europe' => [
117 'expected' => 'text/html; charset=ISO-8859-4',
118 'mediaType' => new MediaType('text', 'html', ['charset' => 'ISO-8859-4']),
120 'multipart/form-data' => [
121 'expected' => 'multipart/form-data; boundary=---------------------------974767299852498929531610575',
122 'mediaType' => new MediaType('multipart', 'form-data', ['boundary' => '---------------------------974767299852498929531610575']),
124 'Multiple parameters' => [
125 'expected' => 'application/octet-stream; charset=ISO-8859-4; another=parameter',
126 'mediaType' => new MediaType('application', 'octet-stream', ['charset' => 'ISO-8859-4', 'another' => 'parameter']),
129 'expected' => 'application/vnd.adobe.air-application-installer-package+zip',
130 'mediaType' => new MediaType('application', 'vnd.adobe.air-application-installer-package+zip'),
132 'Parameter quoted string' => [
133 'expected' => 'text/html; parameter="Quoted string with a space and a \"double-quote\""',
134 'mediaType' => new MediaType('text', 'html', ['parameter' => 'Quoted string with a space and a "double-quote"']),
140 * @dataProvider dataToString
142 * @param string $expected
143 * @param MediaType $mediaType
146 public function testToString(string $expected, MediaType $mediaType)
148 $this->assertEquals($expected, $mediaType->__toString());