1<?php
2
3namespace Sabre\DAV;
4
5use Sabre\DAVServerTest;
6use Sabre\HTTP;
7
8/**
9 * Tests related to the HEAD 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 HttpHeadTest 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 testHEAD() {
33
34        $request = new HTTP\Request('HEAD', '//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('', $response->getBodyAsString());
53
54    }
55
56    /**
57     * According to the specs, HEAD should behave identical to GET. But, broken
58     * clients needs HEAD requests on collections to respond with a 200, so
59     * that's what we do.
60     */
61    function testHEADCollection() {
62
63        $request = new HTTP\Request('HEAD', '/dir');
64        $response = $this->request($request);
65
66        $this->assertEquals(200, $response->getStatus());
67
68    }
69
70    /**
71     * HEAD automatically internally maps to GET via a sub-request.
72     * The Auth plugin must not be triggered twice for these, so we'll
73     * test for that.
74     */
75    function testDoubleAuth() {
76
77        $count = 0;
78
79        $authBackend = new Auth\Backend\BasicCallBack(function($userName,$password) use (&$count) {
80            $count++;
81            return true;
82        });
83        $this->server->addPlugin(
84            new Auth\Plugin(
85                $authBackend
86            )
87        );
88        $request = new HTTP\Request('HEAD', '/file1', ['Authorization' => 'Basic ' . base64_encode('user:pass')]);
89        $response = $this->request($request);
90
91        $this->assertEquals(200, $response->getStatus());
92
93        $this->assertEquals(1, $count, 'Auth was triggered twice :(');
94
95    }
96
97}
98