Skip to content

Commit

Permalink
feature #24778 [BrowserKit] add a way to switch to ajax for one reque…
Browse files Browse the repository at this point in the history
…st (Simperfit)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[BrowserKit] add a way to switch to ajax for one request

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20306
| License       | MIT
| Doc PR        | will do

Follow the work on #20306.

/cc @fabpot is it the right implementation ?

Commits
-------

a10eae7 [BrowserKit] add a way to switch to ajax for one request
  • Loading branch information
fabpot committed Feb 19, 2018
2 parents 74cea1d + a10eae7 commit 17d1054
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '')
return isset($this->server[$key]) ? $this->server[$key] : $default;
}

public function switchToXHR()
{
$this->setServerParameter('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
}

public function removeXHR()
{
unset($this->server['HTTP_X_REQUESTED_WITH']);
}

/**
* Returns the History instance.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Expand Up @@ -94,6 +94,16 @@ public function testGetRequest()
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
}

public function testGetRequestWithXHR()
{
$client = new TestClient();
$client->switchToXHR();
$client->request('GET', 'http://example.com/', array(), array(), array(), null, true, true);
$this->assertEquals($client->getRequest()->getServer()['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest');
$client->removeXHR();
$this->assertFalse($client->getServerParameter('HTTP_X_REQUESTED_WITH', false));
}

public function testGetRequestWithIpAsHttpHost()
{
$client = new TestClient();
Expand Down

0 comments on commit 17d1054

Please sign in to comment.