Skip to content

Commit

Permalink
general unshortener that just follows links around
Browse files Browse the repository at this point in the history
  • Loading branch information
Swizec committed May 16, 2011
1 parent dd94a40 commit d0b0b81
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 7 additions & 5 deletions lib/unshortener.js
Expand Up @@ -7,19 +7,21 @@ exports.expand = function (url, callback) {

url = (typeof(url) === 'object') ? url : urllib.parse(url);

console.log(url);

var req;
var options = {host: url.host,
port: 80,
path: url.pathname,
method: 'HEAD'};

var handle = function (res) {
console.log(res);
if (res.statusCode === 301 || res.statusCode === 302) {
exports.expand(res.headers.location, callback);
}else{
callback(url);
}
};

req = (url.protocol === 'http') ? http.request(options, handle)
: https.request(options, handle);
req = (url.protocol === 'http:') ? http.request(options, handle)
: https.request(options, handle);
req.end();
};
4 changes: 0 additions & 4 deletions test/unshortener.test.js
Expand Up @@ -6,13 +6,9 @@ var urllib = require('url');

module.exports = {
'expand_string': function () {
var fired = false;
unshortener.expand('http://bit.ly/mShvIR', function (url) {
assert.strictEqual(url.href, 'http://www.premiumpixels.com/freebies/add-to-cart-buttons-psd/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+premiumpixels+%28Premium+Pixels%29');
fired = true;
});

assert.ok(fired);
},

'expand_object': function () {
Expand Down

0 comments on commit d0b0b81

Please sign in to comment.