Open Journal Systems  3.3.0
RedirectPluginTest.php
1 <?php
2 
4 
10 
15 {
16  public function testRedirectsRequests()
17  {
18  // Flush the server and queue up a redirect followed by a successful response
19  $this->getServer()->flush();
20  $this->getServer()->enqueue(array(
21  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n",
22  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n",
23  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
24  ));
25 
26  // Create a client that uses the default redirect behavior
27  $client = new Client($this->getServer()->getUrl());
28  $history = new HistoryPlugin();
29  $client->addSubscriber($history);
30 
31  $request = $client->get('/foo');
32  $response = $request->send();
33  $this->assertEquals(200, $response->getStatusCode());
34  $this->assertContains('/redirect2', $response->getEffectiveUrl());
35 
36  // Ensure that two requests were sent
37  $requests = $this->getServer()->getReceivedRequests(true);
38  $this->assertEquals('/foo', $requests[0]->getResource());
39  $this->assertEquals('GET', $requests[0]->getMethod());
40  $this->assertEquals('/redirect1', $requests[1]->getResource());
41  $this->assertEquals('GET', $requests[1]->getMethod());
42  $this->assertEquals('/redirect2', $requests[2]->getResource());
43  $this->assertEquals('GET', $requests[2]->getMethod());
44 
45  // Ensure that the redirect count was incremented
46  $this->assertEquals(2, $request->getParams()->get(RedirectPlugin::REDIRECT_COUNT));
47  $this->assertCount(3, $history);
48  $requestHistory = $history->getAll();
49 
50  $this->assertEquals(301, $requestHistory[0]['response']->getStatusCode());
51  $this->assertEquals('/redirect1', (string) $requestHistory[0]['response']->getHeader('Location'));
52  $this->assertEquals(301, $requestHistory[1]['response']->getStatusCode());
53  $this->assertEquals('/redirect2', (string) $requestHistory[1]['response']->getHeader('Location'));
54  $this->assertEquals(200, $requestHistory[2]['response']->getStatusCode());
55  }
56 
58  {
59  // Flush the server and queue up a redirect followed by a successful response
60  $this->getServer()->flush();
61  $this->getServer()->enqueue(array(
62  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n",
63  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n",
64  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect3\r\nContent-Length: 0\r\n\r\n",
65  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect4\r\nContent-Length: 0\r\n\r\n",
66  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect5\r\nContent-Length: 0\r\n\r\n",
67  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect6\r\nContent-Length: 0\r\n\r\n"
68  ));
69 
70  try {
71  $client = new Client($this->getServer()->getUrl());
72  $client->get('/foo')->send();
73  $this->fail('Did not throw expected exception');
74  } catch (TooManyRedirectsException $e) {
75  $this->assertContains(
76  "5 redirects were issued for this request:\nGET /foo HTTP/1.1\r\n",
77  $e->getMessage()
78  );
79  }
80  }
81 
83  {
84  $this->getServer()->flush();
85  $this->getServer()->enqueue(array(
86  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
87  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
88  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
89  ));
90 
91  $client = new Client($this->getServer()->getUrl());
92  $client->post('/foo', array('X-Baz' => 'bar'), 'testing')->send();
93 
94  $requests = $this->getServer()->getReceivedRequests(true);
95  $this->assertEquals('POST', $requests[0]->getMethod());
96  $this->assertEquals('GET', $requests[1]->getMethod());
97  $this->assertEquals('bar', (string) $requests[1]->getHeader('X-Baz'));
98  $this->assertEquals('GET', $requests[2]->getMethod());
99  }
100 
102  {
103  $this->getServer()->flush();
104  $this->getServer()->enqueue(array(
105  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
106  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
107  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
108  ));
109 
110  $client = new Client($this->getServer()->getUrl());
111  $request = $client->post('/foo', array('X-Baz' => 'bar'), 'testing');
112  $request->getParams()->set(RedirectPlugin::STRICT_REDIRECTS, true);
113  $request->send();
114 
115  $requests = $this->getServer()->getReceivedRequests(true);
116  $this->assertEquals('POST', $requests[0]->getMethod());
117  $this->assertEquals('POST', $requests[1]->getMethod());
118  $this->assertEquals('bar', (string) $requests[1]->getHeader('X-Baz'));
119  $this->assertEquals('POST', $requests[2]->getMethod());
120  }
121 
122  public function testRedirect303WithGet()
123  {
124  $this->getServer()->flush();
125  $this->getServer()->enqueue(array(
126  "HTTP/1.1 303 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
127  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
128  ));
129 
130  $client = new Client($this->getServer()->getUrl());
131  $request = $client->post('/foo');
132  $request->send();
133 
134  $requests = $this->getServer()->getReceivedRequests(true);
135  $this->assertEquals('POST', $requests[0]->getMethod());
136  $this->assertEquals('GET', $requests[1]->getMethod());
137  }
138 
140  {
141  $this->getServer()->flush();
142  $this->getServer()->enqueue(array(
143  "HTTP/1.1 303 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
144  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
145  ));
146 
147  $client = new Client($this->getServer()->getUrl());
148  $request = $client->post('/foo');
149  $request->getParams()->set(RedirectPlugin::STRICT_REDIRECTS, true);
150  $request->send();
151 
152  $requests = $this->getServer()->getReceivedRequests(true);
153  $this->assertEquals('POST', $requests[0]->getMethod());
154  $this->assertEquals('GET', $requests[1]->getMethod());
155  }
156 
158  {
159  $this->getServer()->flush();
160  $this->getServer()->enqueue(array(
161  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
162  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
163  ));
164 
165  $client = new Client($this->getServer()->getUrl());
166  $request = $client->put();
167  $request->configureRedirects(true);
168  $body = EntityBody::factory('foo');
169  $body->read(1);
170  $request->setBody($body);
171  $request->send();
172  $requests = $this->getServer()->getReceivedRequests(true);
173  $this->assertEquals('foo', (string) $requests[0]->getBody());
174  }
175 
180  {
181  $this->getServer()->flush();
182  $this->getServer()->enqueue(array(
183  "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi",
184  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n"
185  ));
186 
187  $client = new Client($this->getServer()->getUrl());
188  $request = $client->put();
189  $request->configureRedirects(true);
190  $body = EntityBody::factory(fopen($this->getServer()->getUrl(), 'r'));
191  $body->read(1);
192  $request->setBody($body)->send();
193  }
194 
196  {
197  $this->getServer()->flush();
198  $this->getServer()->enqueue(array("HTTP/1.1 301 Foo\r\nLocation: /foo\r\nContent-Length: 0\r\n\r\n"));
199  $client = new Client($this->getServer()->getUrl());
200  $request = $client->put();
201  $request->configureRedirects(false, 0);
202  $this->assertEquals(301, $request->send()->getStatusCode());
203  }
204 
206  {
207  $this->getServer()->flush();
208  $this->getServer()->enqueue(array(
209  "HTTP/1.1 301 Moved Permanently\r\nLocation: redirect?foo=bar\r\nContent-Length: 0\r\n\r\n",
210  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
211  ));
212  $client = new Client($this->getServer()->getUrl());
213  $request = $client->get('?foo=bar');
214  $request->send();
215  $requests = $this->getServer()->getReceivedRequests(true);
216  $this->assertEquals($this->getServer()->getUrl() . '?foo=bar', $requests[0]->getUrl());
217  $this->assertEquals($this->getServer()->getUrl() . 'redirect?foo=bar', $requests[1]->getUrl());
218  // Ensure that the history on the actual request is correct
219  $this->assertEquals($this->getServer()->getUrl() . '?foo=bar', $request->getUrl());
220  }
221 
223  {
224  // Flush the server and queue up a redirect followed by a successful response
225  $this->getServer()->flush();
226  $this->getServer()->enqueue(array(
227  "HTTP/1.1 301 Moved Permanently\r\nLocation: redirect\r\nContent-Length: 0\r\n\r\n",
228  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
229  ));
230  $client = new Client($this->getServer()->getUrl());
231  $request = $client->get('/foo');
232  $request->send();
233  $requests = $this->getServer()->getReceivedRequests(true);
234  $this->assertEquals('/redirect', $requests[1]->getResource());
235  }
236 
237  public function testResetsHistoryEachSend()
238  {
239  // Flush the server and queue up a redirect followed by a successful response
240  $this->getServer()->flush();
241  $this->getServer()->enqueue(array(
242  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n",
243  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n",
244  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
245  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
246  ));
247 
248  // Create a client that uses the default redirect behavior
249  $client = new Client($this->getServer()->getUrl());
250  $history = new HistoryPlugin();
251  $client->addSubscriber($history);
252 
253  $request = $client->get('/foo');
254  $response = $request->send();
255  $this->assertEquals(3, count($history));
256  $this->assertTrue($request->getParams()->hasKey('redirect.count'));
257  $this->assertContains('/redirect2', $response->getEffectiveUrl());
258 
259  $request->send();
260  $this->assertFalse($request->getParams()->hasKey('redirect.count'));
261  }
262 
264  {
265  // Flush the server and queue up a redirect followed by a successful response
266  $this->getServer()->flush();
267  $this->getServer()->enqueue(array(
268  "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect 1\r\nContent-Length: 0\r\n\r\n",
269  "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
270  ));
271  $client = new Client($this->getServer()->getUrl());
272  $request = $client->get('/foo');
273  $request->send();
274  $reqs = $this->getServer()->getReceivedRequests(true);
275  $this->assertEquals('/redirect%201', $reqs[1]->getResource());
276  }
277 }
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testRedirectWithStrictRfc386Compliance
testRedirectWithStrictRfc386Compliance()
Definition: RedirectPluginTest.php:222
Guzzle\Plugin\History\HistoryPlugin
Definition: HistoryPlugin.php:13
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testCanLimitNumberOfRedirects
testCanLimitNumberOfRedirects()
Definition: RedirectPluginTest.php:57
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testResetsHistoryEachSend
testResetsHistoryEachSend()
Definition: RedirectPluginTest.php:237
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testRedirectsCanBeDisabledPerRequest
testRedirectsCanBeDisabledPerRequest()
Definition: RedirectPluginTest.php:195
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testThrowsExceptionWhenStreamCannotBeRewound
testThrowsExceptionWhenStreamCannotBeRewound()
Definition: RedirectPluginTest.php:179
Guzzle\Http\EntityBody\factory
static factory($resource='', $size=null)
Definition: EntityBody.php:36
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testHandlesRedirectsWithSpacesProperly
testHandlesRedirectsWithSpacesProperly()
Definition: RedirectPluginTest.php:263
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Http\RedirectPlugin\REDIRECT_COUNT
const REDIRECT_COUNT
Definition: RedirectPlugin.php:21
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest
Definition: RedirectPluginTest.php:14
Guzzle\Tests\Plugin\Redirect
Definition: RedirectPluginTest.php:3
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testRedirect303WithGet
testRedirect303WithGet()
Definition: RedirectPluginTest.php:122
Guzzle\Http\RedirectPlugin\STRICT_REDIRECTS
const STRICT_REDIRECTS
Definition: RedirectPlugin.php:23
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testCanRedirectWithNoLeadingSlashAndQuery
testCanRedirectWithNoLeadingSlashAndQuery()
Definition: RedirectPluginTest.php:205
Guzzle\Http\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php:24
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testRewindsStreamWhenRedirectingIfNeeded
testRewindsStreamWhenRedirectingIfNeeded()
Definition: RedirectPluginTest.php:157
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testDefaultBehaviorIsToRedirectWithGetForEntityEnclosingRequests
testDefaultBehaviorIsToRedirectWithGetForEntityEnclosingRequests()
Definition: RedirectPluginTest.php:82
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testRedirect303WithGetWithStrictRfcCompliance
testRedirect303WithGetWithStrictRfcCompliance()
Definition: RedirectPluginTest.php:139
Guzzle\Tests\GuzzleTestCase\getServer
static getServer()
Definition: GuzzleTestCase.php:36
Guzzle\Http\RedirectPlugin
Definition: RedirectPlugin.php:19
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testCanRedirectWithStrictRfcCompliance
testCanRedirectWithStrictRfcCompliance()
Definition: RedirectPluginTest.php:101
Guzzle\Http\Exception\TooManyRedirectsException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Exception/TooManyRedirectsException.php:5
Guzzle\Tests\Plugin\Redirect\RedirectPluginTest\testRedirectsRequests
testRedirectsRequests()
Definition: RedirectPluginTest.php:16