Commit 32f0d2cb 32f0d2cba040895b557ccb67ffb47a0d051c3f0d by Nicolas Perriault

Casper.open() can now perform POST, PUT, HEAD and DELETE HTTP requests

1 parent 375bf0b4
......@@ -32,6 +32,7 @@ var colorizer = require('colorizer');
var events = require('events');
var fs = require('fs');
var mouse = require('mouse');
var qs = require('querystring');
var tester = require('tester');
var utils = require('utils');
var f = utils.format;
......@@ -645,27 +646,58 @@ Casper.prototype.mouseClick = function(selector) {
};
/**
* Opens a page. Takes only one argument, the url to open (using the
* callback argument would defeat the whole purpose of Casper
* actually).
* Performs an HTTP request.
*
* @param String location The url to open
* @param Object settings The request settings
* @return Casper
*/
Casper.prototype.open = function(location, options) {
options = utils.isObject(options) ? options : {};
this.requestUrl = location;
// http auth
var httpAuthMatch = location.match(/^https?:\/\/(.+):(.+)@/i);
if (httpAuthMatch) {
var httpAuth = {
username: httpAuthMatch[1],
password: httpAuthMatch[2]
Casper.prototype.open = function(location, settings) {
var self = this;
// settings validation
if (!settings) {
settings = {
method: "get"
};
this.setHttpAuth(httpAuth.username, httpAuth.password);
}
this.emit('open', location);
this.page.open(this.filter('open.location', location) || location);
if (!utils.isObject(settings)) {
throw new Error("open(): request settings must be an Object");
}
// http method
// taken from https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp#L302
var methods = ["get", "head", "put", "post", "delete"];
if (settings.method && (!utils.isString(settings.method) || methods.indexOf(settings.method) === -1)) {
throw new Error("open(): settings.method must be part of " + methods.join(', '));
}
// http data
if (settings.data) {
if (utils.isObject(settings.data)) { // query object
settings.data = qs.encode(settings.data);
} else if (!utils.isString(settings.data)) {
throw new Error("open(): invalid request settings data value: " + settings.data);
}
}
// current request url
this.requestUrl = this.filter('open.location', location) || location;
// http auth
if (settings.username && settings.password) {
this.setHttpAuth(settings.username, settings.password);
} else {
var httpAuthMatch = location.match(/^https?:\/\/(.+):(.+)@/i);
if (httpAuthMatch) {
var httpAuth = {
username: httpAuthMatch[1],
password: httpAuthMatch[2]
};
this.setHttpAuth(httpAuth.username, httpAuth.password);
}
}
this.emit('open', this.requestUrl, settings);
//this.page.open(this.requestUrl, settings.method, settings.data);
this.page.openUrl(this.requestUrl, {
operation: settings.method,
data: settings.data
}, this.page.settings);
return this;
};
......@@ -741,7 +773,7 @@ Casper.prototype.runStep = function(step) {
var stepInfo = f("Step %d/%d", this.step, this.steps.length);
var stepResult;
if (!skipLog) {
this.log(stepInfo + f('%s (HTTP %d)', this.getCurrentUrl(), this.currentHTTPStatus), "info");
this.log(stepInfo + f(' %s (HTTP %d)', this.getCurrentUrl(), this.currentHTTPStatus), "info");
}
if (utils.isNumber(this.options.stepTimeout) && this.options.stepTimeout > 0) {
var stepTimeoutCheckInterval = setInterval(function(self, start, stepNum) {
......@@ -1218,16 +1250,15 @@ function createPage(casper) {
}
if (casper.options.clientScripts) {
if (!utils.isArray(casper.options.clientScripts)) {
casper.log("The clientScripts option must be an array", "error");
throw new Error("The clientScripts option must be an array");
} else {
for (var i = 0; i < casper.options.clientScripts.length; i++) {
var script = casper.options.clientScripts[i];
casper.options.clientScripts.forEach(function(script) {
if (casper.page.injectJs(script)) {
casper.log(f('Automatically injected %s client side', script), "debug");
} else {
casper.log(f('Failed injecting %s client side', script), "warning");
}
}
});
}
}
// Client-side utils injection
......
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
// Query String Utilities
var QueryString = exports;
//var urlDecode = process.binding('http_parser').urlDecode; // phantomjs incompatible
// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
// See: https://github.com/joyent/node/issues/1707
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function charCode(c) {
return c.charCodeAt(0);
}
// a safe fast alternative to decodeURIComponent
QueryString.unescapeBuffer = function(s, decodeSpaces) {
var out = new Buffer(s.length);
var state = 'CHAR'; // states: CHAR, HEX0, HEX1
var n, m, hexchar;
for (var inIndex = 0, outIndex = 0; inIndex <= s.length; inIndex++) {
var c = s.charCodeAt(inIndex);
switch (state) {
case 'CHAR':
switch (c) {
case charCode('%'):
n = 0;
m = 0;
state = 'HEX0';
break;
case charCode('+'):
if (decodeSpaces) c = charCode(' ');
// pass thru
default:
out[outIndex++] = c;
break;
}
break;
case 'HEX0':
state = 'HEX1';
hexchar = c;
if (charCode('0') <= c && c <= charCode('9')) {
n = c - charCode('0');
} else if (charCode('a') <= c && c <= charCode('f')) {
n = c - charCode('a') + 10;
} else if (charCode('A') <= c && c <= charCode('F')) {
n = c - charCode('A') + 10;
} else {
out[outIndex++] = charCode('%');
out[outIndex++] = c;
state = 'CHAR';
break;
}
break;
case 'HEX1':
state = 'CHAR';
if (charCode('0') <= c && c <= charCode('9')) {
m = c - charCode('0');
} else if (charCode('a') <= c && c <= charCode('f')) {
m = c - charCode('a') + 10;
} else if (charCode('A') <= c && c <= charCode('F')) {
m = c - charCode('A') + 10;
} else {
out[outIndex++] = charCode('%');
out[outIndex++] = hexchar;
out[outIndex++] = c;
break;
}
out[outIndex++] = 16 * n + m;
break;
}
}
// TODO support returning arbitrary buffers.
return out.slice(0, outIndex - 1);
};
QueryString.unescape = function(s, decodeSpaces) {
return QueryString.unescapeBuffer(s, decodeSpaces).toString();
};
QueryString.escape = function(str) {
return encodeURIComponent(str);
};
var stringifyPrimitive = function(v) {
switch (typeof v) {
case 'string':
return v;
case 'boolean':
return v ? 'true' : 'false';
case 'number':
return isFinite(v) ? v : '';
default:
return '';
}
};
QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
obj = (obj === null) ? undefined : obj;
switch (typeof obj) {
case 'object':
return Object.keys(obj).map(function(k) {
if (Array.isArray(obj[k])) {
return obj[k].map(function(v) {
return QueryString.escape(stringifyPrimitive(k)) +
eq +
QueryString.escape(stringifyPrimitive(v));
}).join(sep);
} else {
return QueryString.escape(stringifyPrimitive(k)) +
eq +
QueryString.escape(stringifyPrimitive(obj[k]));
}
}).join(sep);
default:
if (!name) return '';
return QueryString.escape(stringifyPrimitive(name)) + eq +
QueryString.escape(stringifyPrimitive(obj));
}
};
// Parse a key=val string.
QueryString.parse = QueryString.decode = function(qs, sep, eq) {
sep = sep || '&';
eq = eq || '=';
var obj = {};
if (typeof qs !== 'string' || qs.length === 0) {
return obj;
}
qs.split(sep).forEach(function(kvp) {
var x = kvp.split(eq);
var k = QueryString.unescape(x[0], true);
var v = QueryString.unescape(x.slice(1).join(eq), true);
if (!hasOwnProperty(obj, k)) {
obj[k] = v;
} else if (!Array.isArray(obj[k])) {
obj[k] = [obj[k], v];
} else {
obj[k].push(v);
}
});
return obj;
};
......@@ -14,4 +14,4 @@
<li>three</li>
</ul>
</body>
</html>
\ No newline at end of file
</html>
......
......@@ -5,7 +5,7 @@
t.assertEquals(self.fetchText('ul li'), 'onetwothree', 'Casper.fetchText() can retrieve text contents');
});
casper.run(function(self) {
casper.run(function() {
t.done();
});
})(casper.test);
......
......@@ -31,7 +31,7 @@
self.options.onAlert = function(self, message) {
t.assertEquals(message, 'plop', 'Casper.options.onAlert() can intercept an alert message');
};
}).thenOpen('tests/site/alert.html').click('button', function(self) {
}).thenOpen('tests/site/alert.html').thenClick('button', function(self) {
self.options.onAlert = null;
});
......
(function(t) {
var current = 0, tests = [
function(settings) {
t.assertEquals(settings, {
method: "get"
}, "Casper.open() used the expected GET settings");
},
function(settings) {
t.assertEquals(settings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected POST settings");
},
function(settings) {
t.assertEquals(settings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected PUT settings");
},
function(settings) {
t.assertEquals(settings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.open() used the expected HTTP auth settings");
}
];
casper.start();
casper.on('open', function(url, settings) {
tests[current++](settings);
});
// GET
casper.open('tests/site/index.html').then(function() {
t.pass("Casper.open() can open and load a location using GET");
});
// POST
casper.open('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
t.pass("Casper.open() can open and load a location using POST");
});
// PUT
casper.open('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
t.pass("Casper.open() can open and load a location using PUT");
});
// HTTP Auth
casper.open('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}).then(function() {
t.pass("Casper.open() can open and load a location using HTTP auth");
});
casper.run(function() {
this.removeAllListeners('open');
t.done();
});
})(casper.test);
......@@ -28,7 +28,7 @@
self.test.assertEquals(i, item - 1, 'Casper.each() passes a contextualized index');
});
casper.run(function(self) {
casper.run(function() {
t.done();
});
})(casper.test);
......