handle cases where bitcoin client returns html
Fixes GH-9
This commit is contained in:
parent
79ab65759a
commit
d7ab0c6e4d
@ -59,10 +59,25 @@ var Client = function(port, host, user, password) {
|
||||
// depending on whether it's got a result or an error, we call
|
||||
// emitSuccess or emitError on the promise.
|
||||
response.on('end', function() {
|
||||
var decoded = JSON.parse(buffer);
|
||||
try {
|
||||
var decoded = JSON.parse(buffer);
|
||||
} catch (e) {
|
||||
if (response.statusCode !== 200) {
|
||||
errback({
|
||||
code: -32602,
|
||||
message: 'Invalid params, response status code: ' + response.statusCode
|
||||
});
|
||||
} else {
|
||||
errback({
|
||||
code: -32603,
|
||||
message: 'Problem parsing JSON response from server'
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(decoded.hasOwnProperty('error') && decoded.error != null) {
|
||||
if (errback)
|
||||
errback(decoded.error)
|
||||
errback(decoded.error);
|
||||
} else if(decoded.hasOwnProperty('result')) {
|
||||
if (callback)
|
||||
callback(decoded.result);
|
||||
@ -159,8 +174,6 @@ Server.prototype.handlePOST = function(req, res) {
|
||||
var handle = function (buf) {
|
||||
var decoded = JSON.parse(buf);
|
||||
|
||||
console.log(decoded);
|
||||
|
||||
// Check for the required fields, and if they aren't there, then
|
||||
// dispatch to the handleInvalidRequest function.
|
||||
if(!(decoded.method && decoded.params)) {
|
||||
|
||||
34
test/api.js
34
test/api.js
@ -3,7 +3,7 @@ var path = require('path');
|
||||
// test variables
|
||||
|
||||
var test = {
|
||||
account: "test"
|
||||
account: 'test'
|
||||
};
|
||||
|
||||
var config = require('./config');
|
||||
@ -139,18 +139,40 @@ vows.describe('api').addBatch({
|
||||
}
|
||||
}
|
||||
},
|
||||
"creating a bitcoin related error": {
|
||||
'creating a bitcoin related error': {
|
||||
topic: function(client) {
|
||||
client.cmd('nomethod', this.callback);
|
||||
},
|
||||
"should create non-null err in callback": function(err, expectedValue) {
|
||||
'should create non-null err in callback': function(err, expectedValue) {
|
||||
assert.deepEqual(err, {
|
||||
code: -32601,
|
||||
message: 'Method not found'
|
||||
});
|
||||
assert.equal(expectedValue, undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
'invalid credentials': {
|
||||
topic: function() {
|
||||
return new bitcoin.Client(config.host, config.port, 'baduser', 'badpwd');
|
||||
},
|
||||
'should still return client object': function(client) {
|
||||
assert.equal(typeof client, 'object');
|
||||
assert.equal(client.host, config.host);
|
||||
assert.equal(client.port, config.port);
|
||||
assert.equal(client.user, 'baduser');
|
||||
assert.equal(client.pass, 'badpwd');
|
||||
},
|
||||
'will return status 401 with html': {
|
||||
topic: function(client) {
|
||||
client.getDifficulty(this.callback);
|
||||
},
|
||||
'and should be able to handle it': function(err, difficulty) {
|
||||
assert.isNotNull(err);
|
||||
assert.isObject(err);
|
||||
assert.equal(difficulty, undefined);
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
}).export(module);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user