1<?php 2 3namespace Sabre\DAV\PartialUpdate; 4 5use Sabre\DAV; 6use Sabre\HTTP; 7 8require_once 'Sabre/DAV/PartialUpdate/FileMock.php'; 9 10class PluginTest extends \Sabre\DAVServerTest { 11 12 protected $node; 13 protected $plugin; 14 15 function setUp() { 16 17 $this->node = new FileMock(); 18 $this->tree[] = $this->node; 19 20 parent::setUp(); 21 22 $this->plugin = new Plugin(); 23 $this->server->addPlugin($this->plugin); 24 25 26 27 } 28 29 function testInit() { 30 31 $this->assertEquals('partialupdate', $this->plugin->getPluginName()); 32 $this->assertEquals(['sabredav-partialupdate'], $this->plugin->getFeatures()); 33 $this->assertEquals([ 34 'PATCH' 35 ], $this->plugin->getHTTPMethods('partial')); 36 $this->assertEquals([ 37 ], $this->plugin->getHTTPMethods('')); 38 39 } 40 41 function testPatchNoRange() { 42 43 $this->node->put('00000000'); 44 $request = HTTP\Sapi::createFromServerArray([ 45 'REQUEST_METHOD' => 'PATCH', 46 'REQUEST_URI' => '/partial', 47 ]); 48 $response = $this->request($request); 49 50 $this->assertEquals(400, $response->status, 'Full response body:' . $response->body); 51 52 } 53 54 function testPatchNotSupported() { 55 56 $this->node->put('00000000'); 57 $request = new HTTP\Request('PATCH', '/', ['X-Update-Range' => '3-4']); 58 $request->setBody( 59 '111' 60 ); 61 $response = $this->request($request); 62 63 $this->assertEquals(405, $response->status, 'Full response body:' . $response->body); 64 65 } 66 67 function testPatchNoContentType() { 68 69 $this->node->put('00000000'); 70 $request = new HTTP\Request('PATCH', '/partial', ['X-Update-Range' => 'bytes=3-4']); 71 $request->setBody( 72 '111' 73 ); 74 $response = $this->request($request); 75 76 $this->assertEquals(415, $response->status, 'Full response body:' . $response->body); 77 78 } 79 80 function testPatchBadRange() { 81 82 $this->node->put('00000000'); 83 $request = new HTTP\Request('PATCH', '/partial', ['X-Update-Range' => 'bytes=3-4', 'Content-Type' => 'application/x-sabredav-partialupdate', 'Content-Length' => '3']); 84 $request->setBody( 85 '111' 86 ); 87 $response = $this->request($request); 88 89 $this->assertEquals(416, $response->status, 'Full response body:' . $response->body); 90 91 } 92 93 function testPatchNoLength() { 94 95 $this->node->put('00000000'); 96 $request = new HTTP\Request('PATCH', '/partial', ['X-Update-Range' => 'bytes=3-5', 'Content-Type' => 'application/x-sabredav-partialupdate']); 97 $request->setBody( 98 '111' 99 ); 100 $response = $this->request($request); 101 102 $this->assertEquals(411, $response->status, 'Full response body:' . $response->body); 103 104 } 105 106 function testPatchSuccess() { 107 108 $this->node->put('00000000'); 109 $request = new HTTP\Request('PATCH', '/partial', ['X-Update-Range' => 'bytes=3-5', 'Content-Type' => 'application/x-sabredav-partialupdate', 'Content-Length' => 3]); 110 $request->setBody( 111 '111' 112 ); 113 $response = $this->request($request); 114 115 $this->assertEquals(204, $response->status, 'Full response body:' . $response->body); 116 $this->assertEquals('00011100', $this->node->get()); 117 118 } 119 120 function testPatchNoEndRange() { 121 122 $this->node->put('00000'); 123 $request = new HTTP\Request('PATCH', '/partial', ['X-Update-Range' => 'bytes=3-', 'Content-Type' => 'application/x-sabredav-partialupdate', 'Content-Length' => '3']); 124 $request->setBody( 125 '111' 126 ); 127 128 $response = $this->request($request); 129 130 $this->assertEquals(204, $response->getStatus(), 'Full response body:' . $response->getBodyAsString()); 131 $this->assertEquals('00111', $this->node->get()); 132 133 } 134 135} 136