File: MessageBodyTest.php

package info (click to toggle)
php-pecl-http 2.0.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,436 kB
  • ctags: 1,839
  • sloc: ansic: 15,322; php: 726; xml: 254; makefile: 10; pascal: 3
file content (135 lines) | stat: -rw-r--r-- 4,097 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

class MessageBodyTest extends PHPUnit_Framework_TestCase {
    protected $file, $temp;

    function setUp() {
        $this->file = new http\Message\Body(fopen(__FILE__, "r"));
        $this->temp = new http\Message\Body();
    }

    function testStat() {
        $this->assertEquals(filesize(__FILE__), $this->file->stat("size"));
        $this->assertEquals(filemtime(__FILE__), $this->file->stat("mtime"));
        $this->assertEquals(fileatime(__FILE__), $this->file->stat("atime"));
        $this->assertEquals(filectime(__FILE__), $this->file->stat("ctime"));
        $this->assertEquals(
            (object) array(
                "size" => 0,
                "mtime" => 0,
                "atime" => 0,
                "ctime" => 0,
            ),
            $this->temp->stat()
        );
    }

    function testAppendError() {
    	$this->setExpectedException("http\Exception\RuntimeException");
        $this->file->append("nope");
    }
    function testAppend() {
        $this->temp->append("yes");
    }

    function testAddForm() {
        $this->temp->addForm(
            array(
                "foo" => "bar",
                "more" => array(
                    "bah", "baz", "fuz"
                ),
            ),
            array(
                array(
                    "file" => __FILE__,
                    "name" => "upload",
                    "type" => "text/plain",
                )
            )
        );

        $file = str_replace("%", "%c", file_get_contents(__FILE__));
        $this->assertStringMatchesFormat(
            "--%x.%x\r\n".
            "Content-Disposition: form-data; name=\"foo\"\r\n".
            "\r\n".
            "bar\r\n".
            "--%x.%x\r\n".
            "Content-Disposition: form-data; name=\"more[0]\"\r\n".
            "\r\n".
            "bah\r\n".
            "--%x.%x\r\n".
            "Content-Disposition: form-data; name=\"more[1]\"\r\n".
            "\r\n".
            "baz\r\n".
            "--%x.%x\r\n".
            "Content-Disposition: form-data; name=\"more[2]\"\r\n".
            "\r\n".
            "fuz\r\n".
            "--%x.%x\r\n".
            "Content-Disposition: form-data; name=\"upload\"; filename=\"MessageBodyTest.php\"\r\n".
            "Content-Transfer-Encoding: binary\r\n".
            "Content-Type: text/plain\r\n".
            "\r\n".
            "{$file}\r\n".
            "--%x.%x--\r\n".
            "",
            str_replace("\r", "", $this->temp) // phpunit replaces \r\n with \n
        );
    }

    function testAddPart() {
        $this->temp->addPart(new http\Message("This: is a header\n\nand this is the data\n"));
        $this->assertStringMatchesFormat(
            "--%x.%x\r\n".
            "This: is a header\r\n".
            "Content-Length: 21\r\n".
            "\r\n".
            "and this is the data\n\r\n".
            "--%x.%x--\r\n".
            "",
            str_replace("\r", "", $this->temp)
        );
    }

    function testEtag() {
        $s = stat(__FILE__);
        $this->assertEquals(
            sprintf(
                "%lx-%lx-%lx", 
                $s["ino"],$s["mtime"],$s["size"]
            ),
            $this->file->etag()
        );
        $this->assertEquals(crc32(""), $this->temp->etag());
    }

    function testToStream() {
        $this->file->toStream($f = fopen("php://temp", "w")); 
        fseek($f, 0, SEEK_SET);
        $this->assertEquals(
            file_get_contents(__FILE__), 
            fread($f, filesize(__FILE__))
        );
    }

    function testToCallback() {
        $s = "";
        $this->file->toCallback(
            function($body, $string) use (&$s) { $s.=$string; }
        );
        $this->assertEquals($s, (string) $this->file);
    }

    function testClone() {
        $this->assertEquals((string) $this->file, (string) clone $this->file);
    }
    
    function testGetResource() {
    	$stream = $this->file->getResource();
    	$this->assertTrue(is_resource($stream));
    	$stat = fstat($stream);
    	$this->assertEquals(filesize(__FILE__), $stat["size"]);
    }
}