1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\DAV; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehleruse Sabre\DAVServerTest; 6*a1a3b679SAndreas Boehleruse Sabre\HTTP; 7*a1a3b679SAndreas Boehler 8*a1a3b679SAndreas Boehler/** 9*a1a3b679SAndreas Boehler * Tests related to the HEAD request. 10*a1a3b679SAndreas Boehler * 11*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/). 12*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/) 13*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License 14*a1a3b679SAndreas Boehler */ 15*a1a3b679SAndreas Boehlerclass HttpHeadTest extends DAVServerTest { 16*a1a3b679SAndreas Boehler 17*a1a3b679SAndreas Boehler /** 18*a1a3b679SAndreas Boehler * Sets up the DAV tree. 19*a1a3b679SAndreas Boehler * 20*a1a3b679SAndreas Boehler * @return void 21*a1a3b679SAndreas Boehler */ 22*a1a3b679SAndreas Boehler function setUpTree() { 23*a1a3b679SAndreas Boehler 24*a1a3b679SAndreas Boehler $this->tree = new Mock\Collection('root', [ 25*a1a3b679SAndreas Boehler 'file1' => 'foo', 26*a1a3b679SAndreas Boehler new Mock\Collection('dir', []), 27*a1a3b679SAndreas Boehler new Mock\StreamingFile('streaming', 'stream') 28*a1a3b679SAndreas Boehler ]); 29*a1a3b679SAndreas Boehler 30*a1a3b679SAndreas Boehler } 31*a1a3b679SAndreas Boehler 32*a1a3b679SAndreas Boehler function testHEAD() { 33*a1a3b679SAndreas Boehler 34*a1a3b679SAndreas Boehler $request = new HTTP\Request('HEAD', '//file1'); 35*a1a3b679SAndreas Boehler $response = $this->request($request); 36*a1a3b679SAndreas Boehler 37*a1a3b679SAndreas Boehler $this->assertEquals(200, $response->getStatus()); 38*a1a3b679SAndreas Boehler 39*a1a3b679SAndreas Boehler // Removing Last-Modified because it keeps changing. 40*a1a3b679SAndreas Boehler $response->removeHeader('Last-Modified'); 41*a1a3b679SAndreas Boehler 42*a1a3b679SAndreas Boehler $this->assertEquals( 43*a1a3b679SAndreas Boehler [ 44*a1a3b679SAndreas Boehler 'X-Sabre-Version' => [Version::VERSION], 45*a1a3b679SAndreas Boehler 'Content-Type' => ['application/octet-stream'], 46*a1a3b679SAndreas Boehler 'Content-Length' => [3], 47*a1a3b679SAndreas Boehler 'ETag' => ['"' . md5('foo') . '"'], 48*a1a3b679SAndreas Boehler ], 49*a1a3b679SAndreas Boehler $response->getHeaders() 50*a1a3b679SAndreas Boehler ); 51*a1a3b679SAndreas Boehler 52*a1a3b679SAndreas Boehler $this->assertEquals('', $response->getBodyAsString()); 53*a1a3b679SAndreas Boehler 54*a1a3b679SAndreas Boehler } 55*a1a3b679SAndreas Boehler 56*a1a3b679SAndreas Boehler /** 57*a1a3b679SAndreas Boehler * According to the specs, HEAD should behave identical to GET. But, broken 58*a1a3b679SAndreas Boehler * clients needs HEAD requests on collections to respond with a 200, so 59*a1a3b679SAndreas Boehler * that's what we do. 60*a1a3b679SAndreas Boehler */ 61*a1a3b679SAndreas Boehler function testHEADCollection() { 62*a1a3b679SAndreas Boehler 63*a1a3b679SAndreas Boehler $request = new HTTP\Request('HEAD', '/dir'); 64*a1a3b679SAndreas Boehler $response = $this->request($request); 65*a1a3b679SAndreas Boehler 66*a1a3b679SAndreas Boehler $this->assertEquals(200, $response->getStatus()); 67*a1a3b679SAndreas Boehler 68*a1a3b679SAndreas Boehler } 69*a1a3b679SAndreas Boehler 70*a1a3b679SAndreas Boehler /** 71*a1a3b679SAndreas Boehler * HEAD automatically internally maps to GET via a sub-request. 72*a1a3b679SAndreas Boehler * The Auth plugin must not be triggered twice for these, so we'll 73*a1a3b679SAndreas Boehler * test for that. 74*a1a3b679SAndreas Boehler */ 75*a1a3b679SAndreas Boehler function testDoubleAuth() { 76*a1a3b679SAndreas Boehler 77*a1a3b679SAndreas Boehler $count = 0; 78*a1a3b679SAndreas Boehler 79*a1a3b679SAndreas Boehler $authBackend = new Auth\Backend\BasicCallBack(function($userName,$password) use (&$count) { 80*a1a3b679SAndreas Boehler $count++; 81*a1a3b679SAndreas Boehler return true; 82*a1a3b679SAndreas Boehler }); 83*a1a3b679SAndreas Boehler $this->server->addPlugin( 84*a1a3b679SAndreas Boehler new Auth\Plugin( 85*a1a3b679SAndreas Boehler $authBackend 86*a1a3b679SAndreas Boehler ) 87*a1a3b679SAndreas Boehler ); 88*a1a3b679SAndreas Boehler $request = new HTTP\Request('HEAD', '/file1', ['Authorization' => 'Basic ' . base64_encode('user:pass')]); 89*a1a3b679SAndreas Boehler $response = $this->request($request); 90*a1a3b679SAndreas Boehler 91*a1a3b679SAndreas Boehler $this->assertEquals(200, $response->getStatus()); 92*a1a3b679SAndreas Boehler 93*a1a3b679SAndreas Boehler $this->assertEquals(1, $count, 'Auth was triggered twice :('); 94*a1a3b679SAndreas Boehler 95*a1a3b679SAndreas Boehler } 96*a1a3b679SAndreas Boehler 97*a1a3b679SAndreas Boehler} 98