Skip to content
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

Issue #228: Allowed the setting of custom headers #229

Merged
merged 1 commit into from
Jun 23, 2013
Merged

Issue #228: Allowed the setting of custom headers #229

merged 1 commit into from
Jun 23, 2013

Conversation

MorrisonCole
Copy link
Contributor

This pull-request allows you to pass custom request headers down to PhantomJS. The syntax for doing so is a little different to the PhantomJS equivalent, but is relatively simple:

In PhantomJS, you might do something like:

var page = require('webpage').create();
page.customHeaders = {
  'Accept-Language': 'it-IT',
  'Max-Forwards': '6'
};

And (with this patch), you can do:

capabilities.setCapability(PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "it-IT");
capabilities.setCapability(PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Max-Forwards", "6");

Similar resolved issues: Issue #134

Technical notes:
After the iterations on the implementation, we've ended up with a bit of duplication. My Javascript skills are sufficiently bad that I'm not going to refactor it without the tools to test afterward (latest commit is from a dying laptop with no charger), so I'll leave that in for now. To be fair, it reads fine anyway.

Also, it'd be nice to update the page.customHeaders object so that we don't lose any previous key/values that may have existed. Not really a problem, as it doesn't look like we have any default headers at the moment anyway, but it might need to be changed in the future.

* Separate headers with a comma (e.g. "Accept-Language:it-IT,Max-Forwards:6").
* See <a href="https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#wiki-webpage-customHeaders">PhantomJS docs/a>.
*/
public static final String PHANTOMJS_CUSTOMHEADERS_PREFIX = "phantomjs.customHeaders";
Copy link
Owner

Choose a reason for hiding this comment

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

I think the actual string is wrong here.
Also, I'd call it PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX.

@detro
Copy link
Owner

detro commented Jun 20, 2013

Could you please add a bit more explaination to the PR?
I do get what you want to do reading your code, but a verbal explaination would make sure it does what it says on the tin.

BTW, thank you very much for this effort.
I'm sure this is going to be merged once those small issues are addressed ;)

@MorrisonCole
Copy link
Contributor Author

@detro I've updated the pull-request with an example usage and (basic) explanation.

Also note that I changed:

PHANTOMJS_CUSTOMHEADERS_PREFIX = "phantomjs.customHeaders"

to:

PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX = "phantomjs.page.customHeaders"

(per your suggestion).

@detro
Copy link
Owner

detro commented Jun 21, 2013

I'm stupid.
I missed the fact that the way this works is by setting 1 Capability and a list of HEADER:VALUE,HEADER:VALUE... headers.

This doesn't match the way "phantomjs.page.settings.x" works: that requires you to set 1 Capability PER setting.
So something like:

setCapability("phantomjs.page.settings.userAgent", "My user agent");
setCapability("phantomjs.page.settings.AllowXSS", true);

and so forth.

That's why I called it PHANTOMJS_PAGE_SETTINGS_PREFIX: because that constant only provides a PREFIX and the developer has to use that and attach another string to it with the name of the setting they want to set.

I'd like to maintain consistency: could you please refactor?

Btw, the changes you have done are all going in the right direction.
Sorry if I seem a bit "anal" :)

@MorrisonCole
Copy link
Contributor Author

I'd argue that it'd be more consistent implementing it in a similar way to PhantomJS.

If in Phantom we're doing

page.customHeaders = { 'Accept-Language': 'it-IT' };

Shouldn't GhostDriver be doing:

capabilities.setCapability("phantomjs.page.customHeaders", "Accept-Language:it-IT");

Of course if you still don't want to do that, I'm happy to change it (although I don't think it makes much sense).

@detro
Copy link
Owner

detro commented Jun 21, 2013

Leaving the way you suggest will make 2 capabilities, that do 2 very (very!) similar things, require a totally different approach.

And I can't change phantomjs.page.settings now because it's already released and I don't think it's worth to pay the price of breaking compatibility.

It's a tool, and people don't like when you break their tools :)

@MorrisonCole
Copy link
Contributor Author

Point taken. I personally preferred sticking as closely as possible to the PhantomJS implementation, but it does make it a little messy (comma delimited argument passing, eurgh).

So before I go ahead and make the changes, you want something like:

PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX = "phantomjs.page.customHeaders.";
capabilities.setCapability(PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "it-IT");

Right? :)

Edit: Facepalm, corrected to PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX, was tired!

@detro
Copy link
Owner

detro commented Jun 21, 2013

PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX = "phantomjs.page.customHeaders."; //< notice the dot at the end
capabilities.setCapability(PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "it-IT");

Issue #228: Allowed the setting of custom headers + minor documentation

Issue #228: Corrected the Java customHeaders prefix

Allowed setting of page custom headers via capabilities, similar to page settings
@MorrisonCole
Copy link
Contributor Author

Done and done. Updated the pull-request description to reflect the changes.

I agree that it's nicer as you suggested. Cool!

detro added a commit that referenced this pull request Jun 23, 2013
Issue #228: Allowed the setting of custom headers
@detro detro merged commit 43a8fbc into detro:master Jun 23, 2013
@detro
Copy link
Owner

detro commented Jun 23, 2013

Merged.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants