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

Skip to content

Commit 2d34e78

Browse files
committed
[BrowserKit] fixed method/files/content when redirecting a request
1 parent 64e1655 commit 2d34e78

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ abstract class Client
4141
protected $followRedirects;
4242

4343
private $internalRequest;
44+
private $internalResponse;
4445

4546
/**
4647
* Constructor.
@@ -266,7 +267,7 @@ public function request($method, $uri, array $parameters = array(), array $files
266267
$this->response = $this->doRequest($this->request);
267268
}
268269

269-
$response = $this->filterResponse($this->response);
270+
$this->internalResponse = $response = $this->filterResponse($this->response);
270271

271272
$this->cookieJar->updateFromResponse($response, $uri);
272273

@@ -422,10 +423,22 @@ public function followRedirect()
422423
throw new \LogicException('The request was not redirected.');
423424
}
424425

425-
$server = $this->internalRequest->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();
426439
unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']);
427440

428-
return $this->request('get', $this->redirect, array(), array(), $server);
441+
return $this->request($method, $this->redirect, $request->getParameters(), $files, $server, $content);
429442
}
430443

431444
/**

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)