[Pkg-owncloud-commits] [php-sabredav] 49/80: Modernized range tests

David Prévot taffit at moszumanska.debian.org
Thu Jan 7 02:56:32 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabredav.

commit 31135b42ada458a95a77a0ea2201492094c03993
Author: Evert Pot <me at evertpot.com>
Date:   Mon Jan 4 18:49:56 2016 -0500

    Modernized range tests
---
 tests/Sabre/DAV/Mock/File.php       |  21 ++-
 tests/Sabre/DAV/ServerRangeTest.php | 249 +++++++++++++++---------------------
 2 files changed, 121 insertions(+), 149 deletions(-)

diff --git a/tests/Sabre/DAV/Mock/File.php b/tests/Sabre/DAV/Mock/File.php
index bde9560..23855e3 100644
--- a/tests/Sabre/DAV/Mock/File.php
+++ b/tests/Sabre/DAV/Mock/File.php
@@ -18,6 +18,7 @@ class File extends DAV\File {
     protected $name;
     protected $contents;
     protected $parent;
+    protected $lastModified;
 
     /**
      * Creates the object
@@ -26,12 +27,18 @@ class File extends DAV\File {
      * @param array $children
      * @return void
      */
-    function __construct($name, $contents, Collection $parent = null) {
+    function __construct($name, $contents, Collection $parent = null, $lastModified = -1) {
 
         $this->name = $name;
         $this->put($contents);
         $this->parent = $parent;
 
+        if ($lastModified === -1) {
+            $lastModified = time();
+        }
+
+        $this->lastModified = $lastModified;
+
     }
 
     /**
@@ -138,4 +145,16 @@ class File extends DAV\File {
 
     }
 
+    /**
+     * Returns the last modification time as a unix timestamp.
+     * If the information is not available, return null.
+     *
+     * @return int
+     */
+    function getLastModified() {
+
+        return $this->lastModified;
+
+    }
+
 }
diff --git a/tests/Sabre/DAV/ServerRangeTest.php b/tests/Sabre/DAV/ServerRangeTest.php
index 15a51c1..4d19eb5 100644
--- a/tests/Sabre/DAV/ServerRangeTest.php
+++ b/tests/Sabre/DAV/ServerRangeTest.php
@@ -1,44 +1,54 @@
 <?php
 
 namespace Sabre\DAV;
+
+use DateTime;
 use Sabre\HTTP;
 
-require_once 'Sabre/DAV/AbstractServer.php';
+/**
+ * This file tests HTTP requests that use the Range: header.
+ *
+ * @copyright Copyright (C) fruux GmbH. (https://fruux.com/)
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class ServerRangeTest extends \Sabre\DAVServerTest {
+
+    protected $setupFiles = true;
+
+    /**
+     * We need this string a lot
+     */
+    protected $lastModified;
 
-class ServerRangeTest extends AbstractServer{
+    function setUp() {
 
-    protected function getRootNode() {
+        parent::setUp();
+        $this->server->createFile('files/test.txt', 'Test contents');
 
-        return new FSExt\Directory(SABRE_TEMPDIR);
+        $this->lastModified = HTTP\Util::toHTTPDate(
+            new DateTime('@' . $this->server->tree->getNodeForPath('files/test.txt')->getLastModified())
+        );
 
     }
 
     function testRange() {
 
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=2-5',
-        ];
-        $filename = SABRE_TEMPDIR . '/test.txt';
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
+        $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=2-5']);
+        $response = $this->request($request);
 
         $this->assertEquals([
             'X-Sabre-Version' => [Version::VERSION],
             'Content-Type'    => ['application/octet-stream'],
             'Content-Length'  => [4],
             'Content-Range'   => ['bytes 2-5/13'],
-            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
-            'ETag'            => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
+            'ETag'            => ['"' . md5('Test contents') . '"'],
+            'Last-Modified'   => [$this->lastModified],
             ],
-            $this->response->getHeaders()
-         );
-
-        $this->assertEquals(206, $this->response->status);
-        $this->assertEquals('st c', stream_get_contents($this->response->body, 4));
+            $response->getHeaders()
+        );
+        $this->assertEquals(206, $response->getStatus());
+        $this->assertEquals('st c', $response->getBodyAsString());
 
     }
 
@@ -47,30 +57,22 @@ class ServerRangeTest extends AbstractServer{
      */
     function testStartRange() {
 
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=2-',
-        ];
-        $filename = SABRE_TEMPDIR . '/test.txt';
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
+        $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=2-']);
+        $response = $this->request($request);
 
         $this->assertEquals([
             'X-Sabre-Version' => [Version::VERSION],
             'Content-Type'    => ['application/octet-stream'],
             'Content-Length'  => [11],
             'Content-Range'   => ['bytes 2-12/13'],
-            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
-            'ETag'            => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
+            'ETag'            => ['"' . md5('Test contents') . '"'],
+            'Last-Modified'   => [$this->lastModified],
             ],
-            $this->response->getHeaders()
-         );
+            $response->getHeaders()
+        );
 
-        $this->assertEquals(206, $this->response->status);
-        $this->assertEquals('st contents', stream_get_contents($this->response->body, 11));
+        $this->assertEquals(206, $response->getStatus());
+        $this->assertEquals('st contents', $response->getBodyAsString());
 
     }
 
