Skip to content

Commit

Permalink
feature #26381 Transform both switchToXHR() and removeXhr() to xmlHtt…
Browse files Browse the repository at this point in the history
…pRequest() (Simperfit)

This PR was merged into the 4.1-dev branch.

Discussion
----------

Transform both switchToXHR() and removeXhr() to xmlHttpRequest()

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | none   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | Will do.

See #24778 (comment) for more information about this.

We are switching from a possible global estate change to just only one request affected.

Commits
-------

4ed0889 feature: transform both switchToXHR and removeXhr to a xhrRequest
  • Loading branch information
fabpot committed Mar 20, 2018
2 parents 98d5cf9 + 4ed0889 commit 1fffb85
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -151,14 +151,15 @@ public function getServerParameter($key, $default = '')
return isset($this->server[$key]) ? $this->server[$key] : $default;
}

public function switchToXHR()
public function xmlHttpRequest(string $method, string $uri, array $parameters = array(), array $files = array(), array $server = array(), string $content = null, bool $changeHistory = true): Crawler
{
$this->setServerParameter('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
}

public function removeXHR()
{
unset($this->server['HTTP_X_REQUESTED_WITH']);
try {
return $this->request($method, $uri, $parameters, $files, $server, $content, $changeHistory);
} finally {
unset($this->server['HTTP_X_REQUESTED_WITH']);
}
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Expand Up @@ -104,13 +104,11 @@ public function testGetRequestNull()
$this->assertNull($client->getRequest());
}

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

Expand Down

0 comments on commit 1fffb85

Please sign in to comment.