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

Added an option for adding key/values to phjs' page.customHeaders #21

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ module.exports = function(grunt) {
}
}
},
headers: {
options: {
url: 'http://localhost:8000',
server: './test/fixtures/headers_server.js',
page: {
customHeaders: {
'X-CUSTOM': 'custom_header_567'
}
},
expected: 'custom_header_567',
test: function test(msg) {
test.actual = msg;
}
}
},
},
});

Expand All @@ -50,20 +65,33 @@ module.exports = function(grunt) {
var options = this.options();
var phantomjs = require('./lib/phantomjs').init(grunt);

var server = null;
if (options.server) { server = require(options.server); }

// Do something.
phantomjs.on('test', options.test);
phantomjs.on('done', phantomjs.halt);

phantomjs.on('done', function() {
phantomjs.halt();
if (options.server) { server = require(options.server); }
});

phantomjs.on('debug', function(msg) {
grunt.log.writeln('debug:' + msg);
});

// Built-in error handlers.
phantomjs.on('fail.load', function(url) {
phantomjs.halt();
if (options.server) { server = require(options.server); }
grunt.verbose.write('Running PhantomJS...').or.write('...');
grunt.log.error();
grunt.warn('PhantomJS unable to load "' + url + '" URI.');
});

phantomjs.on('fail.timeout', function() {
phantomjs.halt();
if (options.server) { server = require(options.server); }
grunt.log.writeln();
grunt.warn('PhantomJS timed out.');
});
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ The best way to understand how this lib should be used is by looking at the [gru

Also, in the case of the grunt-contrib-qunit plugin, it's important to know that the page being loaded into PhantomJS *doesn't* know it will be loaded into PhantomJS, and as such doesn't have any PhantomJS->Grunt code in it. That communication code, aka. the ["bridge"](https://github.com/gruntjs/grunt-contrib-qunit/blob/master/phantomjs/bridge.js), is dynamically [injected into the html page](https://github.com/gruntjs/grunt-contrib-qunit/blob/master/tasks/qunit.js#L136).

### Options

This lib has a few options that can be added to it:
Copy link
Member

Choose a reason for hiding this comment

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

moot sentence

Copy link
Author

Choose a reason for hiding this comment

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

removed


* `timeout`: phantomjs' timeout, in milliseconds.
Copy link
Member

Choose a reason for hiding this comment

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

PhantomJS

Copy link
Author

Choose a reason for hiding this comment

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

fixed

* `inject`: Javascript to inject into the page.
Copy link
Member

Choose a reason for hiding this comment

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

JavaScript

Copy link
Author

Choose a reason for hiding this comment

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

fixed

* `page`: an object of options for the phantomjs `page` object.
Copy link
Member

Choose a reason for hiding this comment

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

PhantomJS

Also page should link to the PhantomJS docs on the page object.

Copy link
Author

Choose a reason for hiding this comment

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

fixed


## An inline example

If a Grunt task looked something like this:
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
"grunt": "~0.4.0",
"difflet": "~0.2.3"
"difflet": "~0.2.3",
"express": "~3.1.1",
"underscore": "~1.4.4"
Copy link
Member

Choose a reason for hiding this comment

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

lodash

Copy link
Author

Choose a reason for hiding this comment

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

fixed

},
"main": "lib/phantomjs"
}
7 changes: 7 additions & 0 deletions phantomjs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ var inject = function() {
injected = true;
};

// Add options to the page.
if (options.page) {
for (var prop in options.page) {
page[prop] = options.page[prop];
}
}

Copy link
Member

Choose a reason for hiding this comment

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

_.extend()

Copy link
Author

Choose a reason for hiding this comment

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

fixed

// Keep track if the client-side helper script already has been injected.
page.onUrlChanged = function(newUrl) {
injected = false;
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/headers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html>
<head>
<script>

// Send messages to the parent PhantomJS process via alert! Good times!!
function sendMessage() {
var args = [].slice.call(arguments);
alert(JSON.stringify(args));
}

// Template the headers out
var headers = {
<% _.each(_.pairs(headers), function(pair) { %> "<%= pair[0] %>":"<%= pair[1] %>", <% }); %>
};

for (var prop in headers) {
sendMessage('debug', prop + ":" + headers[prop]);
}
sendMessage('test', headers['x-custom']);
sendMessage('done');

</script>
</head>
<body>
</body>
</html>
18 changes: 18 additions & 0 deletions test/fixtures/headers_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var _ = require('underscore');
var fs = require('fs');
var express = require('express');
var port = 8000;
var site = express();

site.get('*', function(req, res) {
fs.readFile('./test/fixtures/headers.html', 'utf8', function (err, data) {
if (err) throw err;
var h_tmpl = _.template(data);
res.write(h_tmpl({headers: req.headers}));
res.end();
});
});

site.listen(port);

console.log("Listening on port " + port);
Copy link
Member

Choose a reason for hiding this comment

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

single quotes

Copy link
Author

Choose a reason for hiding this comment

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

fixed