Comparing version
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
@@ -3,0 +3,0 @@ var api, slumber; |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
@@ -12,12 +12,12 @@ var api, slumber; | ||
}, function() { | ||
var entry, _i, _len, _ref, _results; | ||
_ref = ["api.base_url", "api('note').base_url", "api('note')(42).base_url", "api('note')(42)('comment').base_url", "api('note')(42)('comment')(12345).base_url", "api('note/42/comment/12345').base_url"]; | ||
_results = []; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
entry = _ref[_i]; | ||
_results.push(console.log('-- ', entry, eval(entry))); | ||
var entry, i, len, ref, results; | ||
ref = ["api.base_url", "api('note').base_url", "api('note')(42).base_url", "api('note')(42)('comment').base_url", "api('note')(42)('comment')(12345).base_url", "api('note/42/comment/12345').base_url"]; | ||
results = []; | ||
for (i = 0, len = ref.length; i < len; i++) { | ||
entry = ref[i]; | ||
results.push(console.log('-- ', entry, eval(entry))); | ||
} | ||
return _results; | ||
return results; | ||
}); | ||
}).call(this); |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
@@ -3,0 +3,0 @@ var api, slumber; |
126
lib/API.js
@@ -1,5 +0,5 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
var API, Serializer, append_slash, callable, debug, querystring, request, _ref, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
var API, Serializer, append_slash, callable, debug, hasInsensitive, merge, querystring, ref, request, | ||
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
@@ -10,3 +10,3 @@ querystring = require('querystring'); | ||
_ref = require('./utils'), callable = _ref.callable, append_slash = _ref.append_slash; | ||
ref = require('./utils'), callable = ref.callable, append_slash = ref.append_slash, merge = ref.merge, hasInsensitive = ref.hasInsensitive; | ||
@@ -18,36 +18,37 @@ request = require('request'); | ||
API = callable((function() { | ||
function _Class(base_url, opts, fn) { | ||
var _base, _base1, _base2, _base3, _base4; | ||
this.opts = opts != null ? opts : {}; | ||
function _Class(base_url, opts1, fn) { | ||
var base, base1, base2, base3, base4; | ||
this.opts = opts1 != null ? opts1 : {}; | ||
if (fn == null) { | ||
fn = null; | ||
} | ||
this.patch = __bind(this.patch, this); | ||
this.put = __bind(this.put, this); | ||
this.post = __bind(this.post, this); | ||
this["delete"] = __bind(this["delete"], this); | ||
this.get = __bind(this.get, this); | ||
this.wrap_response = __bind(this.wrap_response, this); | ||
this._prepare_opts = __bind(this._prepare_opts, this); | ||
this._request = __bind(this._request, this); | ||
this._try_to_serialize = __bind(this._try_to_serialize, this); | ||
this._create_child = __bind(this._create_child, this); | ||
this.patch = bind(this.patch, this); | ||
this.put = bind(this.put, this); | ||
this.post = bind(this.post, this); | ||
this["delete"] = bind(this["delete"], this); | ||
this.get = bind(this.get, this); | ||
this.wrap_response = bind(this.wrap_response, this); | ||
this._prepare_opts = bind(this._prepare_opts, this); | ||
this._request = bind(this._request, this); | ||
this._construct_request = bind(this._construct_request, this); | ||
this._try_to_serialize = bind(this._try_to_serialize, this); | ||
this._create_child = bind(this._create_child, this); | ||
if (base_url != null) { | ||
this.opts.base_url = base_url; | ||
} | ||
if ((_base = this.opts).append_slash == null) { | ||
_base.append_slash = true; | ||
if ((base = this.opts).append_slash == null) { | ||
base.append_slash = true; | ||
} | ||
if ((_base1 = this.opts).auth == null) { | ||
_base1.auth = null; | ||
if ((base1 = this.opts).auth == null) { | ||
base1.auth = null; | ||
} | ||
if ((_base2 = this.opts).request_opts == null) { | ||
_base2.request_opts = { | ||
if ((base2 = this.opts).request_opts == null) { | ||
base2.request_opts = { | ||
rejectUnauthorized: false | ||
}; | ||
} | ||
if ((_base3 = this.opts).format == null) { | ||
_base3.format = 'json'; | ||
if ((base3 = this.opts).format == null) { | ||
base3.format = 'json'; | ||
} | ||
this.serializer = (_base4 = this.opts).serializer != null ? _base4.serializer : _base4.serializer = new Serializer(this.opts.format); | ||
this.serializer = (base4 = this.opts).serializer != null ? base4.serializer : base4.serializer = new Serializer(this.opts.format); | ||
if (this.opts.append_slash) { | ||
@@ -91,14 +92,18 @@ this.opts.base_url = append_slash(this.opts.base_url); | ||
_Class.prototype._request = function(method, kwargs, fn) { | ||
var key, req, request_options, value, _ref1; | ||
_Class.prototype._construct_request = function(method, kwargs) { | ||
var base, defaultVersion, key, ref1, request_options, value; | ||
request_options = { | ||
url: this.base_url, | ||
method: method, | ||
headers: { | ||
accept: this.serializer.get_serializer().get_content_type() | ||
} | ||
headers: {} | ||
}; | ||
_ref1 = this.opts.request_opts; | ||
for (key in _ref1) { | ||
value = _ref1[key]; | ||
if (this.opts.headers != null) { | ||
request_options.headers = merge(request_options.headers, this.opts.headers); | ||
} | ||
if (kwargs.headers != null) { | ||
request_options.headers = merge(request_options.headers, kwargs.headers); | ||
} | ||
ref1 = this.opts.request_opts; | ||
for (key in ref1) { | ||
value = ref1[key]; | ||
if (request_options[key] == null) { | ||
@@ -108,2 +113,5 @@ request_options[key] = value; | ||
} | ||
if ((base = request_options.headers).accept == null) { | ||
base.accept = this.serializer.get_serializer().get_content_type(); | ||
} | ||
if (kwargs.args != null) { | ||
@@ -125,2 +133,12 @@ request_options.url += '?' + querystring.stringify(kwargs.args); | ||
} | ||
if (!hasInsensitive(request_options.headers, 'user-agent')) { | ||
defaultVersion = require('../package.json').version; | ||
request_options.headers['User-Agent'] = "node-slumber/" + defaultVersion; | ||
} | ||
return request_options; | ||
}; | ||
_Class.prototype._request = function(method, kwargs, fn) { | ||
var req, request_options; | ||
request_options = this._construct_request(method, kwargs); | ||
debug("" + method, request_options.url); | ||
@@ -155,2 +173,4 @@ return req = request(request_options, fn); | ||
} | ||
} else if (key === 'headers') { | ||
to.headers = value; | ||
} else { | ||
@@ -180,7 +200,13 @@ to[default_dest][key] = value; | ||
} | ||
if (fn == null) { | ||
throw Error('Missing callback'); | ||
} | ||
opts = this._prepare_opts(query, 'args'); | ||
handle = (function(_this) { | ||
return function(err, response, body) { | ||
var _ref1; | ||
if ((200 <= (_ref1 = response.statusCode) && _ref1 <= 299)) { | ||
var ref1; | ||
if (err) { | ||
return _this.wrap_response(fn, err, null, null); | ||
} | ||
if ((200 <= (ref1 = response.statusCode) && ref1 <= 299)) { | ||
return _this.wrap_response(fn, err, response, _this._try_to_serialize(response, body)); | ||
@@ -205,7 +231,10 @@ } else if (response != null ? response.statusCode : void 0) { | ||
} | ||
if (fn == null) { | ||
throw Error('Missing callback'); | ||
} | ||
opts = this._prepare_opts(query, 'args'); | ||
handle = (function(_this) { | ||
return function(err, response, body) { | ||
var _ref1; | ||
if ((200 <= (_ref1 = response.statusCode) && _ref1 <= 299)) { | ||
var ref1; | ||
if ((200 <= (ref1 = response.statusCode) && ref1 <= 299)) { | ||
if (response.statusCode === 204) { | ||
@@ -226,7 +255,10 @@ return _this.wrap_response(fn, err, response, true); | ||
var handle, opts, resp; | ||
if (fn == null) { | ||
throw Error('Missing callback'); | ||
} | ||
opts = this._prepare_opts(data, 'data'); | ||
handle = (function(_this) { | ||
return function(err, response, body) { | ||
var _ref1; | ||
if ((200 <= (_ref1 = response.statusCode) && _ref1 <= 299)) { | ||
var ref1; | ||
if ((200 <= (ref1 = response.statusCode) && ref1 <= 299)) { | ||
return _this.wrap_response(fn, err, response, _this._try_to_serialize(response, body)); | ||
@@ -242,7 +274,10 @@ } | ||
var handle, opts, resp; | ||
if (fn == null) { | ||
throw Error('Missing callback'); | ||
} | ||
opts = this._prepare_opts(data, 'data'); | ||
handle = (function(_this) { | ||
return function(err, response, body) { | ||
var _ref1; | ||
if ((200 <= (_ref1 = response.statusCode) && _ref1 <= 299)) { | ||
var ref1; | ||
if ((200 <= (ref1 = response.statusCode) && ref1 <= 299)) { | ||
return _this.wrap_response(fn, err, response, _this._try_to_serialize(response, body)); | ||
@@ -258,7 +293,10 @@ } | ||
var handle, opts, resp; | ||
if (fn == null) { | ||
throw Error('Missing callback'); | ||
} | ||
opts = this._prepare_opts(data, 'data'); | ||
handle = (function(_this) { | ||
return function(err, response, body) { | ||
var _ref1; | ||
if ((200 <= (_ref1 = response.statusCode) && _ref1 <= 299)) { | ||
var ref1; | ||
if ((200 <= (ref1 = response.statusCode) && ref1 <= 299)) { | ||
return _this.wrap_response(fn, err, response, _this._try_to_serialize(response, body)); | ||
@@ -265,0 +303,0 @@ } |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
@@ -3,0 +3,0 @@ var API; |
@@ -1,7 +0,7 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
var BaseSerializer, JsonSerializer, SERIALIZERS, YamlSerializer, debug, yamljs, _ref, _ref1, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
var BaseSerializer, JsonSerializer, SERIALIZERS, YamlSerializer, debug, yamljs, | ||
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
hasProp = {}.hasOwnProperty; | ||
@@ -16,5 +16,5 @@ debug = require('debug')('slumber:Serializer'); | ||
function BaseSerializer() { | ||
this.dumps = __bind(this.dumps, this); | ||
this.loads = __bind(this.loads, this); | ||
this.get_content_type = __bind(this.get_content_type, this); | ||
this.dumps = bind(this.dumps, this); | ||
this.loads = bind(this.loads, this); | ||
this.get_content_type = bind(this.get_content_type, this); | ||
this.debug = require('debug')("slumber:" + this.constructor.name); | ||
@@ -45,10 +45,9 @@ this.debug('constructor'); | ||
JsonSerializer = (function(_super) { | ||
__extends(JsonSerializer, _super); | ||
JsonSerializer = (function(superClass) { | ||
extend(JsonSerializer, superClass); | ||
function JsonSerializer() { | ||
this.dumps = __bind(this.dumps, this); | ||
this.loads = __bind(this.loads, this); | ||
_ref = JsonSerializer.__super__.constructor.apply(this, arguments); | ||
return _ref; | ||
this.dumps = bind(this.dumps, this); | ||
this.loads = bind(this.loads, this); | ||
return JsonSerializer.__super__.constructor.apply(this, arguments); | ||
} | ||
@@ -76,10 +75,9 @@ | ||
yamljs = require('yamljs'); | ||
YamlSerializer = (function(_super) { | ||
__extends(YamlSerializer, _super); | ||
YamlSerializer = (function(superClass) { | ||
extend(YamlSerializer, superClass); | ||
function YamlSerializer() { | ||
this.dumps = __bind(this.dumps, this); | ||
this.loads = __bind(this.loads, this); | ||
_ref1 = YamlSerializer.__super__.constructor.apply(this, arguments); | ||
return _ref1; | ||
this.dumps = bind(this.dumps, this); | ||
this.loads = bind(this.loads, this); | ||
return YamlSerializer.__super__.constructor.apply(this, arguments); | ||
} | ||
@@ -107,3 +105,3 @@ | ||
function Serializer(_default, serializers) { | ||
var key, obj, serializer, _i, _len; | ||
var i, key, len, obj, serializer; | ||
this["default"] = _default != null ? _default : 'json'; | ||
@@ -113,6 +111,6 @@ if (serializers == null) { | ||
} | ||
this.get_content_type = __bind(this.get_content_type, this); | ||
this.dumps = __bind(this.dumps, this); | ||
this.loads = __bind(this.loads, this); | ||
this.get_serializer = __bind(this.get_serializer, this); | ||
this.get_content_type = bind(this.get_content_type, this); | ||
this.dumps = bind(this.dumps, this); | ||
this.loads = bind(this.loads, this); | ||
this.get_serializer = bind(this.get_serializer, this); | ||
debug('constructor', "@default=" + this["default"], "serializers=" + serializers); | ||
@@ -122,9 +120,9 @@ if (serializers == null) { | ||
(function() { | ||
var _results; | ||
_results = []; | ||
var results; | ||
results = []; | ||
for (key in SERIALIZERS) { | ||
obj = SERIALIZERS[key]; | ||
_results.push(new obj); | ||
results.push(new obj); | ||
} | ||
return _results; | ||
return results; | ||
})() | ||
@@ -137,4 +135,4 @@ ][0]; | ||
this.serializers = {}; | ||
for (_i = 0, _len = serializers.length; _i < _len; _i++) { | ||
serializer = serializers[_i]; | ||
for (i = 0, len = serializers.length; i < len; i++) { | ||
serializer = serializers[i]; | ||
this.serializers[serializer.key] = serializer; | ||
@@ -145,3 +143,3 @@ } | ||
Serializer.prototype.get_serializer = function(name, content_type) { | ||
var ctype, serializer, serializer_name, _i, _len, _ref2, _ref3; | ||
var ctype, i, len, ref, ref1, serializer, serializer_name; | ||
if (name == null) { | ||
@@ -158,3 +156,3 @@ name = null; | ||
if (this.serializers[name] == null) { | ||
throw "" + name + " is not an available serializer"; | ||
throw name + " is not an available serializer"; | ||
} | ||
@@ -164,8 +162,8 @@ return this.serializers[name]; | ||
if (content_type != null) { | ||
_ref2 = this.serializers; | ||
for (serializer_name in _ref2) { | ||
serializer = _ref2[serializer_name]; | ||
_ref3 = serializer.content_types; | ||
for (_i = 0, _len = _ref3.length; _i < _len; _i++) { | ||
ctype = _ref3[_i]; | ||
ref = this.serializers; | ||
for (serializer_name in ref) { | ||
serializer = ref[serializer_name]; | ||
ref1 = serializer.content_types; | ||
for (i = 0, len = ref1.length; i < len; i++) { | ||
ctype = ref1[i]; | ||
if (content_type === ctype) { | ||
@@ -172,0 +170,0 @@ return serializer; |
@@ -1,5 +0,5 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
var debug, | ||
__slice = [].slice; | ||
var debug, extend, | ||
slice = [].slice; | ||
@@ -9,6 +9,6 @@ debug = require('debug')('slumber:utils'); | ||
module.exports.callable = function(ctor) { | ||
var callable_ctor, _ref; | ||
var callable_ctor, ref; | ||
callable_ctor = function() { | ||
var a, obj, result; | ||
a = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
a = 1 <= arguments.length ? slice.call(arguments, 0) : []; | ||
obj = function() { | ||
@@ -18,3 +18,3 @@ return obj.callable.apply(obj, arguments); | ||
obj.__proto__ = ctor.prototype; | ||
result = ctor.call.apply(ctor, [obj].concat(__slice.call(a))); | ||
result = ctor.call.apply(ctor, [obj].concat(slice.call(a))); | ||
if (typeof result === 'object') { | ||
@@ -28,3 +28,3 @@ return result; | ||
callable_ctor.prototype = ctor.prototype; | ||
_ref = Function.prototype, callable_ctor.prototype.call = _ref.call, callable_ctor.prototype.apply = _ref.apply; | ||
ref = Function.prototype, callable_ctor.prototype.call = ref.call, callable_ctor.prototype.apply = ref.apply; | ||
return callable_ctor; | ||
@@ -37,2 +37,30 @@ }; | ||
exports.merge = function(options, overrides) { | ||
return extend(extend({}, options), overrides); | ||
}; | ||
extend = exports.extend = function(object, properties) { | ||
var key, val; | ||
for (key in properties) { | ||
val = properties[key]; | ||
object[key] = val; | ||
} | ||
return object; | ||
}; | ||
exports.hasInsensitive = function(object, key) { | ||
var k, v; | ||
if (!key) { | ||
return false; | ||
} | ||
key = key.toLowerCase(); | ||
for (k in object) { | ||
v = object[k]; | ||
if (key === k.toLowerCase()) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
}).call(this); |
{ | ||
"name": "slumber", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Port of Python's slumber library -- A library that makes consuming a RESTful API easier and more convenient", | ||
@@ -21,5 +21,5 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"request": "2.33", | ||
"debug": "^2.1.3", | ||
"querystring": "0.2.0", | ||
"debug": "0.7" | ||
"request": "^2.55.0" | ||
}, | ||
@@ -26,0 +26,0 @@ "devDependencies": { |
@@ -41,25 +41,53 @@ Slumber (Node.js version) | ||
# Note: Any kwargs passed to get(), post(), put(), delete() will be used as url parameters | ||
api('note').get() | ||
api('note').get (err, data) -> | ||
console.log err, data | ||
# --- | ||
callback = (err, data) -> | ||
console.log err, data | ||
# POST http://slumber.in/api/v1/note/ | ||
new_post = api('note').post({'title': 'My Test Note', 'content': 'This is the content of my Test Note!'}) | ||
new_post = api('note').post({'title': 'My Test Note', 'content': 'This is the content of my Test Note!'}, cb) | ||
# PUT http://slumber.in/api/v1/note/{id}/ | ||
api('note')(new_post['id']).put({'content': 'I just changed the content of my Test Note!'}) | ||
api('note')(new_post['id']).put({'content': 'I just changed the content of my Test Note!'}, cb) | ||
# PATCH http://slumber.in/api/v1/note/{id}/ | ||
api('note')(new_post['id']).patch({'content': 'Wat!'}) | ||
api('note')(new_post['id']).patch({'content': 'Wat!'}, cb) | ||
# GET http://slumber.in/api/v1/note/{id}/ | ||
api('note')(new_post['id']).get() | ||
api('note')(new_post['id']).get(cb) | ||
# DELETE http://slumber.in/api/v1/note/{id}/ | ||
api('note')(new_post['id']).delete() | ||
api('note')(new_post['id']).delete(cb) | ||
api('resource').get {username: "example", api_key: "1639eb74e86717f410c640d2712557aac0e989c8"} | ||
api('resource').get {username: "example", api_key: "1639eb74e86717f410c640d2712557aac0e989c8"}, cb | ||
# GET http://slumber.in/api/v1/note/?title__startswith=Bacon | ||
api('note').get(title__startswith="Bacon") | ||
api('note').get(title__startswith="Bacon", cb) | ||
``` | ||
How to use callbacks | ||
-------------------- | ||
`node-slumber` uses a dynamic callback mechanism, based on the arity of the callback. | ||
Depending on the callback arity, you will have: | ||
- with an arity of 1: `function(err)` | ||
- with an arity of 2: `function(err, processedData)` | ||
- with an arity of 3: `function(err, fullRequestResponse, processedData)` | ||
Debug | ||
----- | ||
`node-slumber` uses the [debug](https://www.npmjs.com/package/debug) package. | ||
To enable debug you can use the environment variable `DEBUG=` as : | ||
- `DEBUG='slumber:api' ...` to see debug for `node-slumber` API calls only | ||
- `DEBUG='slumber:*' ...` to see debug for `node-slumber`, more verbose | ||
- `DEBUG='*' node ...` to see debug for all modules using `debug`, extremely verbose | ||
Development | ||
@@ -66,0 +94,0 @@ ----------- |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
@@ -49,3 +49,3 @@ var CUSTOMERS, app, assert, base_url, connect, express, freeport, querystring, slumber, url; | ||
app.get('/test-auth', function(req, res) { | ||
app.get('/test-headers', function(req, res) { | ||
return res.json({ | ||
@@ -127,3 +127,3 @@ 'headers': req.headers | ||
return it('should retrieve a string with base_url of api with 1 child', function() { | ||
return assert.equal(api('customers').base_url, "" + base_url + "customers/"); | ||
return assert.equal(api('customers').base_url, base_url + "customers/"); | ||
}); | ||
@@ -133,3 +133,3 @@ }); | ||
return it('should retrieve a string with base_url of api with 2 children', function() { | ||
return assert.equal(api('customers')(42).base_url, "" + base_url + "customers/42/"); | ||
return assert.equal(api('customers')(42).base_url, base_url + "customers/42/"); | ||
}); | ||
@@ -166,3 +166,3 @@ }); | ||
return it('should return the best serializer depending on content-type', function() { | ||
var k, mapping, v, _results; | ||
var k, mapping, results, v; | ||
mapping = { | ||
@@ -177,14 +177,14 @@ 'text/yaml': 'yaml', | ||
}; | ||
_results = []; | ||
results = []; | ||
for (k in mapping) { | ||
v = mapping[k]; | ||
if (v === null) { | ||
_results.push(assert.throws((function() { | ||
results.push(assert.throws((function() { | ||
return api.serializer.get_serializer(null, k); | ||
}), /there is no available serializer for content-type/)); | ||
} else { | ||
_results.push(assert.equal(v, api.serializer.get_serializer(null, k).key)); | ||
results.push(assert.equal(v, api.serializer.get_serializer(null, k).key)); | ||
} | ||
} | ||
return _results; | ||
return results; | ||
}); | ||
@@ -304,3 +304,3 @@ }); | ||
}, function() { | ||
return api('test-auth').get(function(err, ret) { | ||
return api('test-headers').get(function(err, ret) { | ||
assert.equal(err, null); | ||
@@ -313,3 +313,3 @@ assert.equal(ret.headers.authorization, 'Basic YWRtaW46c2VjdXJl'); | ||
}); | ||
return describe('Anonymous', function() { | ||
describe('Anonymous', function() { | ||
before(function(done) { | ||
@@ -430,4 +430,157 @@ return freeport(function(err, port) { | ||
}); | ||
return describe('Passing headers', function() { | ||
var headers, headers_override; | ||
headers = { | ||
"X-AAA": 42, | ||
"X-BBB": "test" | ||
}; | ||
headers_override = { | ||
"X-AAA": 43, | ||
"X-CCC": "hello" | ||
}; | ||
it('should pass headers when calling method', function(done) { | ||
return freeport(function(err, port) { | ||
return app.listen(port, function() { | ||
return api = slumber.API("http://localhost:" + port + "/", {}, function() { | ||
return api('test-headers').get({ | ||
headers: headers | ||
}, function(err, ret) { | ||
assert.equal(err, null); | ||
assert.equal(ret.headers['x-aaa'], '42'); | ||
assert.equal(ret.headers['x-bbb'], 'test'); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should pass headers globally', function(done) { | ||
return freeport(function(err, port) { | ||
return app.listen(port, function() { | ||
return api = slumber.API("http://localhost:" + port + "/", { | ||
headers: headers | ||
}, function() { | ||
return api('test-headers').get({}, function(err, ret) { | ||
assert.equal(err, null); | ||
assert.equal(ret.headers['x-aaa'], '42'); | ||
assert.equal(ret.headers['x-bbb'], 'test'); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should pass headers globally and override them when calling method', function(done) { | ||
return freeport(function(err, port) { | ||
return app.listen(port, function() { | ||
return api = slumber.API("http://localhost:" + port + "/", { | ||
headers: headers | ||
}, function() { | ||
return api('test-headers').get({ | ||
headers: headers_override | ||
}, function(err, ret) { | ||
assert.equal(err, null); | ||
assert.equal(ret.headers['x-aaa'], '43'); | ||
assert.equal(ret.headers['x-bbb'], 'test'); | ||
assert.equal(ret.headers['x-ccc'], 'hello'); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should have a default user-agent', function(done) { | ||
return freeport(function(err, port) { | ||
return app.listen(port, function() { | ||
return api = slumber.API("http://localhost:" + port + "/", {}, function() { | ||
return api('test-headers').get({}, function(err, ret) { | ||
var defaultVersion, targetVersion; | ||
assert.equal(err, null); | ||
defaultVersion = require('../package.json').version; | ||
targetVersion = "node-slumber/" + defaultVersion; | ||
assert.equal(ret.headers['user-agent'], targetVersion); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should override user-agent globally', function(done) { | ||
return freeport(function(err, port) { | ||
return app.listen(port, function() { | ||
return api = slumber.API("http://localhost:" + port + "/", { | ||
headers: { | ||
'user-agent': 'test' | ||
} | ||
}, function() { | ||
return api('test-headers').get({}, function(err, ret) { | ||
assert.equal(err, null); | ||
assert.equal(ret.headers['user-agent'], 'test'); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should override user-agent globally using capitalize', function(done) { | ||
return freeport(function(err, port) { | ||
return app.listen(port, function() { | ||
return api = slumber.API("http://localhost:" + port + "/", { | ||
headers: { | ||
'User-Agent': 'test' | ||
} | ||
}, function() { | ||
return api('test-headers').get({}, function(err, ret) { | ||
assert.equal(err, null); | ||
assert.equal(ret.headers['user-agent'], 'test'); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
return it('should override user-agent when calling method', function(done) { | ||
return freeport(function(err, port) { | ||
return app.listen(port, function() { | ||
return api = slumber.API("http://localhost:" + port + "/", {}, function() { | ||
return api('test-headers').get({ | ||
headers: { | ||
'User-Agent': 'test' | ||
} | ||
}, function(err, ret) { | ||
assert.equal(err, null); | ||
assert.equal(ret.headers['user-agent'], 'test'); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Rare cases', function() { | ||
var api; | ||
api = null; | ||
describe('Non existing remote host', function() { | ||
api = slumber.API('http://alskdjgalskdjgalskdjgalskdjgalskdgj.com', {}); | ||
return it('should raise an handled error', function(done) { | ||
return api('lkasdjglaksdjglkasdjglkasdjglkasdg').get(function(err, ret) { | ||
assert.equal(ret, null); | ||
assert.equal(err.code, 'ENOTFOUND'); | ||
assert.equal(err.errno, 'ENOTFOUND'); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
return describe('Call method without callback', function() { | ||
api = slumber.API('http://alskdjgalskdjgalskdjgalskdjgalskdgj.com', {}); | ||
return it('should raise an exception', function() { | ||
return assert.throws((function() { | ||
return api('lkasdjglaksdjglkasdjglkasdjglkasdg').get(); | ||
}), Error); | ||
}); | ||
}); | ||
}); | ||
}).call(this); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
70505
24.68%22
4.76%1123
23.27%111
33.73%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated