1<?php
2
3namespace Sabre\DAV;
4
5use Sabre\DAVServerTest;
6use Sabre\HTTP;
7
8/**
9 * Tests related to the GET request.
10 *
11 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
12 * @author Evert Pot (http://evertpot.com/)
13 * @license http://sabre.io/license/ Modified BSD License
14 */
15class HttpGetTest extends DAVServerTest {
16
17    /**
18     * Sets up the DAV tree.
19     *
20     * @return void
21     */
22    function setUpTree() {
23
24        $this->tree = new Mock\Collection('root', [
25            'file1' => 'foo',
26            new Mock\Collection('dir', []),
27            new Mock\StreamingFile('streaming', 'stream')
28        ]);
29
30    }
31
32    function testGet() {
33
34        $request = new HTTP\Request('GET', '/file1');
35        $response = $this->request($request);
36
37        $this->assertEquals(200, $response->getStatus());
38
39        // Removing Last-Modified because it keeps changing.
40        $response->removeHeader('Last-Modified');
41
42        $this->assertEquals(
43            [
44                'X-Sabre-Version' => [Version::VERSION],
45                'Content-Type'    => ['application/octet-stream'],
46                'Content-Length'  => [3],
47                'ETag'            => ['"' . md5('foo') . '"'],
48            ],
49            $response->getHeaders()
50        );
51
52        $this->assertEquals('foo', $response->getBodyAsString());
53
54    }
55
56    function testGetHttp10() {
57
58        $request = new HTTP\Request('GET', '/file1');
59        $request->setHttpVersion('1.0');
60        $response = $this->request($request);
61
62        $this->assertEquals(200, $response->getStatus());
63
64        // Removing Last-Modified because it keeps changing.
65        $response->removeHeader('Last-Modified');
66
67        $this->assertEquals(
68            [
69                'X-Sabre-Version' => [Version::VERSION],
70                'Content-Type'    => ['application/octet-stream'],
71                'Content-Length'  => [3],
72                'ETag'            => ['"' . md5('foo') . '"'],
73            ],
74            $response->getHeaders()
75        );
76
77        $this->assertEquals('1.0', $response->getHttpVersion());
78
79        $this->assertEquals('foo', $response->getBodyAsString());
80
81    }
82
83    function testGet404() {
84
85        $request = new HTTP\Request('GET', '/notfound');
86        $response = $this->request($request);
87
88        $this->assertEquals(404, $response->getStatus());
89
90    }
91
92    function testGet404_aswell() {
93
94        $request = new HTTP\Request('GET', '/file1/subfile');
95        $response = $this->request($request);
96
97        $this->assertEquals(404, $response->getStatus());
98
99    }
100
101    /**
102     * We automatically normalize double slashes.
103     */
104    function testGetDoubleSlash() {
105
106        $request = new HTTP\Request('GET', '//file1');
107        $response = $this->request($request);
108
109        $this->assertEquals(200, $response->getStatus());
110
111        // Removing Last-Modified because it keeps changing.
112        $response->removeHeader('Last-Modified');
113
114        $this->assertEquals(
115            [
116                'X-Sabre-Version' => [Version::VERSION],
117                'Content-Type'    => ['application/octet-stream'],
118                'Content-Length'  => [3],
119                'ETag'            => ['"' . md5('foo') . '"'],
120            ],
121            $response->getHeaders()
122        );
123
124        $this->assertEquals('foo', $response->getBodyAsString());
125
126    }
127
128    function testGetCollection() {
129
130        $request = new HTTP\Request('GET', '/dir');
131        $response = $this->request($request);
132
133        $this->assertEquals(501, $response->getStatus());
134
135    }
136
137    function testGetStreaming() {
138
139        $request = new HTTP\Request('GET', '/streaming');
140        $response = $this->request($request);
141
142        $this->assertEquals(200, $response->getStatus());
143
144        // Removing Last-Modified because it keeps changing.
145        $response->removeHeader('Last-Modified');
146
147        $this->assertEquals(
148            [
149                'X-Sabre-Version' => [Version::VERSION],
150                'Content-Type'    => ['application/octet-stream'],
151            ],
152            $response->getHeaders()
153        );
154
155        $this->assertEquals('stream', $response->getBodyAsString());
156
157    }
158}
159