scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2 namespace GuzzleHttp\Tests\Message;
3  
4 use GuzzleHttp\Message\AbstractMessage;
5 use GuzzleHttp\Message\Request;
6 use GuzzleHttp\Message\Response;
7 use GuzzleHttp\Stream\Stream;
8  
9 /**
10 * @covers \GuzzleHttp\Message\AbstractMessage
11 */
12 class AbstractMessageTest extends \PHPUnit_Framework_TestCase
13 {
14 public function testHasProtocolVersion()
15 {
16 $m = new Request('GET', '/');
17 $this->assertEquals(1.1, $m->getProtocolVersion());
18 }
19  
20 public function testHasHeaders()
21 {
22 $m = new Request('GET', 'http://foo.com');
23 $this->assertFalse($m->hasHeader('foo'));
24 $m->addHeader('foo', 'bar');
25 $this->assertTrue($m->hasHeader('foo'));
26 }
27  
28 public function testInitializesMessageWithProtocolVersionOption()
29 {
30 $m = new Request('GET', '/', [], null, [
31 'protocol_version' => '10'
32 ]);
33 $this->assertEquals(10, $m->getProtocolVersion());
34 }
35  
36 public function testHasBody()
37 {
38 $m = new Request('GET', 'http://foo.com');
39 $this->assertNull($m->getBody());
40 $s = Stream::factory('test');
41 $m->setBody($s);
42 $this->assertSame($s, $m->getBody());
43 $this->assertFalse($m->hasHeader('Content-Length'));
44 }
45  
46 public function testCanRemoveBodyBySettingToNullAndRemovesCommonBodyHeaders()
47 {
48 $m = new Request('GET', 'http://foo.com');
49 $m->setBody(Stream::factory('foo'));
50 $m->setHeader('Content-Length', 3)->setHeader('Transfer-Encoding', 'chunked');
51 $m->setBody(null);
52 $this->assertNull($m->getBody());
53 $this->assertFalse($m->hasHeader('Content-Length'));
54 $this->assertFalse($m->hasHeader('Transfer-Encoding'));
55 }
56  
57 public function testCastsToString()
58 {
59 $m = new Request('GET', 'http://foo.com');
60 $m->setHeader('foo', 'bar');
61 $m->setBody(Stream::factory('baz'));
62 $this->assertEquals("GET / HTTP/1.1\r\nHost: foo.com\r\nfoo: bar\r\n\r\nbaz", (string) $m);
63 }
64  
65 public function parseParamsProvider()
66 {
67 $res1 = array(
68 array(
69 '<http:/.../front.jpeg>',
70 'rel' => 'front',
71 'type' => 'image/jpeg',
72 ),
73 array(
74 '<http://.../back.jpeg>',
75 'rel' => 'back',
76 'type' => 'image/jpeg',
77 ),
78 );
79  
80 return array(
81 array(
82 '<http:/.../front.jpeg>; rel="front"; type="image/jpeg", <http://.../back.jpeg>; rel=back; type="image/jpeg"',
83 $res1
84 ),
85 array(
86 '<http:/.../front.jpeg>; rel="front"; type="image/jpeg",<http://.../back.jpeg>; rel=back; type="image/jpeg"',
87 $res1
88 ),
89 array(
90 'foo="baz"; bar=123, boo, test="123", foobar="foo;bar"',
91 array(
92 array('foo' => 'baz', 'bar' => '123'),
93 array('boo'),
94 array('test' => '123'),
95 array('foobar' => 'foo;bar')
96 )
97 ),
98 array(
99 '<http://.../side.jpeg?test=1>; rel="side"; type="image/jpeg",<http://.../side.jpeg?test=2>; rel=side; type="image/jpeg"',
100 array(
101 array('<http://.../side.jpeg?test=1>', 'rel' => 'side', 'type' => 'image/jpeg'),
102 array('<http://.../side.jpeg?test=2>', 'rel' => 'side', 'type' => 'image/jpeg')
103 )
104 ),
105 array(
106 '',
107 array()
108 )
109 );
110 }
111  
112 /**
113 * @dataProvider parseParamsProvider
114 */
115 public function testParseParams($header, $result)
116 {
117 $request = new Request('GET', '/', ['foo' => $header]);
118 $this->assertEquals($result, Request::parseHeader($request, 'foo'));
119 }
120  
121 public function testAddsHeadersWhenNotPresent()
122 {
123 $h = new Request('GET', 'http://foo.com');
124 $h->addHeader('foo', 'bar');
125 $this->assertInternalType('string', $h->getHeader('foo'));
126 $this->assertEquals('bar', $h->getHeader('foo'));
127 }
128  
129 public function testAddsHeadersWhenPresentSameCase()
130 {
131 $h = new Request('GET', 'http://foo.com');
132 $h->addHeader('foo', 'bar')->addHeader('foo', 'baz');
133 $this->assertEquals('bar, baz', $h->getHeader('foo'));
134 $this->assertEquals(['bar', 'baz'], $h->getHeader('foo', true));
135 }
136  
137 public function testAddsMultipleHeaders()
138 {
139 $h = new Request('GET', 'http://foo.com');
140 $h->addHeaders([
141 'foo' => ' bar',
142 'baz' => [' bam ', 'boo']
143 ]);
144 $this->assertEquals([
145 'foo' => ['bar'],
146 'baz' => ['bam', 'boo'],
147 'Host' => ['foo.com']
148 ], $h->getHeaders());
149 }
150  
151 public function testAddsHeadersWhenPresentDifferentCase()
152 {
153 $h = new Request('GET', 'http://foo.com');
154 $h->addHeader('Foo', 'bar')->addHeader('fOO', 'baz');
155 $this->assertEquals('bar, baz', $h->getHeader('foo'));
156 }
157  
158 public function testAddsHeadersWithArray()
159 {
160 $h = new Request('GET', 'http://foo.com');
161 $h->addHeader('Foo', ['bar', 'baz']);
162 $this->assertEquals('bar, baz', $h->getHeader('foo'));
163 }
164  
165 /**
166 * @expectedException \InvalidArgumentException
167 */
168 public function testThrowsExceptionWhenInvalidValueProvidedToAddHeader()
169 {
170 (new Request('GET', 'http://foo.com'))->addHeader('foo', false);
171 }
172  
173 public function testGetHeadersReturnsAnArrayOfOverTheWireHeaderValues()
174 {
175 $h = new Request('GET', 'http://foo.com');
176 $h->addHeader('foo', 'bar');
177 $h->addHeader('Foo', 'baz');
178 $h->addHeader('boO', 'test');
179 $result = $h->getHeaders();
180 $this->assertInternalType('array', $result);
181 $this->assertArrayHasKey('Foo', $result);
182 $this->assertArrayNotHasKey('foo', $result);
183 $this->assertArrayHasKey('boO', $result);
184 $this->assertEquals(['bar', 'baz'], $result['Foo']);
185 $this->assertEquals(['test'], $result['boO']);
186 }
187  
188 public function testSetHeaderOverwritesExistingValues()
189 {
190 $h = new Request('GET', 'http://foo.com');
191 $h->setHeader('foo', 'bar');
192 $this->assertEquals('bar', $h->getHeader('foo'));
193 $h->setHeader('Foo', 'baz');
194 $this->assertEquals('baz', $h->getHeader('foo'));
195 $this->assertArrayHasKey('Foo', $h->getHeaders());
196 }
197  
198 public function testSetHeaderOverwritesExistingValuesUsingHeaderArray()
199 {
200 $h = new Request('GET', 'http://foo.com');
201 $h->setHeader('foo', ['bar']);
202 $this->assertEquals('bar', $h->getHeader('foo'));
203 }
204  
205 public function testSetHeaderOverwritesExistingValuesUsingArray()
206 {
207 $h = new Request('GET', 'http://foo.com');
208 $h->setHeader('foo', ['bar']);
209 $this->assertEquals('bar', $h->getHeader('foo'));
210 }
211  
212 /**
213 * @expectedException \InvalidArgumentException
214 */
215 public function testThrowsExceptionWhenInvalidValueProvidedToSetHeader()
216 {
217 (new Request('GET', 'http://foo.com'))->setHeader('foo', false);
218 }
219  
220 public function testSetHeadersOverwritesAllHeaders()
221 {
222 $h = new Request('GET', 'http://foo.com');
223 $h->setHeader('foo', 'bar');
224 $h->setHeaders(['foo' => 'a', 'boo' => 'b']);
225 $this->assertEquals(['foo' => ['a'], 'boo' => ['b']], $h->getHeaders());
226 }
227  
228 public function testChecksIfCaseInsensitiveHeaderIsPresent()
229 {
230 $h = new Request('GET', 'http://foo.com');
231 $h->setHeader('foo', 'bar');
232 $this->assertTrue($h->hasHeader('foo'));
233 $this->assertTrue($h->hasHeader('Foo'));
234 $h->setHeader('fOo', 'bar');
235 $this->assertTrue($h->hasHeader('Foo'));
236 }
237  
238 public function testRemovesHeaders()
239 {
240 $h = new Request('GET', 'http://foo.com');
241 $h->setHeader('foo', 'bar');
242 $h->removeHeader('foo');
243 $this->assertFalse($h->hasHeader('foo'));
244 $h->setHeader('Foo', 'bar');
245 $h->removeHeader('FOO');
246 $this->assertFalse($h->hasHeader('foo'));
247 }
248  
249 public function testReturnsCorrectTypeWhenMissing()
250 {
251 $h = new Request('GET', 'http://foo.com');
252 $this->assertInternalType('string', $h->getHeader('foo'));
253 $this->assertInternalType('array', $h->getHeader('foo', true));
254 }
255  
256 public function testSetsIntegersAndFloatsAsHeaders()
257 {
258 $h = new Request('GET', 'http://foo.com');
259 $h->setHeader('foo', 10);
260 $h->setHeader('bar', 10.5);
261 $h->addHeader('foo', 10);
262 $h->addHeader('bar', 10.5);
263 $this->assertSame('10, 10', $h->getHeader('foo'));
264 $this->assertSame('10.5, 10.5', $h->getHeader('bar'));
265 }
266  
267 public function testGetsResponseStartLine()
268 {
269 $m = new Response(200);
270 $this->assertEquals('HTTP/1.1 200 OK', Response::getStartLine($m));
271 }
272  
273 /**
274 * @expectedException \InvalidArgumentException
275 */
276 public function testThrowsWhenMessageIsUnknown()
277 {
278 $m = $this->getMockBuilder('GuzzleHttp\Message\AbstractMessage')
279 ->getMockForAbstractClass();
280 AbstractMessage::getStartLine($m);
281 }
282 }