Commit 5f514cb7 5f514cb715780be03a24d4cae0ab12d8fc8246d5 by Nicolas Perriault

fixes #414 - better utils.cleanUrl()

1 parent beb33963
......@@ -61,23 +61,19 @@ function betterTypeOf(input) {
exports.betterTypeOf = betterTypeOf;
/**
* Cleans a passed URL if it lacks a slash at the end when a sole domain is used.
* Cleans a passed URL.
*
* @param String url An HTTP URL
* @return String
*/
function cleanUrl(url) {
"use strict";
var parts = /(https?):\/\/(.*)/i.exec(url);
if (!parts) {
if (url.indexOf('/') === 0) {
return url;
}
var protocol = parts[1];
var subparts = parts[2].split('/');
if (subparts.length === 1) {
return format("%s://%s/", protocol, subparts[0]);
}
return url;
var a = document.createElement('a');
a.href = url;
return a.href;
}
exports.cleanUrl = cleanUrl;
......
......@@ -24,7 +24,7 @@ casper.test.begin('utils.betterTypeOf() tests', 10, function(test) {
test.done();
});
casper.test.begin('utils.cleanUrl() tests', 10, function(test) {
casper.test.begin('utils.cleanUrl() tests', 11, function(test) {
var testCases = {
'http://google.com/': 'http://google.com/',
'http://google.com': 'http://google.com/',
......@@ -34,6 +34,7 @@ casper.test.begin('utils.cleanUrl() tests', 10, function(test) {
'https://google.com': 'https://google.com/',
'https://www.google.com/': 'https://www.google.com/',
'https://www.google.com/?plop=2': 'https://www.google.com/?plop=2',
'https://www.google.com?plop=2': 'https://www.google.com/?plop=2',
'file:///Users/toto/toto.html': 'file:///Users/toto/toto.html',
'/100': '/100'
};
......