let DEV_2 be trunk
[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 (object) array(
18 "size" => 0,
19 "mtime" => 0,
20 "atime" => 0,
21 "ctime" => 0,
22 ),
23 $this->temp->stat()
24 );
25 }
26
27 function testAppendError() {
28 $this->setExpectedException("http\Exception\RuntimeException");
29 $this->file->append("nope");
30 }
31 function testAppend() {
32 $this->temp->append("yes");
33 }
34
35 function testAddForm() {
36 $this->temp->addForm(
37 array(
38 "foo" => "bar",
39 "more" => array(
40 "bah", "baz", "fuz"
41 ),
42 ),
43 array(
44 array(
45 "file" => __FILE__,
46 "name" => "upload",
47 "type" => "text/plain",
48 )
49 )
50 );
51
52 $file = str_replace("%", "%c", file_get_contents(__FILE__));
53 $this->assertStringMatchesFormat(
54 "--%x.%x\r\n".
55 "Content-Disposition: form-data; name=\"foo\"\r\n".
56 "\r\n".
57 "bar\r\n".
58 "--%x.%x\r\n".
59 "Content-Disposition: form-data; name=\"more[0]\"\r\n".
60 "\r\n".
61 "bah\r\n".
62 "--%x.%x\r\n".
63 "Content-Disposition: form-data; name=\"more[1]\"\r\n".
64 "\r\n".
65 "baz\r\n".
66 "--%x.%x\r\n".
67 "Content-Disposition: form-data; name=\"more[2]\"\r\n".
68 "\r\n".
69 "fuz\r\n".
70 "--%x.%x\r\n".
71 "Content-Disposition: form-data; name=\"upload\"; filename=\"MessageBodyTest.php\"\r\n".
72 "Content-Transfer-Encoding: binary\r\n".
73 "Content-Type: text/plain\r\n".
74 "\r\n".
75 "{$file}\r\n".
76 "--%x.%x--\r\n".
77 "",
78 str_replace("\r", "", $this->temp) // phpunit replaces \r\n with \n
79 );
80 }
81
82 function testAddPart() {
83 $this->temp->addPart(new http\Message("This: is a header\n\nand this is the data\n"));
84 $this->assertStringMatchesFormat(
85 "--%x.%x\r\n".
86 "This: is a header\r\n".
87 "Content-Length: 21\r\n".
88 "\r\n".
89 "and this is the data\n\r\n".
90 "--%x.%x--\r\n".
91 "",
92 str_replace("\r", "", $this->temp)
93 );
94 }
95
96 function testEtag() {
97 $s = stat(__FILE__);
98 $this->assertEquals(
99 sprintf(
100 "%lx-%lx-%lx",
101 $s["ino"],$s["mtime"],$s["size"]
102 ),
103 $this->file->etag()
104 );
105 $this->assertEquals(crc32(""), $this->temp->etag());
106 }
107
108 function testToStream() {
109 $this->file->toStream($f = fopen("php://temp", "w"));
110 fseek($f, 0, SEEK_SET);
111 $this->assertEquals(
112 file_get_contents(__FILE__),
113 fread($f, filesize(__FILE__))
114 );
115 }
116
117 function testToCallback() {
118 $s = "";
119 $this->file->toCallback(
120 function($body, $string) use (&$s) { $s.=$string; }
121 );
122 $this->assertEquals($s, (string) $this->file);
123 }
124
125 function testClone() {
126 $this->assertEquals((string) $this->file, (string) clone $this->file);
127 }
128
129 function testGetResource() {
130 $stream = $this->file->getResource();
131 $this->assertTrue(is_resource($stream));
132 $stat = fstat($stream);
133 $this->assertEquals(filesize(__FILE__), $stat["size"]);
134 }
135 }