From d1c1cf9a3f90d3a089af46be4bdb1ee10b546276 Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Fri, 12 Jul 2013 15:03:40 -0700 Subject: [PATCH] Added better error handling for akismet's responses --- lib/akismet.js | 16 +++- package.json | 2 +- test/akismet.spec.js | 192 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 171 insertions(+), 39 deletions(-) diff --git a/lib/akismet.js b/lib/akismet.js index b7cebd2..827a2e3 100644 --- a/lib/akismet.js +++ b/lib/akismet.js @@ -20,7 +20,7 @@ var Akismet = function(options) { this.host = options.host || 'rest.akismet.com'; this.version = options.version || '1.1'; this.endpoint = options.endpoint || this.key + '.' + this.host + '/' + this.version + '/'; - this.userAgent = options.userAgent || 'Node.js/' + process.version + ' | Akismet-api/1.0.1'; + this.userAgent = options.userAgent || 'Node.js/' + process.version + ' | Akismet-api/1.1.0'; /* * Verify that the provided key is accepted by Akismet @@ -38,7 +38,12 @@ var Akismet = function(options) { .end(function(err, res) { if (err) cb(err, null); else if (res.text == 'valid') cb(null, true); - else cb(new Error(res.header['x-akismet-debug-help']), false); + else if (res.header['x-akismet-debug-help']) + cb(new Error(res.header['x-akismet-debug-help']), false); + else if (res.text == 'invalid') + cb(new Error('Invalid API key'), false); + else + cb(new Error(res.text), false); }); }; @@ -70,7 +75,12 @@ var Akismet = function(options) { if (err) cb(err, null); else if (res.text == 'true') cb(null, true); else if (res.text == 'false') cb(null, false); - else cb(new Error(res.header['x-akismet-debug-help']), null); + else if (res.header['x-akismet-debug-help']) + cb(new Error(res.header['x-akismet-debug-help']), null); + else if (res.text == 'invalid') + cb(new Error('Invalid API key'), null); + else + cb(new Error(res.text), null); }); }; diff --git a/package.json b/package.json index 980e137..cb6e22d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "akismet-api", - "version": "1.0.1", + "version": "1.1.0", "description": "Nodejs bindings to the Akismet (http://akismet.com) spam detection service", "main": "lib/akismet.js", "dependencies": { diff --git a/test/akismet.spec.js b/test/akismet.spec.js index d92d395..162094b 100644 --- a/test/akismet.spec.js +++ b/test/akismet.spec.js @@ -46,7 +46,7 @@ describe('Akismet-api', function() { expect(client.host).to.equal('rest.akismet.com'); expect(client.endpoint).to.equal('undefined.rest.akismet.com/1.1/'); expect(client.port).to.equal(80); - expect(client.userAgent).to.equal('Node.js/' + process.version + ' | Akismet-api/1.0.1'); + expect(client.userAgent).to.equal('Node.js/' + process.version + ' | Akismet-api/1.1.0'); expect(client.version).to.equal('1.1'); }); @@ -93,6 +93,85 @@ describe('Akismet-api', function() { describe('when the request returns \'invalid\'', function() { + describe('when the x-akismet-debug-help header is present', function() { + + var client; + var scope; + + beforeEach(function() { + client = Akismet.client({ + blog : 'http://example.com', + key : 'testKey2', + host : 'rest2.akismet.com' + }); + scope = nock('http://rest2.akismet.com') + .matchHeader('Content-Type', 'application/x-www-form-urlencoded') + .post('/1.1/verify-key') + .reply(200, 'invalid', { + 'Content-Type' : 'text/plain', + 'X-akismet-debug-help' : 'Could not find your key' + }); + }); + + it('should return false', function(done) { + client.verifyKey(function(err, valid) { + expect(valid).to.be.false; + scope.done(); + done(); + }); + }); + + it('should return the akismet debug error', function(done) { + client.verifyKey(function(err, valid) { + expect(err.message).to.equal('Could not find your key'); + scope.done(); + done(); + }); + }); + + }); + + describe('when the x-akismet-debug-help header is not present', function() { + + var client; + var scope; + + beforeEach(function() { + client = Akismet.client({ + blog : 'http://example.com', + key : 'testKey2', + host : 'rest2.akismet.com' + }); + scope = nock('http://rest2.akismet.com') + .matchHeader('Content-Type', 'application/x-www-form-urlencoded') + .post('/1.1/verify-key') + .reply(200, 'invalid', { + 'Content-Type' : 'text/plain' + }); + }); + + it('should return false', function(done) { + client.verifyKey(function(err, valid) { + expect(valid).to.be.false; + scope.done(); + done(); + }); + }); + + it('should return \'Invalid API key\'', function(done) { + client.verifyKey(function(err, valid) { + expect(err.message).to.equal('Invalid API key'); + scope.done(); + done(); + }); + }); + + }); + + }); + + describe('when the request returns anything else', function() { + var client; var scope; @@ -105,9 +184,8 @@ describe('Akismet-api', function() { scope = nock('http://rest2.akismet.com') .matchHeader('Content-Type', 'application/x-www-form-urlencoded') .post('/1.1/verify-key') - .reply(200, 'invalid', { - 'Content-Type' : 'text/plain', - 'X-akismet-debug-help' : 'Could not find your key' + .reply(200, 'whatisthiserror', { + 'Content-Type' : 'text/plain' }); }); @@ -119,9 +197,9 @@ describe('Akismet-api', function() { }); }); - it('should return the akismet debug error', function(done) { + it('should return the response', function(done) { client.verifyKey(function(err, valid) { - expect(err.message).to.equal('Could not find your key'); + expect(err.message).to.equal('whatisthiserror'); scope.done(); done(); }); @@ -243,42 +321,86 @@ describe('Akismet-api', function() { }); describe('when the request returns something else', function() { + + describe('when the x-akismet-debug-help header is present', function() { - var client; - var scope; - - beforeEach(function() { - client = Akismet.client({ - blog : 'http://example.com', - key : 'testKey6' + var client; + var scope; + + beforeEach(function() { + client = Akismet.client({ + blog : 'http://example.com', + key : 'testKey6' + }); + scope = nock('http://testKey6.rest.akismet.com') + .matchHeader('Content-Type', 'application/x-www-form-urlencoded') + .post('/1.1/comment-check') + .reply(200, 'notAValidValueAtAll', { + 'Content-Type' : 'text/plain', + 'X-akismet-debug-help' : 'You did something wrong!' + }); + }); + + it('should return null', function(done) { + client.checkSpam({ + user_ip : '123.123.123.123' + }, function(err, spam) { + expect(spam).to.be.null; + scope.done(); + done(); + }); }); - scope = nock('http://testKey6.rest.akismet.com') - .matchHeader('Content-Type', 'application/x-www-form-urlencoded') - .post('/1.1/comment-check') - .reply(200, 'notAValidValueAtAll', { - 'Content-Type' : 'text/plain', - 'X-akismet-debug-help' : 'You did something wrong!' + + it('should return the akismet debug error', function(done) { + client.checkSpam({ + user_ip : '123.123.123.123' + }, function(err, spam) { + expect(err.message).to.equal('You did something wrong!'); + scope.done(); + done(); + }); }); - }); - it('should return null', function(done) { - client.checkSpam({ - user_ip : '123.123.123.123' - }, function(err, spam) { - expect(spam).to.be.null; - scope.done(); - done(); - }); }); + + describe('when the x-akismet-debug-help header is not present', function() { - it('should return the akismet debug error', function(done) { - client.checkSpam({ - user_ip : '123.123.123.123' - }, function(err, spam) { - expect(err.message).to.equal('You did something wrong!'); - scope.done(); - done(); + var client; + var scope; + + beforeEach(function() { + client = Akismet.client({ + blog : 'http://example.com', + key : 'testKey6' + }); + scope = nock('http://testKey6.rest.akismet.com') + .matchHeader('Content-Type', 'application/x-www-form-urlencoded') + .post('/1.1/comment-check') + .reply(200, 'notAValidValueAtAll', { + 'Content-Type' : 'text/plain' + }); + }); + + it('should return null', function(done) { + client.checkSpam({ + user_ip : '123.123.123.123' + }, function(err, spam) { + expect(spam).to.be.null; + scope.done(); + done(); + }); }); + + it('should return the response', function(done) { + client.checkSpam({ + user_ip : '123.123.123.123' + }, function(err, spam) { + expect(err.message).to.equal('notAValidValueAtAll'); + scope.done(); + done(); + }); + }); + }); });