etag test & fixes; set default etag mode for temp streams to crc32(b)
[m6w6/ext-http] / phpunit / MessageBodyTest.php
1 <?php
2
3 class MessageBodyTest extends PHPUnit_Framework_TestCase {
4 protected $file, $temp;
5
6 function setUp() {
7 $this->file = new http\Message\Body(fopen(__FILE__, "r"));
8 $this->temp = new http\Message\Body();
9 }
10
11 function testStat() {
12 $this->assertEquals(filesize(__FILE__), $this->file->stat("size"));
13 $this->assertEquals(filemtime(__FILE__), $this->file->stat("mtime"));
14 $this->assertEquals(fileatime(__FILE__), $this->file->stat("atime"));
15 $this->assertEquals(filectime(__FILE__), $this->file->stat("ctime"));
16 $this->assertEquals(
17 array(
18 "size" => 0,
19 "mtime" => 0,
20 "atime" => 0,
21 "ctime" => 0,
22 ),
23 $this->temp->stat()
24 );
25 }
26
27 function testAppend() {
28 $this->assertEquals(0, $this->file->append("nope"));
29 $this->assertEquals(3, $this->temp->append("yes"));
30 }
31
32 function testAdd() {
33 $this->assertTrue(
34 $this->temp->add(
35 array(
36 "foo" => "bar",
37 "more" => array(
38 "bah", "baz", "fuz"
39 ),
40 ),
41 array(
42 array(
43 "file" => __FILE__,
44 "name" => "upload",
45 "type" => "text/plain",
46 )
47 )
48 )
49 );
50
51 $file = str_replace("%", "%c", file_get_contents(__FILE__));
52 $this->assertStringMatchesFormat(
53 "--%x.%x\r\n".
54 "Content-Disposition: form-data; name=\"foo\"\r\n".
55 "\r\n".
56 "bar\r\n".
57 "--%x.%x\r\n".
58 "Content-Disposition: form-data; name=\"more[0]\"\r\n".
59 "\r\n".
60 "bah\r\n".
61 "--%x.%x\r\n".
62 "Content-Disposition: form-data; name=\"more[1]\"\r\n".
63 "\r\n".
64 "baz\r\n".
65 "--%x.%x\r\n".
66 "Content-Disposition: form-data; name=\"more[2]\"\r\n".
67 "\r\n".
68 "fuz\r\n".
69 "--%x.%x\r\n".
70 "Content-Disposition: attachment; name=\"upload\"; filename=\"MessageBodyTest.php\"\r\n".
71 "Content-Transfer-Encoding: binary\r\n".
72 "Content-Type: text/plain\r\n".
73 "\r\n".
74 "{$file}\r\n".
75 "--%x.%x--\r\n".
76 "",
77 str_replace("\r", "", $this->temp) // phpunit replaces \r\n with \n
78 );
79 }
80
81 function testEtag() {
82 $s = stat(__FILE__);
83 $this->assertEquals(
84 sprintf(
85 "%lx-%lx-%lx",
86 $s["ino"],$s["mtime"],$s["size"]
87 ),
88 $this->file->etag()
89 );
90 $this->assertEquals(crc32(""), $this->temp->etag());
91 }
92
93 function testToStream() {
94 $this->file->toStream($f = fopen("php://temp", "w"));
95 fseek($f, 0, SEEK_SET);
96 $this->assertEquals(
97 file_get_contents(__FILE__),
98 fread($f, filesize(__FILE__))
99 );
100 }
101
102 function testToCallback() {
103 $s = "";
104 $this->file->toCallback(
105 function($body, $string) use (&$s) { $s.=$string; }
106 );
107 $this->assertEquals($s, (string) $this->file);
108 }
109 }