1 | <?php |
---|
2 | // $Id: encoding_test.php 1571 2007-09-07 17:14:32Z pp11 $ |
---|
3 | require_once(dirname(__FILE__) . '/../autorun.php'); |
---|
4 | require_once(dirname(__FILE__) . '/../url.php'); |
---|
5 | require_once(dirname(__FILE__) . '/../socket.php'); |
---|
6 | |
---|
7 | Mock::generate('SimpleSocket'); |
---|
8 | |
---|
9 | class TestOfEncodedParts extends UnitTestCase { |
---|
10 | |
---|
11 | function testFormEncodedAsKeyEqualsValue() { |
---|
12 | $pair = new SimpleEncodedPair('a', 'A'); |
---|
13 | $this->assertEqual($pair->asRequest(), 'a=A'); |
---|
14 | } |
---|
15 | |
---|
16 | function testMimeEncodedAsHeadersAndContent() { |
---|
17 | $pair = new SimpleEncodedPair('a', 'A'); |
---|
18 | $this->assertEqual( |
---|
19 | $pair->asMime(), |
---|
20 | "Content-Disposition: form-data; name=\"a\"\r\n\r\nA"); |
---|
21 | } |
---|
22 | |
---|
23 | function testAttachmentEncodedAsHeadersWithDispositionAndContent() { |
---|
24 | $part = new SimpleAttachment('a', 'A', 'aaa.txt'); |
---|
25 | $this->assertEqual( |
---|
26 | $part->asMime(), |
---|
27 | "Content-Disposition: form-data; name=\"a\"; filename=\"aaa.txt\"\r\n" . |
---|
28 | "Content-Type: text/plain\r\n\r\nA"); |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | class TestOfEncoding extends UnitTestCase { |
---|
33 | var $_content_so_far; |
---|
34 | |
---|
35 | function write($content) { |
---|
36 | $this->_content_so_far .= $content; |
---|
37 | } |
---|
38 | |
---|
39 | function clear() { |
---|
40 | $this->_content_so_far = ''; |
---|
41 | } |
---|
42 | |
---|
43 | function assertWritten($encoding, $content, $message = '%s') { |
---|
44 | $this->clear(); |
---|
45 | $encoding->writeTo($this); |
---|
46 | $this->assertIdentical($this->_content_so_far, $content, $message); |
---|
47 | } |
---|
48 | |
---|
49 | function testGetEmpty() { |
---|
50 | $encoding = &new SimpleGetEncoding(); |
---|
51 | $this->assertIdentical($encoding->getValue('a'), false); |
---|
52 | $this->assertIdentical($encoding->asUrlRequest(), ''); |
---|
53 | } |
---|
54 | |
---|
55 | function testPostEmpty() { |
---|
56 | $encoding = &new SimplePostEncoding(); |
---|
57 | $this->assertIdentical($encoding->getValue('a'), false); |
---|
58 | $this->assertWritten($encoding, ''); |
---|
59 | } |
---|
60 | |
---|
61 | function testPrefilled() { |
---|
62 | $encoding = &new SimplePostEncoding(array('a' => 'aaa')); |
---|
63 | $this->assertIdentical($encoding->getValue('a'), 'aaa'); |
---|
64 | $this->assertWritten($encoding, 'a=aaa'); |
---|
65 | } |
---|
66 | |
---|
67 | function testPrefilledWithTwoLevels() { |
---|
68 | $query = array('a' => array('aa' => 'aaa')); |
---|
69 | $encoding = &new SimplePostEncoding($query); |
---|
70 | $this->assertTrue($encoding->hasMoreThanOneLevel($query)); |
---|
71 | $this->assertEqual($encoding->rewriteArrayWithMultipleLevels($query), array('a[aa]' => 'aaa')); |
---|
72 | $this->assertIdentical($encoding->getValue('a[aa]'), 'aaa'); |
---|
73 | $this->assertWritten($encoding, 'a%5Baa%5D=aaa'); |
---|
74 | } |
---|
75 | |
---|
76 | function testPrefilledWithThreeLevels() { |
---|
77 | $query = array('a' => array('aa' => array('aaa' => 'aaaa'))); |
---|
78 | $encoding = &new SimplePostEncoding($query); |
---|
79 | $this->assertTrue($encoding->hasMoreThanOneLevel($query)); |
---|
80 | $this->assertEqual($encoding->rewriteArrayWithMultipleLevels($query), array('a[aa][aaa]' => 'aaaa')); |
---|
81 | $this->assertIdentical($encoding->getValue('a[aa][aaa]'), 'aaaa'); |
---|
82 | $this->assertWritten($encoding, 'a%5Baa%5D%5Baaa%5D=aaaa'); |
---|
83 | } |
---|
84 | |
---|
85 | function testPrefilledWithObject() { |
---|
86 | $encoding = &new SimplePostEncoding(new SimpleEncoding(array('a' => 'aaa'))); |
---|
87 | $this->assertIdentical($encoding->getValue('a'), 'aaa'); |
---|
88 | $this->assertWritten($encoding, 'a=aaa'); |
---|
89 | } |
---|
90 | |
---|
91 | function testMultiplePrefilled() { |
---|
92 | $query = array('a' => array('a1', 'a2')); |
---|
93 | $encoding = &new SimplePostEncoding($query); |
---|
94 | $this->assertTrue($encoding->hasMoreThanOneLevel($query)); |
---|
95 | $this->assertEqual($encoding->rewriteArrayWithMultipleLevels($query), array('a[0]' => 'a1', 'a[1]' => 'a2')); |
---|
96 | $this->assertIdentical($encoding->getValue('a[0]'), 'a1'); |
---|
97 | $this->assertIdentical($encoding->getValue('a[1]'), 'a2'); |
---|
98 | $this->assertWritten($encoding, 'a%5B0%5D=a1&a%5B1%5D=a2'); |
---|
99 | } |
---|
100 | |
---|
101 | function testSingleParameter() { |
---|
102 | $encoding = &new SimplePostEncoding(); |
---|
103 | $encoding->add('a', 'Hello'); |
---|
104 | $this->assertEqual($encoding->getValue('a'), 'Hello'); |
---|
105 | $this->assertWritten($encoding, 'a=Hello'); |
---|
106 | } |
---|
107 | |
---|
108 | function testFalseParameter() { |
---|
109 | $encoding = &new SimplePostEncoding(); |
---|
110 | $encoding->add('a', false); |
---|
111 | $this->assertEqual($encoding->getValue('a'), false); |
---|
112 | $this->assertWritten($encoding, ''); |
---|
113 | } |
---|
114 | |
---|
115 | function testUrlEncoding() { |
---|
116 | $encoding = &new SimplePostEncoding(); |
---|
117 | $encoding->add('a', 'Hello there!'); |
---|
118 | $this->assertWritten($encoding, 'a=Hello+there%21'); |
---|
119 | } |
---|
120 | |
---|
121 | function testUrlEncodingOfKey() { |
---|
122 | $encoding = &new SimplePostEncoding(); |
---|
123 | $encoding->add('a!', 'Hello'); |
---|
124 | $this->assertWritten($encoding, 'a%21=Hello'); |
---|
125 | } |
---|
126 | |
---|
127 | function testMultipleParameter() { |
---|
128 | $encoding = &new SimplePostEncoding(); |
---|
129 | $encoding->add('a', 'Hello'); |
---|
130 | $encoding->add('b', 'Goodbye'); |
---|
131 | $this->assertWritten($encoding, 'a=Hello&b=Goodbye'); |
---|
132 | } |
---|
133 | |
---|
134 | function testEmptyParameters() { |
---|
135 | $encoding = &new SimplePostEncoding(); |
---|
136 | $encoding->add('a', ''); |
---|
137 | $encoding->add('b', ''); |
---|
138 | $this->assertWritten($encoding, 'a=&b='); |
---|
139 | } |
---|
140 | |
---|
141 | function testRepeatedParameter() { |
---|
142 | $encoding = &new SimplePostEncoding(); |
---|
143 | $encoding->add('a', 'Hello'); |
---|
144 | $encoding->add('a', 'Goodbye'); |
---|
145 | $this->assertIdentical($encoding->getValue('a'), array('Hello', 'Goodbye')); |
---|
146 | $this->assertWritten($encoding, 'a=Hello&a=Goodbye'); |
---|
147 | } |
---|
148 | |
---|
149 | function testAddingLists() { |
---|
150 | $encoding = &new SimplePostEncoding(); |
---|
151 | $encoding->add('a', array('Hello', 'Goodbye')); |
---|
152 | $this->assertIdentical($encoding->getValue('a'), array('Hello', 'Goodbye')); |
---|
153 | $this->assertWritten($encoding, 'a=Hello&a=Goodbye'); |
---|
154 | } |
---|
155 | |
---|
156 | function testMergeInHash() { |
---|
157 | $encoding = &new SimpleGetEncoding(array('a' => 'A1', 'b' => 'B')); |
---|
158 | $encoding->merge(array('a' => 'A2')); |
---|
159 | $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2')); |
---|
160 | $this->assertIdentical($encoding->getValue('b'), 'B'); |
---|
161 | } |
---|
162 | |
---|
163 | function testMergeInObject() { |
---|
164 | $encoding = &new SimpleGetEncoding(array('a' => 'A1', 'b' => 'B')); |
---|
165 | $encoding->merge(new SimpleEncoding(array('a' => 'A2'))); |
---|
166 | $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2')); |
---|
167 | $this->assertIdentical($encoding->getValue('b'), 'B'); |
---|
168 | } |
---|
169 | |
---|
170 | function testPrefilledMultipart() { |
---|
171 | $encoding = &new SimpleMultipartEncoding(array('a' => 'aaa'), 'boundary'); |
---|
172 | $this->assertIdentical($encoding->getValue('a'), 'aaa'); |
---|
173 | $this->assertwritten($encoding, |
---|
174 | "--boundary\r\n" . |
---|
175 | "Content-Disposition: form-data; name=\"a\"\r\n" . |
---|
176 | "\r\n" . |
---|
177 | "aaa\r\n" . |
---|
178 | "--boundary--\r\n"); |
---|
179 | } |
---|
180 | |
---|
181 | function testAttachment() { |
---|
182 | $encoding = &new SimpleMultipartEncoding(array(), 'boundary'); |
---|
183 | $encoding->attach('a', 'aaa', 'aaa.txt'); |
---|
184 | $this->assertIdentical($encoding->getValue('a'), 'aaa.txt'); |
---|
185 | $this->assertwritten($encoding, |
---|
186 | "--boundary\r\n" . |
---|
187 | "Content-Disposition: form-data; name=\"a\"; filename=\"aaa.txt\"\r\n" . |
---|
188 | "Content-Type: text/plain\r\n" . |
---|
189 | "\r\n" . |
---|
190 | "aaa\r\n" . |
---|
191 | "--boundary--\r\n"); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | class TestOfFormHeaders extends UnitTestCase { |
---|
196 | |
---|
197 | function testEmptyEncodingWritesZeroContentLength() { |
---|
198 | $socket = &new MockSimpleSocket(); |
---|
199 | $socket->expectArgumentsAt(0, 'write', array("Content-Length: 0\r\n")); |
---|
200 | $socket->expectArgumentsAt(1, 'write', array("Content-Type: application/x-www-form-urlencoded\r\n")); |
---|
201 | $encoding = &new SimplePostEncoding(); |
---|
202 | $encoding->writeHeadersTo($socket); |
---|
203 | } |
---|
204 | |
---|
205 | function testEmptyMultipartEncodingWritesEndBoundaryContentLength() { |
---|
206 | $socket = &new MockSimpleSocket(); |
---|
207 | $socket->expectArgumentsAt(0, 'write', array("Content-Length: 14\r\n")); |
---|
208 | $socket->expectArgumentsAt(1, 'write', array("Content-Type: multipart/form-data, boundary=boundary\r\n")); |
---|
209 | $encoding = &new SimpleMultipartEncoding(array(), 'boundary'); |
---|
210 | $encoding->writeHeadersTo($socket); |
---|
211 | } |
---|
212 | } |
---|
213 | ?> |
---|