]> git.mxchange.org Git - friendica.git/blob - tests/src/Protocol/HTTP/MediaTypeTest.php
Merge pull request #12697 from MrPetovan/bug/deprecated
[friendica.git] / tests / src / Protocol / HTTP / MediaTypeTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Protocol\HTTP;
23
24 use Friendica\Protocol\HTTP\MediaType;
25
26 class MediaTypeTest extends \PHPUnit\Framework\TestCase
27 {
28         public function dataValid(): array
29         {
30                 return [
31                         'HTML UTF-8' => [
32                                 'expected'     => new MediaType('text', 'html', ['charset' => 'utf-8']),
33                                 'content-type' => 'text/html; charset=utf-8',
34                         ],
35                         'HTML Northern Europe' => [
36                                 'expected'     => new MediaType('text', 'html', ['charset' => 'ISO-8859-4']),
37                                 'content-type' => 'text/html; charset=ISO-8859-4',
38                         ],
39                         'multipart/form-data' => [
40                                 'expected'     => new MediaType('multipart', 'form-data', ['boundary' => '---------------------------974767299852498929531610575']),
41                                 'content-type' => 'multipart/form-data; boundary=---------------------------974767299852498929531610575',
42                         ],
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',
46                         ],
47                         'No parameters' => [
48                                 'expected'     => new MediaType('application', 'vnd.adobe.air-application-installer-package+zip'),
49                                 'content-type' => 'application/vnd.adobe.air-application-installer-package+zip',
50                         ],
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;',
54                         ],
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 ;',
58                         ],
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 ; ',
62                         ],
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\""',
66                         ]
67                 ];
68         }
69
70         /**
71          * @dataProvider dataValid
72          *
73          * @param MediaType $expected
74          * @param string    $contentType
75          * @return void
76          */
77         public function testValid(MediaType $expected, string $contentType)
78         {
79                 $this->assertEquals($expected, MediaType::fromContentType($contentType));
80         }
81
82         public function dataInvalid(): array
83         {
84                 return [
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'],
93                 ];
94         }
95
96         /**
97          * @dataProvider dataInvalid
98          *
99          * @param string $contentType
100          * @return void
101          */
102         public function testInvalid(string $contentType)
103         {
104                 $this->expectException(\InvalidArgumentException::class);
105
106                 MediaType::fromContentType($contentType);
107         }
108
109         public function dataToString(): array
110         {
111                 return [
112                         'HTML UTF-8' => [
113                                 'content-type' => 'text/html; charset=utf-8',
114                                 'mediaType'    => new MediaType('text', 'html', ['charset' => 'utf-8']),
115                         ],
116                         'HTML Northern Europe' => [
117                                 'expected'  => 'text/html; charset=ISO-8859-4',
118                                 'mediaType' => new MediaType('text', 'html', ['charset' => 'ISO-8859-4']),
119                         ],
120                         'multipart/form-data' => [
121                                 'expected'  => 'multipart/form-data; boundary=---------------------------974767299852498929531610575',
122                                 'mediaType' => new MediaType('multipart', 'form-data', ['boundary' => '---------------------------974767299852498929531610575']),
123                         ],
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']),
127                         ],
128                         'No parameters' => [
129                                 'expected'  => 'application/vnd.adobe.air-application-installer-package+zip',
130                                 'mediaType' => new MediaType('application', 'vnd.adobe.air-application-installer-package+zip'),
131                         ],
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"']),
135                         ],
136                 ];
137         }
138
139         /**
140          * @dataProvider dataToString
141          *
142          * @param string    $expected
143          * @param MediaType $mediaType
144          * @return void
145          */
146         public function testToString(string $expected, MediaType $mediaType)
147         {
148                 $this->assertEquals($expected, $mediaType->__toString());
149         }
150 }