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