Commit f7b55e5d by 3rd-Eden

[fix] Throw if we don't get an API key

[fix] Response headers are lowercased [fix] Proper setting of pre-loaded values
parent 7249c43d
...@@ -24,6 +24,8 @@ function FullContact(api) { ...@@ -24,6 +24,8 @@ function FullContact(api) {
this.queueing = false; // Should we be queueing requests this.queueing = false; // Should we be queueing requests
this.requests = []; // Stores all queued commands this.requests = []; // Stores all queued commands
if (!this.key) throw new Error('Missing API key');
} }
/** /**
...@@ -70,9 +72,9 @@ FullContact.prototype.request = function req(packet, args) { ...@@ -70,9 +72,9 @@ FullContact.prototype.request = function req(packet, args) {
, self = this; , self = this;
request(packet, function requested(err, res, body) { request(packet, function requested(err, res, body) {
self.ratereset = +res.headers['X-Rate-Limit-Reset'] || self.ratereste; self.ratereset = +res.headers['x-rate-limit-reset'] || self.ratereste;
self.ratelimit = +res.headers['X-Rate-Limit-Limit'] || self.ratelimit; self.ratelimit = +res.headers['x-rate-limit-limit'] || self.ratelimit;
self.remaining = +res.headers['X-Rate-Limit-Remaining'] || self.remaining; self.remaining = +res.headers['x-rate-limit-remaining'] || self.remaining;
if (err) return fn(err); if (err) return fn(err);
...@@ -212,12 +214,14 @@ FullContact.define = function define(where, name, fn) { ...@@ -212,12 +214,14 @@ FullContact.define = function define(where, name, fn) {
Object.defineProperty(where, name, { Object.defineProperty(where, name, {
configurable: true, configurable: true,
get: function get() { get: function get() {
return where[name] = fn.call(this); return Object.defineProperty(this, name, {
value: fn.call(this)
})[name];
}, },
set: function set(value) { set: function set(value) {
Object.defineProperty(where, name, { return Object.defineProperty(this, name, {
value: value value: value
}); })[name];
} }
}); });
}; };
......
...@@ -5,6 +5,8 @@ describe('FullContact', function () { ...@@ -5,6 +5,8 @@ describe('FullContact', function () {
, chai = require('chai') , chai = require('chai')
, expect = chai.expect; , expect = chai.expect;
chai.Assertion.includeStack = true;
// //
// The API key we use for testing. // The API key we use for testing.
// //
...@@ -15,10 +17,15 @@ describe('FullContact', function () { ...@@ -15,10 +17,15 @@ describe('FullContact', function () {
// //
this.timeout(20000); this.timeout(20000);
//
// Pre-create an API instance
//
var api = new FullContact(key);
it('exposes the createClient api which initializes the constructor', function () { it('exposes the createClient api which initializes the constructor', function () {
var api = FullContact.createClient(key); var client = FullContact.createClient(key);
expect(api).to.be.instanceOf(FullContact); expect(client).to.be.instanceOf(FullContact);
}); });
it('exposes the Person constructor', function () { it('exposes the Person constructor', function () {
...@@ -37,27 +44,54 @@ describe('FullContact', function () { ...@@ -37,27 +44,54 @@ describe('FullContact', function () {
expect(FullContact.Name).to.be.a('function'); expect(FullContact.Name).to.be.a('function');
}); });
it('sets the x-rate properties on request', function (done) { it('throws an error when the API is constructed without a key', function () {
var fc = new FullContact(key); try { new FullContact(); }
catch(e) {
return expect(e.message).to.equal('Missing API key');
}
throw new Error('I should have failed');
});
it('errors when an invalid API key is given', function (done) {
var client = new FullContact(key +'adfasfdsfadsfas');
client.person.email('arnout@observe.it', function (err) {
expect(err).to.be.instanceOf(Error);
expect(err.message.toLowerCase()).to.include('api');
expect(err.message.toLowerCase()).to.include('key');
done();
});
});
it('sets the x-rate properties on request', function (done) {
['remaining', 'ratelimit', 'ratereset'].forEach(function (prop) { ['remaining', 'ratelimit', 'ratereset'].forEach(function (prop) {
expect(fc[prop]).to.equal(0); expect(api[prop]).to.equal(0);
}); });
fc.person.email('arnout@observe.it', function normalize(err, data) { api.person.email('arnout@observe.it', function email(err, data) {
if (err) return done(err); if (err) return done(err);
['remaining', 'ratelimit', 'ratereset'].forEach(function (prop) { ['remaining', 'ratelimit', 'ratereset'].forEach(function (prop) {
expect(fc[prop]).to.not.equal(0); expect(api[prop]).to.not.equal(0);
expect(fc[prop]).to.be.a('number'); expect(api[prop]).to.be.a('number');
}); });
done(); done();
}); });
}); });
it('decreases the rate remaining on request'); it('decreases the rate remaining on request', function (done) {
it('errors when an invalid API key is given'); var remaining = api.remaining;
api.person.email('arnout@observe.it', function email(err) {
if (err) return done(err);
expect(api.remaining).to.be.below(remaining);
done();
});
});
it('does batch requests'); it('does batch requests');
}); });
...@@ -5,6 +5,8 @@ describe('FullContact.Person', function () { ...@@ -5,6 +5,8 @@ describe('FullContact.Person', function () {
, chai = require('chai') , chai = require('chai')
, expect = chai.expect; , expect = chai.expect;
chai.Assertion.includeStack = true;
// //
// The API key we use for testing. // The API key we use for testing.
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment