Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc1a539

Browse files
committed
merged branch fabpot/browserkit-redirect-22 (PR #8895)
This PR was merged into the 2.2 branch. Discussion ---------- [BrowserKit] fixed the redirect behavior according to the RFC | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Commits ------- 2d34e78 [BrowserKit] fixed method/files/content when redirecting a request 64e1655 [BrowserKit] removed some headers when redirecting a request 96a4b00 [BrowserKit] fixed headers when redirecting if history is set to false (refs #8697)
2 parents 7e06905 + 2d34e78 commit dc1a539

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ abstract class Client
4040
protected $redirect;
4141
protected $followRedirects;
4242

43+
private $internalRequest;
44+
private $internalResponse;
45+
4346
/**
4447
* Constructor.
4548
*
@@ -250,7 +253,7 @@ public function request($method, $uri, array $parameters = array(), array $files
250253
$server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST);
251254
$server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);
252255

253-
$request = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
256+
$this->internalRequest = $request = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
254257

255258
$this->request = $this->filterRequest($request);
256259

@@ -264,7 +267,7 @@ public function request($method, $uri, array $parameters = array(), array $files
264267
$this->response = $this->doRequest($this->request);
265268
}
266269

267-
$response = $this->filterResponse($this->response);
270+
$this->internalResponse = $response = $this->filterResponse($this->response);
268271

269272
$this->cookieJar->updateFromResponse($response, $uri);
270273

@@ -420,7 +423,22 @@ public function followRedirect()
420423
throw new \LogicException('The request was not redirected.');
421424
}
422425

423-
return $this->request('get', $this->redirect, array(), array(), $this->history->current()->getServer());
426+
$request = $this->internalRequest;
427+
428+
if (in_array($this->internalResponse->getStatus(), array(302, 303))) {
429+
$method = 'get';
430+
$files = array();
431+
$content = null;
432+
} else {
433+
$method = $request->getMethod();
434+
$files = $request->getFiles();
435+
$content = $request->getContent();
436+
}
437+
438+
$server = $request->getServer();
439+
unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']);
440+
441+
return $this->request($method, $this->redirect, $request->getParameters(), $files, $server, $content);
424442
}
425443

426444
/**

src/Symfony/Component/BrowserKit/Tests/ClientTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ public function testFollowRedirect()
326326
$client->request('GET', 'http://www.example.com/foo/foobar');
327327

328328
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');
329+
330+
$client = new TestClient();
331+
$client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected')));
332+
$client->request('POST', 'http://www.example.com/foo/foobar');
333+
334+
$this->assertEquals('get', $client->getRequest()->getMethod(), '->followRedirect() uses a get for 302');
329335
}
330336

331337
public function testFollowRedirectWithCookies()

0 commit comments

Comments
 (0)