xref: /plugin/davcal/vendor/sabre/dav/tests/Sabre/DAV/HTTPPreferParsingTest.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\DAV;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\HTTP;
6*a1a3b679SAndreas Boehler
7*a1a3b679SAndreas Boehlerclass HTTPPreferParsingTest extends \Sabre\DAVServerTest {
8*a1a3b679SAndreas Boehler
9*a1a3b679SAndreas Boehler    function testParseSimple() {
10*a1a3b679SAndreas Boehler
11*a1a3b679SAndreas Boehler        $httpRequest = HTTP\Sapi::createFromServerArray([
12*a1a3b679SAndreas Boehler            'HTTP_PREFER' => 'return-asynch',
13*a1a3b679SAndreas Boehler        ]);
14*a1a3b679SAndreas Boehler
15*a1a3b679SAndreas Boehler        $server = new Server();
16*a1a3b679SAndreas Boehler        $server->httpRequest = $httpRequest;
17*a1a3b679SAndreas Boehler
18*a1a3b679SAndreas Boehler        $this->assertEquals([
19*a1a3b679SAndreas Boehler            'respond-async' => true,
20*a1a3b679SAndreas Boehler            'return'        => null,
21*a1a3b679SAndreas Boehler            'handling'      => null,
22*a1a3b679SAndreas Boehler            'wait'          => null,
23*a1a3b679SAndreas Boehler        ], $server->getHTTPPrefer());
24*a1a3b679SAndreas Boehler
25*a1a3b679SAndreas Boehler    }
26*a1a3b679SAndreas Boehler
27*a1a3b679SAndreas Boehler    function testParseValue() {
28*a1a3b679SAndreas Boehler
29*a1a3b679SAndreas Boehler        $httpRequest = HTTP\Sapi::createFromServerArray([
30*a1a3b679SAndreas Boehler            'HTTP_PREFER' => 'wait=10',
31*a1a3b679SAndreas Boehler        ]);
32*a1a3b679SAndreas Boehler
33*a1a3b679SAndreas Boehler        $server = new Server();
34*a1a3b679SAndreas Boehler        $server->httpRequest = $httpRequest;
35*a1a3b679SAndreas Boehler
36*a1a3b679SAndreas Boehler        $this->assertEquals([
37*a1a3b679SAndreas Boehler            'respond-async' => false,
38*a1a3b679SAndreas Boehler            'return'        => null,
39*a1a3b679SAndreas Boehler            'handling'      => null,
40*a1a3b679SAndreas Boehler            'wait'          => '10',
41*a1a3b679SAndreas Boehler        ], $server->getHTTPPrefer());
42*a1a3b679SAndreas Boehler
43*a1a3b679SAndreas Boehler    }
44*a1a3b679SAndreas Boehler
45*a1a3b679SAndreas Boehler    function testParseMultiple() {
46*a1a3b679SAndreas Boehler
47*a1a3b679SAndreas Boehler        $httpRequest = HTTP\Sapi::createFromServerArray([
48*a1a3b679SAndreas Boehler            'HTTP_PREFER' => 'return-minimal, strict,lenient',
49*a1a3b679SAndreas Boehler        ]);
50*a1a3b679SAndreas Boehler
51*a1a3b679SAndreas Boehler        $server = new Server();
52*a1a3b679SAndreas Boehler        $server->httpRequest = $httpRequest;
53*a1a3b679SAndreas Boehler
54*a1a3b679SAndreas Boehler        $this->assertEquals([
55*a1a3b679SAndreas Boehler            'respond-async' => false,
56*a1a3b679SAndreas Boehler            'return'        => 'minimal',
57*a1a3b679SAndreas Boehler            'handling'      => 'lenient',
58*a1a3b679SAndreas Boehler            'wait'          => null,
59*a1a3b679SAndreas Boehler        ], $server->getHTTPPrefer());
60*a1a3b679SAndreas Boehler
61*a1a3b679SAndreas Boehler    }
62*a1a3b679SAndreas Boehler
63*a1a3b679SAndreas Boehler    function testParseWeirdValue() {
64*a1a3b679SAndreas Boehler
65*a1a3b679SAndreas Boehler        $httpRequest = HTTP\Sapi::createFromServerArray([
66*a1a3b679SAndreas Boehler            'HTTP_PREFER' => 'BOOOH',
67*a1a3b679SAndreas Boehler        ]);
68*a1a3b679SAndreas Boehler
69*a1a3b679SAndreas Boehler        $server = new Server();
70*a1a3b679SAndreas Boehler        $server->httpRequest = $httpRequest;
71*a1a3b679SAndreas Boehler
72*a1a3b679SAndreas Boehler        $this->assertEquals([
73*a1a3b679SAndreas Boehler            'respond-async' => false,
74*a1a3b679SAndreas Boehler            'return'        => null,
75*a1a3b679SAndreas Boehler            'handling'      => null,
76*a1a3b679SAndreas Boehler            'wait'          => null,
77*a1a3b679SAndreas Boehler            'boooh'         => true,
78*a1a3b679SAndreas Boehler        ], $server->getHTTPPrefer());
79*a1a3b679SAndreas Boehler
80*a1a3b679SAndreas Boehler    }
81*a1a3b679SAndreas Boehler
82*a1a3b679SAndreas Boehler    function testBrief() {
83*a1a3b679SAndreas Boehler
84*a1a3b679SAndreas Boehler        $httpRequest = HTTP\Sapi::createFromServerArray([
85*a1a3b679SAndreas Boehler            'HTTP_BRIEF' => 't',
86*a1a3b679SAndreas Boehler        ]);
87*a1a3b679SAndreas Boehler
88*a1a3b679SAndreas Boehler        $server = new Server();
89*a1a3b679SAndreas Boehler        $server->httpRequest = $httpRequest;
90*a1a3b679SAndreas Boehler
91*a1a3b679SAndreas Boehler        $this->assertEquals([
92*a1a3b679SAndreas Boehler            'respond-async' => false,
93*a1a3b679SAndreas Boehler            'return'        => 'minimal',
94*a1a3b679SAndreas Boehler            'handling'      => null,
95*a1a3b679SAndreas Boehler            'wait'          => null,
96*a1a3b679SAndreas Boehler        ], $server->getHTTPPrefer());
97*a1a3b679SAndreas Boehler
98*a1a3b679SAndreas Boehler    }
99*a1a3b679SAndreas Boehler
100*a1a3b679SAndreas Boehler    /**
101*a1a3b679SAndreas Boehler     * propfindMinimal
102*a1a3b679SAndreas Boehler     *
103*a1a3b679SAndreas Boehler     * @return void
104*a1a3b679SAndreas Boehler     */
105*a1a3b679SAndreas Boehler    function testpropfindMinimal() {
106*a1a3b679SAndreas Boehler
107*a1a3b679SAndreas Boehler        $request = HTTP\Sapi::createFromServerArray([
108*a1a3b679SAndreas Boehler            'REQUEST_METHOD' => 'PROPFIND',
109*a1a3b679SAndreas Boehler            'REQUEST_URI'    => '/',
110*a1a3b679SAndreas Boehler            'HTTP_PREFER'    => 'return-minimal',
111*a1a3b679SAndreas Boehler        ]);
112*a1a3b679SAndreas Boehler        $request->setBody(<<<BLA
113*a1a3b679SAndreas Boehler<?xml version="1.0"?>
114*a1a3b679SAndreas Boehler<d:propfind xmlns:d="DAV:">
115*a1a3b679SAndreas Boehler    <d:prop>
116*a1a3b679SAndreas Boehler        <d:something />
117*a1a3b679SAndreas Boehler        <d:resourcetype />
118*a1a3b679SAndreas Boehler    </d:prop>
119*a1a3b679SAndreas Boehler</d:propfind>
120*a1a3b679SAndreas BoehlerBLA
121*a1a3b679SAndreas Boehler        );
122*a1a3b679SAndreas Boehler
123*a1a3b679SAndreas Boehler        $response = $this->request($request);
124*a1a3b679SAndreas Boehler
125*a1a3b679SAndreas Boehler        $body = $response->getBodyAsString();
126*a1a3b679SAndreas Boehler
127*a1a3b679SAndreas Boehler        $this->assertEquals(207, $response->getStatus(), $body);
128*a1a3b679SAndreas Boehler
129*a1a3b679SAndreas Boehler        $this->assertTrue(strpos($body, 'resourcetype') !== false, $body);
130*a1a3b679SAndreas Boehler        $this->assertTrue(strpos($body, 'something') === false, $body);
131*a1a3b679SAndreas Boehler
132*a1a3b679SAndreas Boehler    }
133*a1a3b679SAndreas Boehler
134*a1a3b679SAndreas Boehler    function testproppatchMinimal() {
135*a1a3b679SAndreas Boehler
136*a1a3b679SAndreas Boehler        $request = new HTTP\Request('PROPPATCH', '/', ['Prefer' => 'return-minimal']);
137*a1a3b679SAndreas Boehler        $request->setBody(<<<BLA
138*a1a3b679SAndreas Boehler<?xml version="1.0"?>
139*a1a3b679SAndreas Boehler<d:propertyupdate xmlns:d="DAV:">
140*a1a3b679SAndreas Boehler    <d:set>
141*a1a3b679SAndreas Boehler        <d:prop>
142*a1a3b679SAndreas Boehler            <d:something>nope!</d:something>
143*a1a3b679SAndreas Boehler        </d:prop>
144*a1a3b679SAndreas Boehler    </d:set>
145*a1a3b679SAndreas Boehler</d:propertyupdate>
146*a1a3b679SAndreas BoehlerBLA
147*a1a3b679SAndreas Boehler        );
148*a1a3b679SAndreas Boehler
149*a1a3b679SAndreas Boehler        $this->server->on('propPatch', function($path, PropPatch $propPatch) {
150*a1a3b679SAndreas Boehler
151*a1a3b679SAndreas Boehler            $propPatch->handle('{DAV:}something', function($props) {
152*a1a3b679SAndreas Boehler                return true;
153*a1a3b679SAndreas Boehler            });
154*a1a3b679SAndreas Boehler
155*a1a3b679SAndreas Boehler        });
156*a1a3b679SAndreas Boehler
157*a1a3b679SAndreas Boehler        $response = $this->request($request);
158*a1a3b679SAndreas Boehler
159*a1a3b679SAndreas Boehler        $this->assertEquals(0, strlen($response->body), 'Expected empty body: ' . $response->body);
160*a1a3b679SAndreas Boehler        $this->assertEquals(204, $response->status);
161*a1a3b679SAndreas Boehler
162*a1a3b679SAndreas Boehler    }
163*a1a3b679SAndreas Boehler
164*a1a3b679SAndreas Boehler    function testproppatchMinimalError() {
165*a1a3b679SAndreas Boehler
166*a1a3b679SAndreas Boehler        $request = new HTTP\Request('PROPPATCH', '/', ['Prefer' => 'return-minimal']);
167*a1a3b679SAndreas Boehler        $request->setBody(<<<BLA
168*a1a3b679SAndreas Boehler<?xml version="1.0"?>
169*a1a3b679SAndreas Boehler<d:propertyupdate xmlns:d="DAV:">
170*a1a3b679SAndreas Boehler    <d:set>
171*a1a3b679SAndreas Boehler        <d:prop>
172*a1a3b679SAndreas Boehler            <d:something>nope!</d:something>
173*a1a3b679SAndreas Boehler        </d:prop>
174*a1a3b679SAndreas Boehler    </d:set>
175*a1a3b679SAndreas Boehler</d:propertyupdate>
176*a1a3b679SAndreas BoehlerBLA
177*a1a3b679SAndreas Boehler        );
178*a1a3b679SAndreas Boehler
179*a1a3b679SAndreas Boehler        $response = $this->request($request);
180*a1a3b679SAndreas Boehler
181*a1a3b679SAndreas Boehler        $body = $response->getBodyAsString();
182*a1a3b679SAndreas Boehler
183*a1a3b679SAndreas Boehler        $this->assertEquals(207, $response->status);
184*a1a3b679SAndreas Boehler        $this->assertTrue(strpos($body, 'something') !== false);
185*a1a3b679SAndreas Boehler        $this->assertTrue(strpos($body, '403 Forbidden') !== false, $body);
186*a1a3b679SAndreas Boehler
187*a1a3b679SAndreas Boehler    }
188*a1a3b679SAndreas Boehler}
189