scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Tests\Message; |
||
4 | |||
5 | use GuzzleHttp\Adapter\Transaction; |
||
6 | use GuzzleHttp\Client; |
||
7 | use GuzzleHttp\Event\BeforeEvent; |
||
8 | use GuzzleHttp\Message\Request; |
||
9 | use GuzzleHttp\Stream\NoSeekStream; |
||
10 | use GuzzleHttp\Stream\Stream; |
||
11 | use GuzzleHttp\Subscriber\Prepare; |
||
12 | |||
13 | /** |
||
14 | * @covers GuzzleHttp\Subscriber\Prepare |
||
15 | */ |
||
16 | class PrepareTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | public function testIgnoresRequestsWithNoBody() |
||
19 | { |
||
20 | $s = new Prepare(); |
||
21 | $t = $this->getTrans(); |
||
22 | $s->onBefore(new BeforeEvent($t)); |
||
23 | $this->assertFalse($t->getRequest()->hasHeader('Expect')); |
||
24 | } |
||
25 | |||
26 | public function testAppliesPostBody() |
||
27 | { |
||
28 | $s = new Prepare(); |
||
29 | $t = $this->getTrans(); |
||
30 | $p = $this->getMockBuilder('GuzzleHttp\Post\PostBody') |
||
31 | ->setMethods(['applyRequestHeaders']) |
||
32 | ->getMockForAbstractClass(); |
||
33 | $p->expects($this->once()) |
||
34 | ->method('applyRequestHeaders'); |
||
35 | $t->getRequest()->setBody($p); |
||
36 | $s->onBefore(new BeforeEvent($t)); |
||
37 | } |
||
38 | |||
39 | public function testAddsExpectHeaderWithTrue() |
||
40 | { |
||
41 | $s = new Prepare(); |
||
42 | $t = $this->getTrans(); |
||
43 | $t->getRequest()->getConfig()->set('expect', true); |
||
44 | $t->getRequest()->setBody(Stream::factory('foo')); |
||
45 | $s->onBefore(new BeforeEvent($t)); |
||
46 | $this->assertEquals('100-Continue', $t->getRequest()->getHeader('Expect')); |
||
47 | } |
||
48 | |||
49 | public function testAddsExpectHeaderBySize() |
||
50 | { |
||
51 | $s = new Prepare(); |
||
52 | $t = $this->getTrans(); |
||
53 | $t->getRequest()->getConfig()->set('expect', 2); |
||
54 | $t->getRequest()->setBody(Stream::factory('foo')); |
||
55 | $s->onBefore(new BeforeEvent($t)); |
||
56 | $this->assertTrue($t->getRequest()->hasHeader('Expect')); |
||
57 | } |
||
58 | |||
59 | public function testDoesNotModifyExpectHeaderIfPresent() |
||
60 | { |
||
61 | $s = new Prepare(); |
||
62 | $t = $this->getTrans(); |
||
63 | $t->getRequest()->setHeader('Expect', 'foo'); |
||
64 | $t->getRequest()->setBody(Stream::factory('foo')); |
||
65 | $s->onBefore(new BeforeEvent($t)); |
||
66 | $this->assertEquals('foo', $t->getRequest()->getHeader('Expect')); |
||
67 | } |
||
68 | |||
69 | public function testDoesAddExpectHeaderWhenSetToFalse() |
||
70 | { |
||
71 | $s = new Prepare(); |
||
72 | $t = $this->getTrans(); |
||
73 | $t->getRequest()->getConfig()->set('expect', false); |
||
74 | $t->getRequest()->setBody(Stream::factory('foo')); |
||
75 | $s->onBefore(new BeforeEvent($t)); |
||
76 | $this->assertFalse($t->getRequest()->hasHeader('Expect')); |
||
77 | } |
||
78 | |||
79 | public function testDoesNotAddExpectHeaderBySize() |
||
80 | { |
||
81 | $s = new Prepare(); |
||
82 | $t = $this->getTrans(); |
||
83 | $t->getRequest()->getConfig()->set('expect', 10); |
||
84 | $t->getRequest()->setBody(Stream::factory('foo')); |
||
85 | $s->onBefore(new BeforeEvent($t)); |
||
86 | $this->assertFalse($t->getRequest()->hasHeader('Expect')); |
||
87 | } |
||
88 | |||
89 | public function testAddsExpectHeaderForNonSeekable() |
||
90 | { |
||
91 | $s = new Prepare(); |
||
92 | $t = $this->getTrans(); |
||
93 | $t->getRequest()->setBody(new NoSeekStream(Stream::factory('foo'))); |
||
94 | $s->onBefore(new BeforeEvent($t)); |
||
95 | $this->assertTrue($t->getRequest()->hasHeader('Expect')); |
||
96 | } |
||
97 | |||
98 | public function testRemovesContentLengthWhenSendingWithChunked() |
||
99 | { |
||
100 | $s = new Prepare(); |
||
101 | $t = $this->getTrans(); |
||
102 | $t->getRequest()->setBody(Stream::factory('foo')); |
||
103 | $t->getRequest()->setHeader('Transfer-Encoding', 'chunked'); |
||
104 | $s->onBefore(new BeforeEvent($t)); |
||
105 | $this->assertFalse($t->getRequest()->hasHeader('Content-Length')); |
||
106 | } |
||
107 | |||
108 | public function testUsesProvidedContentLengthAndRemovesXferEncoding() |
||
109 | { |
||
110 | $s = new Prepare(); |
||
111 | $t = $this->getTrans(); |
||
112 | $t->getRequest()->setBody(Stream::factory('foo')); |
||
113 | $t->getRequest()->setHeader('Content-Length', '3'); |
||
114 | $t->getRequest()->setHeader('Transfer-Encoding', 'chunked'); |
||
115 | $s->onBefore(new BeforeEvent($t)); |
||
116 | $this->assertEquals(3, $t->getRequest()->getHeader('Content-Length')); |
||
117 | $this->assertFalse($t->getRequest()->hasHeader('Transfer-Encoding')); |
||
118 | } |
||
119 | |||
120 | public function testSetsContentTypeIfPossibleFromStream() |
||
121 | { |
||
122 | $body = $this->getMockBody(); |
||
123 | $sub = new Prepare(); |
||
124 | $t = $this->getTrans(); |
||
125 | $t->getRequest()->setBody($body); |
||
126 | $sub->onBefore(new BeforeEvent($t)); |
||
127 | $this->assertEquals( |
||
128 | 'image/jpeg', |
||
129 | $t->getRequest()->getHeader('Content-Type') |
||
130 | ); |
||
131 | $this->assertEquals(4, $t->getRequest()->getHeader('Content-Length')); |
||
132 | } |
||
133 | |||
134 | public function testDoesNotOverwriteExistingContentType() |
||
135 | { |
||
136 | $s = new Prepare(); |
||
137 | $t = $this->getTrans(); |
||
138 | $t->getRequest()->setBody($this->getMockBody()); |
||
139 | $t->getRequest()->setHeader('Content-Type', 'foo/baz'); |
||
140 | $s->onBefore(new BeforeEvent($t)); |
||
141 | $this->assertEquals( |
||
142 | 'foo/baz', |
||
143 | $t->getRequest()->getHeader('Content-Type') |
||
144 | ); |
||
145 | } |
||
146 | |||
147 | public function testSetsContentLengthIfPossible() |
||
148 | { |
||
149 | $s = new Prepare(); |
||
150 | $t = $this->getTrans(); |
||
151 | $t->getRequest()->setBody($this->getMockBody()); |
||
152 | $s->onBefore(new BeforeEvent($t)); |
||
153 | $this->assertEquals(4, $t->getRequest()->getHeader('Content-Length')); |
||
154 | } |
||
155 | |||
156 | public function testSetsTransferEncodingChunkedIfNeeded() |
||
157 | { |
||
158 | $r = new Request('PUT', '/'); |
||
159 | $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
||
160 | ->setMethods(['getSize']) |
||
161 | ->getMockForAbstractClass(); |
||
162 | $s->expects($this->exactly(2)) |
||
163 | ->method('getSize') |
||
164 | ->will($this->returnValue(null)); |
||
165 | $r->setBody($s); |
||
166 | $t = $this->getTrans($r); |
||
167 | $s = new Prepare(); |
||
168 | $s->onBefore(new BeforeEvent($t)); |
||
169 | $this->assertEquals('chunked', $r->getHeader('Transfer-Encoding')); |
||
170 | } |
||
171 | |||
172 | private function getTrans($request = null) |
||
173 | { |
||
174 | return new Transaction( |
||
175 | new Client(), |
||
176 | $request ?: new Request('PUT', '/') |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @return \GuzzleHttp\Stream\StreamInterface |
||
182 | */ |
||
183 | private function getMockBody() |
||
184 | { |
||
185 | $s = $this->getMockBuilder('GuzzleHttp\Stream\MetadataStreamInterface') |
||
186 | ->setMethods(['getMetadata', 'getSize']) |
||
187 | ->getMockForAbstractClass(); |
||
188 | $s->expects($this->any()) |
||
189 | ->method('getMetadata') |
||
190 | ->with('uri') |
||
191 | ->will($this->returnValue('/foo/baz/bar.jpg')); |
||
192 | $s->expects($this->exactly(2)) |
||
193 | ->method('getSize') |
||
194 | ->will($this->returnValue(4)); |
||
195 | |||
196 | return $s; |
||
197 | } |
||
198 | } |