1<?php
2
3namespace Sabre\HTTP;
4
5class UtilTest extends \PHPUnit_Framework_TestCase {
6
7    function testParseHTTPDate() {
8
9        $times = [
10            'Wed, 13 Oct 2010 10:26:00 GMT',
11            'Wednesday, 13-Oct-10 10:26:00 GMT',
12            'Wed Oct 13 10:26:00 2010',
13        ];
14
15        $expected = 1286965560;
16
17        foreach ($times as $time) {
18            $result = Util::parseHTTPDate($time);
19            $this->assertEquals($expected, $result->format('U'));
20        }
21
22        $result = Util::parseHTTPDate('Wed Oct  6 10:26:00 2010');
23        $this->assertEquals(1286360760, $result->format('U'));
24
25    }
26
27    function testParseHTTPDateFail() {
28
29        $times = [
30            //random string
31            'NOW',
32            // not-GMT timezone
33            'Wednesday, 13-Oct-10 10:26:00 UTC',
34            // No space before the 6
35            'Wed Oct 6 10:26:00 2010',
36            // Invalid day
37            'Wed Oct  0 10:26:00 2010',
38            'Wed Oct 32 10:26:00 2010',
39            'Wed, 0 Oct 2010 10:26:00 GMT',
40            'Wed, 32 Oct 2010 10:26:00 GMT',
41            'Wednesday, 32-Oct-10 10:26:00 GMT',
42            // Invalid hour
43            'Wed, 13 Oct 2010 24:26:00 GMT',
44            'Wednesday, 13-Oct-10 24:26:00 GMT',
45            'Wed Oct 13 24:26:00 2010',
46        ];
47
48        foreach ($times as $time) {
49            $this->assertFalse(Util::parseHTTPDate($time), 'We used the string: ' . $time);
50        }
51
52    }
53
54    function testTimezones() {
55
56        $default = date_default_timezone_get();
57        date_default_timezone_set('Europe/Amsterdam');
58
59        $this->testParseHTTPDate();
60
61        date_default_timezone_set($default);
62
63    }
64
65    function testToHTTPDate() {
66
67        $dt = new \DateTime('2011-12-10 12:00:00 +0200');
68
69        $this->assertEquals(
70            'Sat, 10 Dec 2011 10:00:00 GMT',
71            Util::toHTTPDate($dt)
72        );
73
74    }
75
76    /**
77     * @dataProvider negotiateData
78     */
79    function testNegotiate($acceptHeader, $available, $expected) {
80
81        $this->assertEquals(
82            $expected,
83            Util::negotiate($acceptHeader, $available)
84        );
85
86    }
87
88    function negotiateData() {
89
90        return [
91            [ // simple
92                'application/xml',
93                ['application/xml'],
94                'application/xml',
95            ],
96            [ // no header
97                null,
98                ['application/xml'],
99                'application/xml',
100            ],
101            [ // 2 options
102                'application/json',
103                ['application/xml', 'application/json'],
104                'application/json',
105            ],
106            [ // 2 choices
107                'application/json, application/xml',
108                ['application/xml'],
109                'application/xml',
110            ],
111            [ // quality
112                'application/xml;q=0.2, application/json',
113                ['application/xml', 'application/json'],
114                'application/json',
115            ],
116            [ // wildcard
117                'image/jpeg, image/png, */*',
118                ['application/xml', 'application/json'],
119                'application/xml',
120            ],
121            [ // wildcard + quality
122                'image/jpeg, image/png; q=0.5, */*',
123                ['application/xml', 'application/json', 'image/png'],
124                'application/xml',
125            ],
126            [ // no match
127                'image/jpeg',
128                ['application/xml'],
129                null,
130            ],
131            [ // This is used in sabre/dav
132                'text/vcard; version=4.0',
133                [
134                    // Most often used mime-type. Version 3
135                    'text/x-vcard',
136                    // The correct standard mime-type. Defaults to version 3 as
137                    // well.
138                    'text/vcard',
139                    // vCard 4
140                    'text/vcard; version=4.0',
141                    // vCard 3
142                    'text/vcard; version=3.0',
143                    // jCard
144                    'application/vcard+json',
145                ],
146                'text/vcard; version=4.0',
147
148            ],
149            [ // rfc7231 example 1
150                'audio/*; q=0.2, audio/basic',
151                [
152                    'audio/pcm',
153                    'audio/basic',
154                ],
155                'audio/basic',
156            ],
157            [ // Lower quality after
158                'audio/pcm; q=0.2, audio/basic; q=0.1',
159                [
160                    'audio/pcm',
161                    'audio/basic',
162                ],
163                'audio/pcm',
164            ],
165            [ // Random parameter, should be ignored
166                'audio/pcm; hello; q=0.2, audio/basic; q=0.1',
167                [
168                    'audio/pcm',
169                    'audio/basic',
170                ],
171                'audio/pcm',
172            ],
173            [ // No whitepace after type, should pick the one that is the most specific.
174                'text/vcard;version=3.0, text/vcard',
175                [
176                    'text/vcard',
177                    'text/vcard; version=3.0'
178                ],
179                'text/vcard; version=3.0',
180            ],
181            [ // Same as last one, but order is different
182                'text/vcard, text/vcard;version=3.0',
183                [
184                    'text/vcard; version=3.0',
185                    'text/vcard',
186                ],
187                'text/vcard; version=3.0',
188            ],
189            [ // Charset should be ignored here.
190                'text/vcard; charset=utf-8; version=3.0, text/vcard',
191                [
192                    'text/vcard',
193                    'text/vcard; version=3.0'
194                ],
195                'text/vcard; version=3.0',
196            ],
197            [ // Undefined offset issue.
198                'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
199                ['application/xml', 'application/json', 'image/png'],
200                'application/xml',
201            ],
202
203        ];
204
205    }
206}
207