Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Simperfit
Copy link
Contributor

@Simperfit Simperfit commented Nov 1, 2017

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 ?

@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch from ebd9e48 to a2b7fe1 Compare November 1, 2017 06:28
@@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '')
return isset($this->server[$key]) ? $this->server[$key] : $default;
}

public function switchToAjax()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to use the term XmlHttpRequest instead of Ajax?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would make more sense

@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch from a2b7fe1 to 5d2f196 Compare November 1, 2017 08:09
@@ -354,7 +374,7 @@ protected function doRequestInProcess($request)
unlink($deprecationsFile);
foreach ($deprecations ? unserialize($deprecations) : array() as $deprecation) {
if ($deprecation[0]) {
trigger_error($deprecation[1], E_USER_DEPRECATED);
@trigger_error($deprecation[1], E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is adding a bug, fabbot is not always right

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will revert

*
* @return Crawler
*/
public function submit(Form $form, array $values = array())
public function submit(Form $form, array $values = array(), $isAjax = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding arguments to a public method is a BC break, see https://symfony.com/bc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood. I have changed the implementation.

@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Nov 1, 2017
@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch 2 times, most recently from 61ae5dc to 512a02e Compare November 1, 2017 17:35
@@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '')
return isset($this->server[$key]) ? $this->server[$key] : $default;
}

public function switchToXMLHttpRequest()
{
$this->setServerParameter('HTTP_X-Requested-With', 'XMLHttpRequest');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be 'HTTP_X_REQUESTED_WITH'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header name is really X-Requested-With, so no I guess.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @dmaicher is right: PHP normalizes dash to low dashes when hydrating the $_SERVER parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, i've tested it and @dmaicher is actually right, sorry I did just check the header name.

@Simperfit
Copy link
Contributor Author

Simperfit commented Nov 5, 2017

fabbot failure seems unrelated

@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch from 512a02e to 317cdf3 Compare December 1, 2017 12:11
@Simperfit
Copy link
Contributor Author

This one is ready for review.

$client->request('GET', 'http://example.com/', array(), array(), array(), null, true, true);
$this->assertEquals($client->getRequest()->getServer()['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest');
$client->removeXMLHttpRequest();
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should make sure the server param is not set anymore instead?

$this->setServerParameter('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
}

public function removeXMLHttpRequest()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this name a bit confusing. We are not removing any request :)

Maybe we can just only have a setter instead? setXMLHttpRequest($isXMLHttpRequest)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add Header a the end I think

@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch from 317cdf3 to 7f79a7f Compare December 9, 2017 16:06
@Simperfit
Copy link
Contributor Author

Named changed and test added.

fabbot error is wrong.

@Simperfit
Copy link
Contributor Author

AppVeyor failure is unrelated and fabbot error is a false positive.

@fabpot WDYT of this new implem ?

@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch from 7f79a7f to 6c4f0b8 Compare February 16, 2018 09:19
@Simperfit
Copy link
Contributor Author

PR rebased with master

$this->assertEquals($client->getRequest()->getServer()['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest');
$client->removeXMLHttpRequestHeader();
$this->assertFalse($client->getServerParameter('HTTP_X_REQUESTED_WITH', false));
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this last assertion looks useless

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right, i've removed it.

@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch from 6c4f0b8 to 1a3bf19 Compare February 16, 2018 12:43
@@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '')
return isset($this->server[$key]) ? $this->server[$key] : $default;
}

public function switchToXMLHttpRequest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about switchToXHR instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch from 1a3bf19 to 02b1e69 Compare February 19, 2018 14:51
@Simperfit Simperfit force-pushed the feature/add-a-way-to-switch-to-ajax-for-one-request branch from 02b1e69 to a10eae7 Compare February 19, 2018 14:52
@fabpot
Copy link
Member

fabpot commented Feb 19, 2018

Thank you @Simperfit.

@fabpot fabpot merged commit a10eae7 into symfony:master Feb 19, 2018
fabpot added a commit that referenced this pull request Feb 19, 2018
…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
@javiereguiluz
Copy link
Member

I know ... it's too late to talk about this because it's merged ... but I wanted to say that I don't like the current implementation :( It modifies a global estate with switchToXHR() and you must remember later to reset the original estate with removeXHR().

As an alternative, I wonder if we could introduce a new xhrRequest() method that wraps the request() method and it works like it, but does the XHR stuff internally just for that request. Thanks!

@Simperfit
Copy link
Contributor Author

@javiereguiluz I agree with this statement, however I think we should let both switchToXHR and removeXHR public, it can be useful to be able to do it by ourself.

@fabpot WDYT ?

@Simperfit Simperfit deleted the feature/add-a-way-to-switch-to-ajax-for-one-request branch March 2, 2018 10:16
@fabpot
Copy link
Member

fabpot commented Mar 2, 2018

I totally agree with @javiereguiluz here. I was too fast to merge. The description of the PR si misleading.

@Simperfit Can you submit a PR to implement what's on the tin?

@Simperfit
Copy link
Contributor Author

Simperfit commented Mar 2, 2018 via email

symfony-splitter pushed a commit to symfony/browser-kit that referenced this pull request Mar 20, 2018
…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 symfony/symfony#24778 (comment) for more information about this.

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

Commits
-------

4ed08896fa feature: transform both switchToXHR and removeXhr to a xhrRequest
fabpot added a commit that referenced this pull request Mar 20, 2018
…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
@fabpot fabpot mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants