Skip to content

Commit

Permalink
Added better error handling for akismet's responses
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfosterelli committed Jul 12, 2013
1 parent 042e646 commit d1c1cf9
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 39 deletions.
16 changes: 13 additions & 3 deletions lib/akismet.js
Expand Up @@ -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
Expand All @@ -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);
});
};

Expand Down Expand Up @@ -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);
});
};

Expand Down
2 changes: 1 addition & 1 deletion 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": {
Expand Down
192 changes: 157 additions & 35 deletions test/akismet.spec.js
Expand Up @@ -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');
});

Expand Down Expand Up @@ -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;

Expand All @@ -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'
});
});

Expand All @@ -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();
});
Expand Down Expand Up @@ -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();
});
});

});

});
Expand Down

0 comments on commit d1c1cf9

Please sign in to comment.