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