implement message body reference counting
[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 testAddForm() {
33 $this->assertTrue(
34 $this->temp->addForm(
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: form-data; 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 testAddPart() {
82 $this->temp->addPart(new http\Message("This: is a header\n\nand this is the data\n"));
83 $this->assertStringMatchesFormat(
84 "--%x.%x\r\n".
85 "This: is a header\r\n".
86 "Content-Length: 21\r\n".
87 "\r\n".
88 "and this is the data\n\r\n".
89 "--%x.%x--\r\n".
90 "",
91 str_replace("\r", "", $this->temp)
92 );
93 }
94
95 function testEtag() {
96 $s = stat(__FILE__);
97 $this->assertEquals(
98 sprintf(
99 "%lx-%lx-%lx",
100 $s["ino"],$s["mtime"],$s["size"]
101 ),
102 $this->file->etag()
103 );
104 $this->assertEquals(crc32(""), $this->temp->etag());
105 }
106
107 function testToStream() {
108 $this->file->toStream($f = fopen("php://temp", "w"));
109 fseek($f, 0, SEEK_SET);
110 $this->assertEquals(
111 file_get_contents(__FILE__),
112 fread($f, filesize(__FILE__))
113 );
114 }
115
116 function testToCallback() {
117 $s = "";
118 $this->file->toCallback(
119 function($body, $string) use (&$s) { $s.=$string; }
120 );
121 $this->assertEquals($s, (string) $this->file);
122 }
123
124 function testClone() {
125 $this->assertEquals((string) $this->file, (string) clone $this->file);
126 }
127
128 function testGetResource() {
129 $stream = $this->file->getResource();
130 $this->assertTrue(is_resource($stream));
131 $stat = fstat($stream);
132 $this->assertEquals(filesize(__FILE__), $stat["size"]);
133 }
134 }