123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- namespace Sabre\DAV;
- use Sabre\DAVServerTest;
- use Sabre\HTTP;
- /**
- * Tests related to the PUT request.
- *
- * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
- * @author Evert Pot (http://evertpot.com/)
- * @license http://sabre.io/license/ Modified BSD License
- */
- class HttpPutTest extends DAVServerTest {
- /**
- * Sets up the DAV tree.
- *
- * @return void
- */
- function setUpTree() {
- $this->tree = new Mock\Collection('root', [
- 'file1' => 'foo',
- ]);
- }
- /**
- * A successful PUT of a new file.
- */
- function testPut() {
- $request = new HTTP\Request('PUT', '/file2', [], 'hello');
- $response = $this->request($request);
- $this->assertEquals(201, $response->getStatus(), 'Incorrect status code received. Full response body:' . $response->getBodyAsString());
- $this->assertEquals(
- 'hello',
- $this->server->tree->getNodeForPath('file2')->get()
- );
- $this->assertEquals(
- [
- 'X-Sabre-Version' => [Version::VERSION],
- 'Content-Length' => ['0'],
- 'ETag' => ['"' . md5('hello') . '"']
- ],
- $response->getHeaders()
- );
- }
- /**
- * A successful PUT on an existing file.
- *
- * @depends testPut
- */
- function testPutExisting() {
- $request = new HTTP\Request('PUT', '/file1', [], 'bar');
- $response = $this->request($request);
- $this->assertEquals(204, $response->getStatus());
- $this->assertEquals(
- 'bar',
- $this->server->tree->getNodeForPath('file1')->get()
- );
- $this->assertEquals(
- [
- 'X-Sabre-Version' => [Version::VERSION],
- 'Content-Length' => ['0'],
- 'ETag' => ['"' . md5('bar') . '"']
- ],
- $response->getHeaders()
- );
- }
- /**
- * PUT on existing file with If-Match: *
- *
- * @depends testPutExisting
- */
- function testPutExistingIfMatchStar() {
- $request = new HTTP\Request(
- 'PUT',
- '/file1',
- ['If-Match' => '*'],
- 'hello'
- );
- $response = $this->request($request);
- $this->assertEquals(204, $response->getStatus());
- $this->assertEquals(
- 'hello',
- $this->server->tree->getNodeForPath('file1')->get()
- );
- $this->assertEquals(
- [
- 'X-Sabre-Version' => [Version::VERSION],
- 'Content-Length' => ['0'],
- 'ETag' => ['"' . md5('hello') . '"']
- ],
- $response->getHeaders()
- );
- }
- /**
- * PUT on existing file with If-Match: with a correct etag
- *
- * @depends testPutExisting
- */
- function testPutExistingIfMatchCorrect() {
- $request = new HTTP\Request(
- 'PUT',
- '/file1',
- ['If-Match' => '"' . md5('foo') . '"'],
- 'hello'
- );
- $response = $this->request($request);
- $this->assertEquals(204, $response->status);
- $this->assertEquals(
- 'hello',
- $this->server->tree->getNodeForPath('file1')->get()
- );
- $this->assertEquals(
- [
- 'X-Sabre-Version' => [Version::VERSION],
- 'Content-Length' => ['0'],
- 'ETag' => ['"' . md5('hello') . '"'],
- ],
- $response->getHeaders()
- );
- }
- /**
- * PUT with Content-Range should be rejected.
- *
- * @depends testPut
- */
- function testPutContentRange() {
- $request = new HTTP\Request(
- 'PUT',
- '/file2',
- ['Content-Range' => 'bytes/100-200'],
- 'hello'
- );
- $response = $this->request($request);
- $this->assertEquals(400, $response->getStatus());
- }
- /**
- * PUT on non-existing file with If-None-Match: * should work.
- *
- * @depends testPut
- */
- function testPutIfNoneMatchStar() {
- $request = new HTTP\Request(
- 'PUT',
- '/file2',
- ['If-None-Match' => '*'],
- 'hello'
- );
- $response = $this->request($request);
- $this->assertEquals(201, $response->getStatus());
- $this->assertEquals(
- 'hello',
- $this->server->tree->getNodeForPath('file2')->get()
- );
- $this->assertEquals(
- [
- 'X-Sabre-Version' => [Version::VERSION],
- 'Content-Length' => ['0'],
- 'ETag' => ['"' . md5('hello') . '"']
- ],
- $response->getHeaders()
- );
- }
- /**
- * PUT on non-existing file with If-Match: * should fail.
- *
- * @depends testPut
- */
- function testPutIfMatchStar() {
- $request = new HTTP\Request(
- 'PUT',
- '/file2',
- ['If-Match' => '*'],
- 'hello'
- );
- $response = $this->request($request);
- $this->assertEquals(412, $response->getStatus());
- }
- /**
- * PUT on existing file with If-None-Match: * should fail.
- *
- * @depends testPut
- */
- function testPutExistingIfNoneMatchStar() {
- $request = new HTTP\Request(
- 'PUT',
- '/file1',
- ['If-None-Match' => '*'],
- 'hello'
- );
- $request->setBody('hello');
- $response = $this->request($request);
- $this->assertEquals(412, $response->getStatus());
- }
- /**
- * PUT thats created in a non-collection should be rejected.
- *
- * @depends testPut
- */
- function testPutNoParent() {
- $request = new HTTP\Request(
- 'PUT',
- '/file1/file2',
- [],
- 'hello'
- );
- $response = $this->request($request);
- $this->assertEquals(409, $response->getStatus());
- }
- /**
- * Finder may sometimes make a request, which gets its content-body
- * stripped. We can't always prevent this from happening, but in some cases
- * we can detected this and return an error instead.
- *
- * @depends testPut
- */
- function testFinderPutSuccess() {
- $request = new HTTP\Request(
- 'PUT',
- '/file2',
- ['X-Expected-Entity-Length' => '5'],
- 'hello'
- );
- $response = $this->request($request);
- $this->assertEquals(201, $response->getStatus());
- $this->assertEquals(
- 'hello',
- $this->server->tree->getNodeForPath('file2')->get()
- );
- $this->assertEquals(
- [
- 'X-Sabre-Version' => [Version::VERSION],
- 'Content-Length' => ['0'],
- 'ETag' => ['"' . md5('hello') . '"'],
- ],
- $response->getHeaders()
- );
- }
- /**
- * Same as the last one, but in this case we're mimicing a failed request.
- *
- * @depends testFinderPutSuccess
- */
- function testFinderPutFail() {
- $request = new HTTP\Request(
- 'PUT',
- '/file2',
- ['X-Expected-Entity-Length' => '5'],
- ''
- );
- $response = $this->request($request);
- $this->assertEquals(403, $response->getStatus());
- }
- /**
- * Plugins can intercept PUT. We need to make sure that works.
- *
- * @depends testPut
- */
- function testPutIntercept() {
- $this->server->on('beforeBind', function($uri) {
- $this->server->httpResponse->setStatus(418);
- return false;
- });
- $request = new HTTP\Request('PUT', '/file2', [], 'hello');
- $response = $this->request($request);
- $this->assertEquals(418, $response->getStatus(), 'Incorrect status code received. Full response body: ' .$response->getBodyAsString());
- $this->assertFalse(
- $this->server->tree->nodeExists('file2')
- );
- $this->assertEquals([
- 'X-Sabre-Version' => [Version::VERSION],
- ], $response->getHeaders());
- }
- }
|