Skip to content

Commit

Permalink
Be strict about options.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas committed Mar 5, 2015
1 parent 9f4027b commit de0dfba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 84 deletions.
11 changes: 8 additions & 3 deletions lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,14 @@ Needle.prototype.setup = function(uri, options) {
return options[aliased.inverted[key]];
}

function check_value(type, key) {
var value = get_option(key);
return (typeof value == type) ? value : defaults[key];
function check_value(expected, key) {
var value = get_option(key),
type = typeof value;

if (type != 'undefined' && type != expected)
throw new TypeError(type + ' received for ' + key + ', but expected a ' + expected);

return (type == expected) ? value : defaults[key];
}

//////////////////////////////////////////////////
Expand Down
13 changes: 0 additions & 13 deletions test/parsing_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@ var should = require('should'),

describe('parsing', function(){

var get_catch = function(url, opts, cb) {
var done = function(e) {
cb(e);
}
try {
needle.get(url, opts, function() {
done();
});
} catch(e) {
done(e);
}
}

describe('when response is an JSON string', function(){

var json_string = '{"foo":"bar"}';
Expand Down
78 changes: 10 additions & 68 deletions test/redirect_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,46 +185,19 @@ describe('redirects', function() {
opts = { follow: value };
})

describe('and redirected to the same path on same host and protocol', function() {
before(function() {
location = url;
})
it('does not follow redirect', not_followed);
})

describe('and redirected to the same path on same host and different protocol', function() {
before(function() {
location = url.replace(protocol, other_protocol).replace(ports[protocol], ports[other_protocol]);
})
it('does not follow redirect', not_followed);
})

describe('and redirected to a different path on same host, same protocol', function() {
before(function() {
location = url.replace('/hello', '/goodbye');
})
it('does not follow redirect', not_followed);
})

describe('and redirected to a different path on same host, different protocol', function() {
describe('and redirected to the same path on same host and protocol', function() {
before(function() {
location = url.replace('/hello', '/goodbye').replace(protocol, other_protocol).replace(ports[protocol], ports[other_protocol]);
location = url;
})
it('does not follow redirect', not_followed);
})

describe('and redirected to same path on another host, same protocol', function() {
before(function() {
location = url.replace('localhost', hostname);
it('throws an error', function() {
(function() {
send_request(opts, function() { });
}).should.throw;
})
it('does not follow redirect', not_followed);
})

describe('and redirected to same path on another host, different protocol', function() {
before(function() {
location = url.replace('localhost', hostname).replace(protocol, other_protocol).replace(ports[protocol], ports[other_protocol]);
})
it('does not follow redirect', not_followed);
})

})
Expand All @@ -239,44 +212,13 @@ describe('redirects', function() {

describe('and redirected to the same path on same host and protocol', function() {
before(function() { location = url })
it('does not follow redirect', not_followed);
})

describe('and redirected to the same path on same host and different protocol', function() {

before(function() {
location = url.replace(protocol, other_protocol).replace(ports[protocol], ports[other_protocol]).replace(ports[protocol], ports[other_protocol]);
it('throws an error', function() {
(function() {
send_request(opts, function() { });
}).should.throw;
})

it('does not follow redirect', not_followed);
})

describe('and redirected to a different path on same host, same protocol', function() {
before(function() {
location = url.replace('/hello', '/goodbye')
})
it('does not follow redirect', not_followed);
})

describe('and redirected to a different path on same host, different protocol', function() {
before(function() {
location = url.replace('/hello', '/goodbye').replace(protocol, other_protocol).replace(ports[protocol], ports[other_protocol]);
})
it('does not follow redirect', not_followed);
})

describe('and redirected to same path on another host, same protocol', function() {
before(function() {
location = url.replace('localhost', hostname);
})
it('does not follow redirect', not_followed);
})

describe('and redirected to same path on another domain, different protocol', function() {
before(function() {
location = url.replace('localhost', hostname).replace(protocol, other_protocol).replace(ports[protocol], ports[other_protocol]);
});
it('does not follow redirect', not_followed);
})

})
Expand Down

0 comments on commit de0dfba

Please sign in to comment.