Skip to content

Commit

Permalink
add support for supplying cookies as an object in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 16, 2014
1 parent c83c5d7 commit 5e6aee6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ function Pageres(options) {
}

this.options = assign({}, options);
this.options.cookies = (this.options.cookies || []).map(parseCookiePhantomjs);

this.options.cookies = (this.options.cookies || []).map(function (el) {
return typeof el === 'string' ? parseCookiePhantomjs(el) : el;
});

this.options.name = this.options.name || '<%= url %>-<%= size %><%= crop %>';
this.stats = {};

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ Crop to the set height.

##### cookies

Type: `array`
Type: `array` of `string`, `object`

Same format as a [browser cookie](http://en.wikipedia.org/wiki/HTTP_cookie).
A string with the same format as a [browser cookie](http://en.wikipedia.org/wiki/HTTP_cookie) or an object of what [`phantomjs.addCookie`](http://phantomjs.org/api/phantom/method/add-cookie.html) accepts.

###### Tip

Expand Down
4 changes: 2 additions & 2 deletions test/serverForCookieTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ app.get('/', function(request, response) {
response.send('<body><div style="background: ' + color + ';position: absolute;top: 0;bottom: 0;left: 0;right: 0;"></div></body');
});

module.exports = function() {
module.exports = function (port) {
var server = http.createServer(app);
server.listen(1337);
server.listen(port);
return server;
};
20 changes: 14 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ test('save image', function (t) {
});
});

test('send cookie', function(t) {
function cookieTest (port, input, t) {
t.plan(6);
var server = new Server();
var filename = 'localhost!1337-320x480.png';
var server = new Server(port);
var filename = 'localhost!' + port + '-320x480.png';

var pageres = new Pageres({cookies: ['pageresColor=black; Path=/; Domain=localhost']})
.src('http://localhost:1337', ['320x480'])
var pageres = new Pageres({cookies: [input]})
.src('http://localhost:' + port, ['320x480'])
.dest(__dirname);

pageres.run(function(err) {
Expand All @@ -159,4 +159,12 @@ test('send cookie', function(t) {
t.assert(pixels[3] === 255);
});
});
});
}

test('send cookie', cookieTest.bind(null, 5000, 'pageresColor=black; Path=/; Domain=localhost'));

test('send cookie using an object', cookieTest.bind(null, 5001, {
name: 'pageresColor',
value: 'black',
domain: 'localhost'
}));

0 comments on commit 5e6aee6

Please sign in to comment.