Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
alternate fix where we had accessor for the BrowerKit request/respons…
…e instances
  • Loading branch information
fabpot committed Apr 24, 2013
1 parent 1005fd1 commit 84ca34b
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 95 deletions.
28 changes: 4 additions & 24 deletions UPGRADE-2.3.md
Expand Up @@ -188,27 +188,7 @@ DomCrawler
BrowserKit
----------

* The `Symfony\Component\BrowserKit\Client::getResponse()` method now
**always** returns a `Symfony\Component\BrowserKit\Response` instance. To
get the response from the code that handled the request, use the new
`getOriginResponse()` method instead of `getResponse()`.

* The `Symfony\Component\BrowserKit\Client::getRequest()` method now
**always** returns a `Symfony\Component\BrowserKit\Request` instance. To
get the response from the code that handled the request, use the new
`getOriginRequest()` method instead of `getRequest()`.

HttpKernel
----------

* The `Symfony\Component\HttpKernel\Client::getResponse()` now returns a
`Symfony\Component\BrowserKit\Response` instance instead of a
`Symfony\Component\HttpFoundation\Response` one (because of a change in
BrowserKit -- see above). You can still get the `HttpFoundation` response
by using `getOriginResponse()` instead of `getResponse()`.

* The `Symfony\Component\HttpKernel\Client::getRequest()` now returns a
`Symfony\Component\BrowserKit\Request` instance instead of a
`Symfony\Component\HttpFoundation\Request` one (because of a change in
BrowserKit -- see above). You can still get the `HttpFoundation` response
by using `getOriginRequest()` instead of `getRequest()`.
* The `Symfony\Component\BrowserKit\Client::getResponse()/getRequest()`
methods now **always** return the request/response instance from the code
that handles the request. To get the BrowserKit request/response instances,
use the new `getInternalResponse()/getInternalRequest()` methods.
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/Client.php
Expand Up @@ -69,7 +69,7 @@ public function getProfile()
return false;
}

return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->originResponse);
return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
}

