scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Tests\Plugin\Redirect; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | use GuzzleHttp\Subscriber\History; |
||
7 | use GuzzleHttp\Subscriber\Mock; |
||
8 | |||
9 | /** |
||
10 | * @covers GuzzleHttp\Subscriber\Redirect |
||
11 | */ |
||
12 | class RedirectTest extends \PHPUnit_Framework_TestCase |
||
13 | { |
||
14 | public function testRedirectsRequests() |
||
15 | { |
||
16 | $mock = new Mock(); |
||
17 | $history = new History(); |
||
18 | $mock->addMultiple([ |
||
19 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n", |
||
20 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n", |
||
21 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
22 | ]); |
||
23 | |||
24 | $client = new Client(['base_url' => 'http://test.com']); |
||
25 | $client->getEmitter()->attach($history); |
||
26 | $client->getEmitter()->attach($mock); |
||
27 | |||
28 | $response = $client->get('/foo'); |
||
29 | $this->assertEquals(200, $response->getStatusCode()); |
||
30 | $this->assertContains('/redirect2', $response->getEffectiveUrl()); |
||
31 | |||
32 | // Ensure that two requests were sent |
||
33 | $requests = $history->getRequests(); |
||
34 | |||
35 | $this->assertEquals('/foo', $requests[0]->getPath()); |
||
36 | $this->assertEquals('GET', $requests[0]->getMethod()); |
||
37 | $this->assertEquals('/redirect1', $requests[1]->getPath()); |
||
38 | $this->assertEquals('GET', $requests[1]->getMethod()); |
||
39 | $this->assertEquals('/redirect2', $requests[2]->getPath()); |
||
40 | $this->assertEquals('GET', $requests[2]->getMethod()); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @expectedException \GuzzleHttp\Exception\TooManyRedirectsException |
||
45 | * @expectedExceptionMessage Will not follow more than |
||
46 | */ |
||
47 | public function testCanLimitNumberOfRedirects() |
||
48 | { |
||
49 | $mock = new Mock([ |
||
50 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n", |
||
51 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n", |
||
52 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect3\r\nContent-Length: 0\r\n\r\n", |
||
53 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect4\r\nContent-Length: 0\r\n\r\n", |
||
54 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect5\r\nContent-Length: 0\r\n\r\n", |
||
55 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect6\r\nContent-Length: 0\r\n\r\n" |
||
56 | ]); |
||
57 | $client = new Client(); |
||
58 | $client->getEmitter()->attach($mock); |
||
59 | $client->get('http://www.example.com/foo'); |
||
60 | } |
||
61 | |||
62 | public function testDefaultBehaviorIsToRedirectWithGetForEntityEnclosingRequests() |
||
63 | { |
||
64 | $h = new History(); |
||
65 | $mock = new Mock([ |
||
66 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
67 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
68 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
69 | ]); |
||
70 | $client = new Client(); |
||
71 | $client->getEmitter()->attach($mock); |
||
72 | $client->getEmitter()->attach($h); |
||
73 | $client->post('http://test.com/foo', [ |
||
74 | 'headers' => ['X-Baz' => 'bar'], |
||
75 | 'body' => 'testing' |
||
76 | ]); |
||
77 | |||
78 | $requests = $h->getRequests(); |
||
79 | $this->assertEquals('POST', $requests[0]->getMethod()); |
||
80 | $this->assertEquals('GET', $requests[1]->getMethod()); |
||
81 | $this->assertEquals('bar', (string) $requests[1]->getHeader('X-Baz')); |
||
82 | $this->assertEquals('GET', $requests[2]->getMethod()); |
||
83 | } |
||
84 | |||
85 | public function testCanRedirectWithStrictRfcCompliance() |
||
86 | { |
||
87 | $h = new History(); |
||
88 | $mock = new Mock([ |
||
89 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
90 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
91 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
92 | ]); |
||
93 | $client = new Client(['base_url' => 'http://test.com']); |
||
94 | $client->getEmitter()->attach($mock); |
||
95 | $client->getEmitter()->attach($h); |
||
96 | $client->post('/foo', [ |
||
97 | 'headers' => ['X-Baz' => 'bar'], |
||
98 | 'body' => 'testing', |
||
99 | 'allow_redirects' => ['max' => 10, 'strict' => true] |
||
100 | ]); |
||
101 | |||
102 | $requests = $h->getRequests(); |
||
103 | $this->assertEquals('POST', $requests[0]->getMethod()); |
||
104 | $this->assertEquals('POST', $requests[1]->getMethod()); |
||
105 | $this->assertEquals('bar', (string) $requests[1]->getHeader('X-Baz')); |
||
106 | $this->assertEquals('POST', $requests[2]->getMethod()); |
||
107 | } |
||
108 | |||
109 | public function testRewindsStreamWhenRedirectingIfNeeded() |
||
110 | { |
||
111 | $h = new History(); |
||
112 | $mock = new Mock([ |
||
113 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
114 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
115 | ]); |
||
116 | $client = new Client(['base_url' => 'http://test.com']); |
||
117 | $client->getEmitter()->attach($mock); |
||
118 | $client->getEmitter()->attach($h); |
||
119 | |||
120 | $body = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
||
121 | ->setMethods(['seek', 'read', 'eof', 'tell']) |
||
122 | ->getMockForAbstractClass(); |
||
123 | $body->expects($this->once())->method('tell')->will($this->returnValue(1)); |
||
124 | $body->expects($this->once())->method('seek')->will($this->returnValue(true)); |
||
125 | $body->expects($this->any())->method('eof')->will($this->returnValue(true)); |
||
126 | $body->expects($this->any())->method('read')->will($this->returnValue('foo')); |
||
127 | $client->post('/foo', [ |
||
128 | 'body' => $body, |
||
129 | 'allow_redirects' => ['max' => 5, 'strict' => true] |
||
130 | ]); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @expectedException \GuzzleHttp\Exception\CouldNotRewindStreamException |
||
135 | * @expectedExceptionMessage Unable to rewind the non-seekable request body after redirecting |
||
136 | */ |
||
137 | public function testThrowsExceptionWhenStreamCannotBeRewound() |
||
138 | { |
||
139 | $h = new History(); |
||
140 | $mock = new Mock([ |
||
141 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
142 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
143 | ]); |
||
144 | $client = new Client(); |
||
145 | $client->getEmitter()->attach($mock); |
||
146 | $client->getEmitter()->attach($h); |
||
147 | |||
148 | $body = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
||
149 | ->setMethods(['seek', 'read', 'eof', 'tell']) |
||
150 | ->getMockForAbstractClass(); |
||
151 | $body->expects($this->once())->method('tell')->will($this->returnValue(1)); |
||
152 | $body->expects($this->once())->method('seek')->will($this->returnValue(false)); |
||
153 | $body->expects($this->any())->method('eof')->will($this->returnValue(true)); |
||
154 | $body->expects($this->any())->method('read')->will($this->returnValue('foo')); |
||
155 | $client->post('http://example.com/foo', [ |
||
156 | 'body' => $body, |
||
157 | 'allow_redirects' => ['max' => 10, 'strict' => true] |
||
158 | ]); |
||
159 | } |
||
160 | |||
161 | public function testRedirectsCanBeDisabledPerRequest() |
||
162 | { |
||
163 | $client = new Client(['base_url' => 'http://test.com']); |
||
164 | $client->getEmitter()->attach(new Mock([ |
||
165 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
166 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
167 | ])); |
||
168 | $response = $client->put('/', ['body' => 'test', 'allow_redirects' => false]); |
||
169 | $this->assertEquals(301, $response->getStatusCode()); |
||
170 | } |
||
171 | |||
172 | public function testCanRedirectWithNoLeadingSlashAndQuery() |
||
173 | { |
||
174 | $h = new History(); |
||
175 | $client = new Client(['base_url' => 'http://www.foo.com']); |
||
176 | $client->getEmitter()->attach(new Mock([ |
||
177 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect?foo=bar\r\nContent-Length: 0\r\n\r\n", |
||
178 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
179 | ])); |
||
180 | $client->getEmitter()->attach($h); |
||
181 | $client->get('?foo=bar'); |
||
182 | $requests = $h->getRequests(); |
||
183 | $this->assertEquals('http://www.foo.com?foo=bar', $requests[0]->getUrl()); |
||
184 | $this->assertEquals('http://www.foo.com/redirect?foo=bar', $requests[1]->getUrl()); |
||
185 | } |
||
186 | |||
187 | public function testHandlesRedirectsWithSpacesProperly() |
||
188 | { |
||
189 | $client = new Client(['base_url' => 'http://www.foo.com']); |
||
190 | $client->getEmitter()->attach(new Mock([ |
||
191 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect 1\r\nContent-Length: 0\r\n\r\n", |
||
192 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" |
||
193 | ])); |
||
194 | $h = new History(); |
||
195 | $client->getEmitter()->attach($h); |
||
196 | $client->get('/foo'); |
||
197 | $reqs = $h->getRequests(); |
||
198 | $this->assertEquals('/redirect%201', $reqs[1]->getResource()); |
||
199 | } |
||
200 | |||
201 | public function testAddsRefererWhenPossible() |
||
202 | { |
||
203 | $client = new Client(['base_url' => 'http://www.foo.com']); |
||
204 | $client->getEmitter()->attach(new Mock([ |
||
205 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /bar\r\nContent-Length: 0\r\n\r\n", |
||
206 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" |
||
207 | ])); |
||
208 | $h = new History(); |
||
209 | $client->getEmitter()->attach($h); |
||
210 | $client->get('/foo', ['allow_redirects' => ['max' => 5, 'referer' => true]]); |
||
211 | $reqs = $h->getRequests(); |
||
212 | $this->assertEquals('http://www.foo.com/foo', $reqs[1]->getHeader('Referer')); |
||
213 | } |
||
214 | |||
215 | public function testDoesNotAddRefererWhenChangingProtocols() |
||
216 | { |
||
217 | $client = new Client(['base_url' => 'https://www.foo.com']); |
||
218 | $client->getEmitter()->attach(new Mock([ |
||
219 | "HTTP/1.1 301 Moved Permanently\r\n" |
||
220 | . "Location: http://www.foo.com/foo\r\n" |
||
221 | . "Content-Length: 0\r\n\r\n", |
||
222 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" |
||
223 | ])); |
||
224 | $h = new History(); |
||
225 | $client->getEmitter()->attach($h); |
||
226 | $client->get('/foo', ['allow_redirects' => ['max' => 5, 'referer' => true]]); |
||
227 | $reqs = $h->getRequests(); |
||
228 | $this->assertFalse($reqs[1]->hasHeader('Referer')); |
||
229 | } |
||
230 | |||
231 | public function testRedirectsWithGetOn303() |
||
232 | { |
||
233 | $h = new History(); |
||
234 | $mock = new Mock([ |
||
235 | "HTTP/1.1 303 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
236 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
||
237 | ]); |
||
238 | $client = new Client(); |
||
239 | $client->getEmitter()->attach($mock); |
||
240 | $client->getEmitter()->attach($h); |
||
241 | $client->post('http://test.com/foo', ['body' => 'testing']); |
||
242 | $requests = $h->getRequests(); |
||
243 | $this->assertEquals('POST', $requests[0]->getMethod()); |
||
244 | $this->assertEquals('GET', $requests[1]->getMethod()); |
||
245 | } |
||
246 | |||
247 | public function testRelativeLinkBasedLatestRequest() |
||
248 | { |
||
249 | $client = new Client(['base_url' => 'http://www.foo.com']); |
||
250 | $client->getEmitter()->attach(new Mock([ |
||
251 | "HTTP/1.1 301 Moved Permanently\r\nLocation: http://www.bar.com\r\nContent-Length: 0\r\n\r\n", |
||
252 | "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", |
||
253 | "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" |
||
254 | ])); |
||
255 | $response = $client->get('/'); |
||
256 | $this->assertEquals('http://www.bar.com/redirect', $response->getEffectiveUrl()); |
||
257 | } |
||
258 | } |