HttpPutTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace Sabre\DAV;
  3. use Sabre\DAVServerTest;
  4. use Sabre\HTTP;
  5. /**
  6. * Tests related to the PUT request.
  7. *
  8. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  9. * @author Evert Pot (http://evertpot.com/)
  10. * @license http://sabre.io/license/ Modified BSD License
  11. */
  12. class HttpPutTest extends DAVServerTest {
  13. /**
  14. * Sets up the DAV tree.
  15. *
  16. * @return void
  17. */
  18. function setUpTree() {
  19. $this->tree = new Mock\Collection('root', [
  20. 'file1' => 'foo',
  21. ]);
  22. }
  23. /**
  24. * A successful PUT of a new file.
  25. */
  26. function testPut() {
  27. $request = new HTTP\Request('PUT', '/file2', [], 'hello');
  28. $response = $this->request($request);
  29. $this->assertEquals(201, $response->getStatus(), 'Incorrect status code received. Full response body:' . $response->getBodyAsString());
  30. $this->assertEquals(
  31. 'hello',
  32. $this->server->tree->getNodeForPath('file2')->get()
  33. );
  34. $this->assertEquals(
  35. [
  36. 'X-Sabre-Version' => [Version::VERSION],
  37. 'Content-Length' => ['0'],
  38. 'ETag' => ['"' . md5('hello') . '"']
  39. ],
  40. $response->getHeaders()
  41. );
  42. }
  43. /**
  44. * A successful PUT on an existing file.
  45. *
  46. * @depends testPut
  47. */
  48. function testPutExisting() {
  49. $request = new HTTP\Request('PUT', '/file1', [], 'bar');
  50. $response = $this->request($request);
  51. $this->assertEquals(204, $response->getStatus());
  52. $this->assertEquals(
  53. 'bar',
  54. $this->server->tree->getNodeForPath('file1')->get()
  55. );
  56. $this->assertEquals(
  57. [
  58. 'X-Sabre-Version' => [Version::VERSION],
  59. 'Content-Length' => ['0'],
  60. 'ETag' => ['"' . md5('bar') . '"']
  61. ],
  62. $response->getHeaders()
  63. );
  64. }
  65. /**
  66. * PUT on existing file with If-Match: *
  67. *
  68. * @depends testPutExisting
  69. */
  70. function testPutExistingIfMatchStar() {
  71. $request = new HTTP\Request(
  72. 'PUT',
  73. '/file1',
  74. ['If-Match' => '*'],
  75. 'hello'
  76. );
  77. $response = $this->request($request);
  78. $this->assertEquals(204, $response->getStatus());
  79. $this->assertEquals(
  80. 'hello',
  81. $this->server->tree->getNodeForPath('file1')->get()
  82. );
  83. $this->assertEquals(
  84. [
  85. 'X-Sabre-Version' => [Version::VERSION],
  86. 'Content-Length' => ['0'],
  87. 'ETag' => ['"' . md5('hello') . '"']
  88. ],
  89. $response->getHeaders()
  90. );
  91. }
  92. /**
  93. * PUT on existing file with If-Match: with a correct etag
  94. *
  95. * @depends testPutExisting
  96. */
  97. function testPutExistingIfMatchCorrect() {
  98. $request = new HTTP\Request(
  99. 'PUT',
  100. '/file1',
  101. ['If-Match' => '"' . md5('foo') . '"'],
  102. 'hello'
  103. );
  104. $response = $this->request($request);
  105. $this->assertEquals(204, $response->status);
  106. $this->assertEquals(
  107. 'hello',
  108. $this->server->tree->getNodeForPath('file1')->get()
  109. );
  110. $this->assertEquals(
  111. [
  112. 'X-Sabre-Version' => [Version::VERSION],
  113. 'Content-Length' => ['0'],
  114. 'ETag' => ['"' . md5('hello') . '"'],
  115. ],
  116. $response->getHeaders()
  117. );
  118. }
  119. /**
  120. * PUT with Content-Range should be rejected.
  121. *
  122. * @depends testPut
  123. */
  124. function testPutContentRange() {
  125. $request = new HTTP\Request(
  126. 'PUT',
  127. '/file2',
  128. ['Content-Range' => 'bytes/100-200'],
  129. 'hello'
  130. );
  131. $response = $this->request($request);
  132. $this->assertEquals(400, $response->getStatus());
  133. }
  134. /**
  135. * PUT on non-existing file with If-None-Match: * should work.
  136. *
  137. * @depends testPut
  138. */
  139. function testPutIfNoneMatchStar() {
  140. $request = new HTTP\Request(
  141. 'PUT',
  142. '/file2',
  143. ['If-None-Match' => '*'],
  144. 'hello'
  145. );
  146. $response = $this->request($request);
  147. $this->assertEquals(201, $response->getStatus());
  148. $this->assertEquals(
  149. 'hello',
  150. $this->server->tree->getNodeForPath('file2')->get()
  151. );
  152. $this->assertEquals(
  153. [
  154. 'X-Sabre-Version' => [Version::VERSION],
  155. 'Content-Length' => ['0'],
  156. 'ETag' => ['"' . md5('hello') . '"']
  157. ],
  158. $response->getHeaders()
  159. );
  160. }
  161. /**
  162. * PUT on non-existing file with If-Match: * should fail.
  163. *
  164. * @depends testPut
  165. */
  166. function testPutIfMatchStar() {
  167. $request = new HTTP\Request(
  168. 'PUT',
  169. '/file2',
  170. ['If-Match' => '*'],
  171. 'hello'
  172. );
  173. $response = $this->request($request);
  174. $this->assertEquals(412, $response->getStatus());
  175. }
  176. /**
  177. * PUT on existing file with If-None-Match: * should fail.
  178. *
  179. * @depends testPut
  180. */
  181. function testPutExistingIfNoneMatchStar() {
  182. $request = new HTTP\Request(
  183. 'PUT',
  184. '/file1',
  185. ['If-None-Match' => '*'],
  186. 'hello'
  187. );
  188. $request->setBody('hello');
  189. $response = $this->request($request);
  190. $this->assertEquals(412, $response->getStatus());
  191. }
  192. /**
  193. * PUT thats created in a non-collection should be rejected.
  194. *
  195. * @depends testPut
  196. */
  197. function testPutNoParent() {
  198. $request = new HTTP\Request(
  199. 'PUT',
  200. '/file1/file2',
  201. [],
  202. 'hello'
  203. );
  204. $response = $this->request($request);
  205. $this->assertEquals(409, $response->getStatus());
  206. }
  207. /**
  208. * Finder may sometimes make a request, which gets its content-body
  209. * stripped. We can't always prevent this from happening, but in some cases
  210. * we can detected this and return an error instead.
  211. *
  212. * @depends testPut
  213. */
  214. function testFinderPutSuccess() {
  215. $request = new HTTP\Request(
  216. 'PUT',
  217. '/file2',
  218. ['X-Expected-Entity-Length' => '5'],
  219. 'hello'
  220. );
  221. $response = $this->request($request);
  222. $this->assertEquals(201, $response->getStatus());
  223. $this->assertEquals(
  224. 'hello',
  225. $this->server->tree->getNodeForPath('file2')->get()
  226. );
  227. $this->assertEquals(
  228. [
  229. 'X-Sabre-Version' => [Version::VERSION],
  230. 'Content-Length' => ['0'],
  231. 'ETag' => ['"' . md5('hello') . '"'],
  232. ],
  233. $response->getHeaders()
  234. );
  235. }
  236. /**
  237. * Same as the last one, but in this case we're mimicing a failed request.
  238. *
  239. * @depends testFinderPutSuccess
  240. */
  241. function testFinderPutFail() {
  242. $request = new HTTP\Request(
  243. 'PUT',
  244. '/file2',
  245. ['X-Expected-Entity-Length' => '5'],
  246. ''
  247. );
  248. $response = $this->request($request);
  249. $this->assertEquals(403, $response->getStatus());
  250. }
  251. /**
  252. * Plugins can intercept PUT. We need to make sure that works.
  253. *
  254. * @depends testPut
  255. */
  256. function testPutIntercept() {
  257. $this->server->on('beforeBind', function($uri) {
  258. $this->server->httpResponse->setStatus(418);
  259. return false;
  260. });
  261. $request = new HTTP\Request('PUT', '/file2', [], 'hello');
  262. $response = $this->request($request);
  263. $this->assertEquals(418, $response->getStatus(), 'Incorrect status code received. Full response body: ' .$response->getBodyAsString());
  264. $this->assertFalse(
  265. $this->server->tree->nodeExists('file2')
  266. );
  267. $this->assertEquals([
  268. 'X-Sabre-Version' => [Version::VERSION],
  269. ], $response->getHeaders());
  270. }
  271. }