/**
Expand Down
Expand Up @@ -21,6 +21,6 @@ public function testStateAfterSubRequest()
$client = $this->createClient(array('test_case' => 'Session', 'root_config' => 'config.yml'));
$client->request('GET', 'https://localhost/subrequest/en');

$this->assertEquals('--fr/json--en/html--fr/json--http://localhost/subrequest/fragment/en', $client->getOriginResponse()->getContent());
$this->assertEquals('--fr/json--en/html--fr/json--http://localhost/subrequest/fragment/en', $client->getResponse()->getContent());
}
}
Expand Up @@ -19,6 +19,6 @@ public function testAuthenticationIsCommencingIfAccessDeniedExceptionIsWrapped()
$client->insulate();

$client->request('GET', '/secure-but-not-covered-by-access-control');
$this->assertRedirect($client->getOriginResponse(), '/login');
$this->assertRedirect($client->getResponse(), '/login');
}
}
Expand Up @@ -29,7 +29,7 @@ public function testFormLoginAndLogoutWithCsrfTokens($config)
$form['user_login[password]'] = 'test';
$client->submit($form);

$this->assertRedirect($client->getOriginResponse(), '/profile');
$this->assertRedirect($client->getResponse(), '/profile');

$crawler = $client->followRedirect();

Expand All @@ -44,7 +44,7 @@ public function testFormLoginAndLogoutWithCsrfTokens($config)

$client->click($logoutLinks[0]);

$this->assertRedirect($client->getOriginResponse(), '/');
$this->assertRedirect($client->getResponse(), '/');
}

/**
Expand All @@ -59,7 +59,7 @@ public function testFormLoginWithInvalidCsrfToken($config)
$form['user_login[_token]'] = '';
$client->submit($form);

$this->assertRedirect($client->getOriginResponse(), '/login');
$this->assertRedirect($client->getResponse(), '/login');

$text = $client->followRedirect()->text();
$this->assertContains('Invalid CSRF token.', $text);
Expand All @@ -79,7 +79,7 @@ public function testFormLoginWithCustomTargetPath($config)
$form['user_login[_target_path]'] = '/foo';
$client->submit($form);

$this->assertRedirect($client->getOriginResponse(), '/foo');
$this->assertRedirect($client->getResponse(), '/foo');

$text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text);
Expand All @@ -95,13 +95,13 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
$client->insulate();

$client->request('GET', '/protected-resource');
$this->assertRedirect($client->getOriginResponse(), '/login');
$this->assertRedirect($client->getResponse(), '/login');

$form = $client->followRedirect()->selectButton('login')->form();
$form['user_login[username]'] = 'johannes';
$form['user_login[password]'] = 'test';
$client->submit($form);
$this->assertRedirect($client->getOriginResponse(), '/protected-resource');
$this->assertRedirect($client->getResponse(), '/protected-resource');

$text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text);
Expand Down
Expand Up @@ -29,7 +29,7 @@ public function testFormLogin($config)
$form['_password'] = 'test';
$client->submit($form);

$this->assertRedirect($client->getOriginResponse(), '/profile');
$this->assertRedirect($client->getResponse(), '/profile');

$text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text);
Expand All @@ -50,7 +50,7 @@ public function testFormLoginWithCustomTargetPath($config)
$form['_target_path'] = '/foo';
$client->submit($form);

$this->assertRedirect($client->getOriginResponse(), '/foo');
$this->assertRedirect($client->getResponse(), '/foo');

$text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text);
Expand All @@ -66,13 +66,13 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
$client->insulate();

$client->request('GET', '/protected_resource');
$this->assertRedirect($client->getOriginResponse(), '/login');
$this->assertRedirect($client->getResponse(), '/login');

$form = $client->followRedirect()->selectButton('login')->form();
$form['_username'] = 'johannes';
$form['_password'] = 'test';
$client->submit($form);
$this->assertRedirect($client->getOriginResponse(), '/protected_resource');
$this->assertRedirect($client->getResponse(), '/protected_resource');

$text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text);
Expand Down
Expand Up @@ -27,11 +27,11 @@ public function testLoginLogoutProcedure($locale)
$form['_password'] = 'test';
$client->submit($form);

$this->assertRedirect($client->getOriginResponse(), '/'.$locale.'/profile');
$this->assertRedirect($client->getResponse(), '/'.$locale.'/profile');
$this->assertEquals('Profile', $client->followRedirect()->text());

$client->request('GET', '/'.$locale.'/logout');
$this->assertRedirect($client->getOriginResponse(), '/'.$locale.'/');
$this->assertRedirect($client->getResponse(), '/'.$locale.'/');
$this->assertEquals('Homepage', $client->followRedirect()->text());
}

Expand All @@ -49,7 +49,7 @@ public function testLoginFailureWithLocalizedFailurePath($locale)
$form['_password'] = 'foobar';
$client->submit($form);

$this->assertRedirect($client->getOriginResponse(), '/'.$locale.'/login');
$this->assertRedirect($client->getResponse(), '/'.$locale.'/login');
}

/**
Expand All @@ -61,7 +61,7 @@ public function testAccessRestrictedResource($locale)
$client->insulate();

$client->request('GET', '/'.$locale.'/secure/');
$this->assertRedirect($client->getOriginResponse(), '/'.$locale.'/login');
$this->assertRedirect($client->getResponse(), '/'.$locale.'/login');
}

/**
Expand All @@ -73,7 +73,7 @@ public function testAccessRestrictedResourceWithForward($locale)
$client->insulate();

$crawler = $client->request('GET', '/'.$locale.'/secure/');
$this->assertCount(1, $crawler->selectButton('login'), (string) $client->getOriginResponse());
$this->assertCount(1, $crawler->selectButton('login'), (string) $client->getResponse());
}

public function getLocales()
Expand Down
Expand Up @@ -22,7 +22,7 @@ public function testRoutingErrorIsNotExposedForProtectedResourceWhenAnonymous($c
$client->insulate();
$client->request('GET', '/protected_resource');

$this->assertRedirect($client->getOriginResponse(), '/login');
$this->assertRedirect($client->getResponse(), '/login');
}

/**
Expand All @@ -38,7 +38,7 @@ public function testRoutingErrorIsExposedWhenNotProtected($config)
$client->insulate();
$client->request('GET', '/unprotected_resource');

$this->assertEquals(404, $client->getOriginResponse()->getStatusCode(), (string) $client->getOriginResponse());
$this->assertEquals(404, $client->getResponse()->getStatusCode(), (string) $client->getResponse());
}

/**
Expand All @@ -60,7 +60,7 @@ public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWith

$client->request('GET', '/highly_protected_resource');

$this->assertNotEquals(404, $client->getOriginResponse()->getStatusCode());
$this->assertNotEquals(404, $client->getResponse()->getStatusCode());
}

/**
Expand Down Expand Up @@ -93,12 +93,12 @@ public function testSecurityConfigurationForMultipleIPAddresses($config)

private function assertAllowed($client, $path) {
$client->request('GET', $path);
$this->assertEquals(404, $client->getOriginResponse()->getStatusCode());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}

private function assertRestricted($client, $path) {
$client->request('GET', $path);
$this->assertEquals(302, $client->getOriginResponse()->getStatusCode());
$this->assertEquals(302, $client->getResponse()->getStatusCode());
}

public function getConfigs()
Expand Down
Expand Up @@ -25,7 +25,7 @@ public function testSwitchUser($originalUser, $targetUser, $expectedUser, $expec

$client->request('GET', '/profile?_switch_user='.$targetUser);

$this->assertEquals($expectedStatus, $client->getOriginResponse()->getStatusCode());
$this->assertEquals($expectedStatus, $client->getResponse()->getStatusCode());
$this->assertEquals($expectedUser, $client->getProfile()->getCollector('security')->getUser());
}

Expand All @@ -36,7 +36,7 @@ public function testSwitchedUserCannotSwitchToOther()
$client->request('GET', '/profile?_switch_user=user_cannot_switch_1');
$client->request('GET', '/profile?_switch_user=user_cannot_switch_2');

$this->assertEquals(500, $client->getOriginResponse()->getStatusCode());
$this->assertEquals(500, $client->getResponse()->getStatusCode());
$this->assertEquals('user_cannot_switch_1', $client->getProfile()->getCollector('security')->getUser());
}

Expand All @@ -47,7 +47,7 @@ public function testSwitchedUserExit()
$client->request('GET', '/profile?_switch_user=user_cannot_switch_1');
$client->request('GET', '/profile?_switch_user=_exit');

$this->assertEquals(200, $client->getOriginResponse()->getStatusCode());
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertEquals('user_can_switch', $client->getProfile()->getCollector('security')->getUser());
}

Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Expand Up @@ -4,12 +4,12 @@ CHANGELOG
2.3.0
-----

* added `Client::getOriginRequest()` and `Client::getOriginResponse()` to
have access to the origin request and response objects
* added `Client::getInternalRequest()` and `Client::getInternalResponse()` to
have access to the BrowserKit internal request and response objects
* [BC BREAK] The `Symfony\Component\HttpKernel\Client::getRequest()` method now
returns a `Symfony\Component\BrowserKit\Request` instance
returns the request instance created by the client
* [BC BREAK] The `Symfony\Component\HttpKernel\Client::request()` method now
always returns a `Symfony\Component\BrowserKit\Response` instance
always returns the response instance created by the client

2.1.0
-----
Expand Down
42 changes: 23 additions & 19 deletions src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -33,9 +33,9 @@ abstract class Client
protected $history;
protected $cookieJar;
protected $server;
protected $originRequest;
protected $internalRequest;
protected $request;
protected $originResponse;
protected $internalResponse;
protected $response;
protected $crawler;
protected $insulated;
Expand Down Expand Up @@ -174,9 +174,9 @@ public function getCrawler()
*
* @api
*/
public function getResponse()
public function getInternalResponse()
{
return $this->response;
return $this->internalResponse;
}