@@ -79,30 +81,22 @@ class ServerRangeTest extends AbstractServer{
      */
     function testEndRange() {
 
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=-8',
-        ];
-        $filename = SABRE_TEMPDIR . '/test.txt';
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
+        $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=-8']);
+        $response = $this->request($request);
 
         $this->assertEquals([
             'X-Sabre-Version' => [Version::VERSION],
             'Content-Type'    => ['application/octet-stream'],
             'Content-Length'  => [8],
             'Content-Range'   => ['bytes 5-12/13'],
-            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
-            'ETag'            => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
+            'ETag'            => ['"' . md5('Test contents') . '"'],
+            'Last-Modified'   => [$this->lastModified],
             ],
-            $this->response->getHeaders()
-         );
+            $response->getHeaders()
+        );
 
-        $this->assertEquals(206, $this->response->status);
-        $this->assertEquals('contents', stream_get_contents($this->response->body, 8));
+        $this->assertEquals(206, $response->getStatus());
+        $this->assertEquals('contents', $response->getBodyAsString());
 
     }
 
@@ -111,17 +105,10 @@ class ServerRangeTest extends AbstractServer{
      */
     function testTooHighRange() {
 
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=100-200',
-        ];
+        $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=100-200']);
+        $response = $this->request($request);
 
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
-
-        $this->assertEquals(416, $this->response->status);
+        $this->assertEquals(416, $response->getStatus());
 
     }
 
@@ -130,17 +117,10 @@ class ServerRangeTest extends AbstractServer{
      */
     function testCrazyRange() {
 
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=8-4',
-        ];
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
+        $request = new HTTP\Request('GET', '/files/test.txt', ['Range' => 'bytes=8-4']);
+        $response = $this->request($request);
 
-        $this->assertEquals(416, $this->response->status);
+        $this->assertEquals(416, $response->getStatus());
 
     }
 
@@ -149,132 +129,105 @@ class ServerRangeTest extends AbstractServer{
      */
     function testIfRangeEtag() {
 
-        $node = $this->server->tree->getNodeForPath('test.txt');
-
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=2-5',
-            'HTTP_IF_RANGE'  => $node->getETag(),
-        ];
-        $filename = SABRE_TEMPDIR . '/test.txt';
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
+        $request = new HTTP\Request('GET', '/files/test.txt', [
+            'Range' => 'bytes=2-5',
+            'If-Range' => '"' . md5('Test contents') . '"',
+        ]);
+        $response = $this->request($request);
 
         $this->assertEquals([
             'X-Sabre-Version' => [Version::VERSION],
             'Content-Type'    => ['application/octet-stream'],
             'Content-Length'  => [4],
             'Content-Range'   => ['bytes 2-5/13'],
-            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
-            'ETag'            => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
+            'ETag'            => ['"' . md5('Test contents') . '"'],
+            'Last-Modified'   => [$this->lastModified],
             ],
-            $this->response->getHeaders()
-         );
+            $response->getHeaders()
+        );
 
-        $this->assertEquals(206, $this->response->status);
-        $this->assertEquals('st c', stream_get_contents($this->response->body, 4));
+        $this->assertEquals(206, $response->getStatus());
+        $this->assertEquals('st c', $response->getBodyAsString());
 
     }
 
     /**
-     * @depends testRange
+     * @depends testIfRangeEtag
      */
     function testIfRangeEtagIncorrect() {
 
-        $node = $this->server->tree->getNodeForPath('test.txt');
-
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=2-5',
-            'HTTP_IF_RANGE'  => $node->getETag() . 'blabla',
-        ];
-        $filename = SABRE_TEMPDIR . '/test.txt';
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
+        $request = new HTTP\Request('GET', '/files/test.txt', [
+            'Range' => 'bytes=2-5',
+            'If-Range' => '"foobar"',
+        ]);
+        $response = $this->request($request);
 
         $this->assertEquals([
             'X-Sabre-Version' => [Version::VERSION],
             'Content-Type'    => ['application/octet-stream'],
             'Content-Length'  => [13],
-            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
-            'ETag'            => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
+            'ETag'            => ['"' . md5('Test contents') . '"'],
+            'Last-Modified'   => [$this->lastModified],
             ],
-            $this->response->getHeaders()
-         );
+            $response->getHeaders()
+        );
 
-        $this->assertEquals(200, $this->response->status);
-        $this->assertEquals('Test contents', stream_get_contents($this->response->body));
+        $this->assertEquals(200, $response->getStatus());
+        $this->assertEquals('Test contents', $response->getBodyAsString());
 
     }
 
     /**
-     * @depends testRange
+     * @depends testIfRangeEtag
      */
     function testIfRangeModificationDate() {
 
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=2-5',
-            'HTTP_IF_RANGE'  => 'tomorrow',
-        ];
-        $filename = SABRE_TEMPDIR . '/test.txt';
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
+        $request = new HTTP\Request('GET', '/files/test.txt', [
+            'Range' => 'bytes=2-5',
+            'If-Range' => 'tomorrow',
+        ]);
+        $response = $this->request($request);
 
         $this->assertEquals([
             'X-Sabre-Version' => [Version::VERSION],
             'Content-Type'    => ['application/octet-stream'],
             'Content-Length'  => [4],
             'Content-Range'   => ['bytes 2-5/13'],
-            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
-            'ETag'            => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
+            'ETag'            => ['"' . md5('Test contents') . '"'],
+            'Last-Modified'   => [$this->lastModified],
             ],
-            $this->response->getHeaders()
-         );
+            $response->getHeaders()
+        );
 
-        $this->assertEquals(206, $this->response->status);
-        $this->assertEquals('st c', stream_get_contents($this->response->body, 4));
+        $this->assertEquals(206, $response->getStatus());
+        $this->assertEquals('st c', $response->getBodyAsString());
 
     }
 
     /**
-     * @depends testRange
+     * @depends testIfRangeModificationDate
      */
     function testIfRangeModificationDateModified() {
 
-        $serverVars = [
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'GET',
-            'HTTP_RANGE'     => 'bytes=2-5',
-            'HTTP_IF_RANGE'  => '-2 years',
-        ];
-        $filename = SABRE_TEMPDIR . '/test.txt';
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
+        $request = new HTTP\Request('GET', '/files/test.txt', [
+            'Range' => 'bytes=2-5',
+            'If-Range' => '-2 years',
+        ]);
+        $response = $this->request($request);
 
         $this->assertEquals([
             'X-Sabre-Version' => [Version::VERSION],
             'Content-Type'    => ['application/octet-stream'],
             'Content-Length'  => [13],
-            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
-            'ETag'            => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"'],
+            'ETag'            => ['"' . md5('Test contents') . '"'],
+            'Last-Modified'   => [$this->lastModified],
             ],
-            $this->response->getHeaders()
-         );
+            $response->getHeaders()
+        );
 
-        $this->assertEquals(200, $this->response->status);
-        $this->assertEquals('Test contents', stream_get_contents($this->response->body));
+        $this->assertEquals(200, $response->getStatus());
+        $this->assertEquals('Test contents', $response->getBodyAsString());
 
     }
+
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list