/**
Expand All @@ -186,10 +186,12 @@ public function getResponse()
* by the code that handles requests.
*
* @return object A response instance
*
* @api
*/
public function getOriginResponse()
public function getResponse()
{
return $this->originResponse;
return $this->response;
}

/**
Expand All @@ -199,9 +201,9 @@ public function getOriginResponse()
*
* @api
*/
public function getRequest()
public function getInternalRequest()
{
return $this->request;
return $this->internalRequest;
}

/**
Expand All @@ -210,11 +212,13 @@ public function getRequest()
* The origin request is the request instance that is sent
* to the code that handles requests.
*
* @api
*
* @return object A Request instance
*/
public function getOriginRequest()
public function getRequest()
{
return $this->originRequest;
return $this->request;
}

/**
Expand Down Expand Up @@ -278,31 +282,31 @@ public function request($method, $uri, array $parameters = array(), array $files
$server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST);
$server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);

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

$this->originRequest = $this->filterRequest($this->request);
$this->request = $this->filterRequest($this->internalRequest);

if (true === $changeHistory) {
$this->history->add($this->request);
$this->history->add($this->internalRequest);
}

if ($this->insulated) {
$this->originResponse = $this->doRequestInProcess($this->originRequest);
$this->response = $this->doRequestInProcess($this->request);
} else {
$this->originResponse = $this->doRequest($this->originRequest);
$this->response = $this->doRequest($this->request);
}

$this->response = $this->filterResponse($this->originResponse);
$this->internalResponse = $this->filterResponse($this->response);

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

$this->redirect = $this->response->getHeader('Location');
$this->redirect = $this->internalResponse->getHeader('Location');

if ($this->followRedirects && $this->redirect) {
return $this->crawler = $this->followRedirect();
}

return $this->crawler = $this->createCrawlerFromContent($this->request->getUri(), $this->response->getContent(), $this->response->getHeader('Content-Type'));
return $this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
}

/**
Expand Down

0 comments on commit 84ca34b

Please sign in to comment.