add tests for api
This commit is contained in:
parent
509a82ac3a
commit
dcc8ade166
@ -1,5 +1,6 @@
|
|||||||
BitcoreHTTP:
|
BitcoreHTTP:
|
||||||
port: 8080
|
port: 8080
|
||||||
|
logging: true
|
||||||
BitcoreNode:
|
BitcoreNode:
|
||||||
NetworkMonitor:
|
NetworkMonitor:
|
||||||
network: livenet
|
network: livenet
|
||||||
|
|||||||
@ -1,10 +1,8 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var config = require('config');
|
var config = require('config');
|
||||||
var BitcoreHTTP = require('./lib/http');
|
var BitcoreHTTP = require('./lib/http');
|
||||||
|
|
||||||
var app = BitcoreHTTP.create(config.get('BitcoreHTTP'));
|
var http = BitcoreHTTP.create(config.get('BitcoreHTTP'));
|
||||||
app.start();
|
http.start();
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,7 @@ function BitcoreHTTP(node, opts) {
|
|||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
this.node = node;
|
this.node = node;
|
||||||
this.port = opts.port || 8000;
|
this.port = opts.port || 8000;
|
||||||
|
this.logging = opts.logging || false;
|
||||||
this.setupExpress();
|
this.setupExpress();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,25 +40,16 @@ BitcoreHTTP.prototype.setupExpress = function() {
|
|||||||
app.use(bodyParser.urlencoded({
|
app.use(bodyParser.urlencoded({
|
||||||
extended: false
|
extended: false
|
||||||
}));
|
}));
|
||||||
app.use(morgan('dev'));
|
if (this.logging) {
|
||||||
|
app.use(morgan('dev'));
|
||||||
|
}
|
||||||
|
|
||||||
// install routes
|
// install routes
|
||||||
app.use('/', routes(this.node));
|
app.use('/', routes(this.node));
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
app.use(function(req, res, next) {
|
app.use(function(req, res) {
|
||||||
var err = new Error('Not Found');
|
res.status(404).send('Not Found');
|
||||||
err.status = 404;
|
|
||||||
next(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
// production error handler
|
|
||||||
app.use(function(err, req, res) {
|
|
||||||
res.status(err.status || 500);
|
|
||||||
res.send({
|
|
||||||
message: err.message,
|
|
||||||
error: {}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.set('port', this.port);
|
app.set('port', this.port);
|
||||||
|
|||||||
32
api/test/http.js
Normal file
32
api/test/http.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var chai = require('chai');
|
||||||
|
var should = chai.should();
|
||||||
|
|
||||||
|
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||||
|
|
||||||
|
var BitcoreHTTP = require('../lib/http');
|
||||||
|
|
||||||
|
describe('BitcoreHTTP', function() {
|
||||||
|
|
||||||
|
// mocks
|
||||||
|
var nodeMock;
|
||||||
|
beforeEach(function() {
|
||||||
|
nodeMock = new EventEmitter();
|
||||||
|
});
|
||||||
|
describe('instantiates', function() {
|
||||||
|
it('from constructor', function() {
|
||||||
|
var http = new BitcoreHTTP(nodeMock);
|
||||||
|
should.exist(http);
|
||||||
|
});
|
||||||
|
it('from create', function() {
|
||||||
|
var http = new BitcoreHTTP.create();
|
||||||
|
should.exist(http);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('starts', function() {
|
||||||
|
var http = new BitcoreHTTP(nodeMock);
|
||||||
|
http.start.bind(http).should.not.throw();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
40
api/test/routes.js
Normal file
40
api/test/routes.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var chai = require('chai');
|
||||||
|
var should = chai.should();
|
||||||
|
var request = require('supertest');
|
||||||
|
|
||||||
|
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||||
|
|
||||||
|
var BitcoreHTTP = require('../lib/http');
|
||||||
|
|
||||||
|
describe('BitcoreHTTP routes', function() {
|
||||||
|
|
||||||
|
// mocks
|
||||||
|
var nodeMock, app, agent;
|
||||||
|
beforeEach(function() {
|
||||||
|
var opts = {
|
||||||
|
port: 1234
|
||||||
|
};
|
||||||
|
nodeMock = new EventEmitter();
|
||||||
|
app = new BitcoreHTTP(nodeMock, opts).app;
|
||||||
|
agent = request(app);
|
||||||
|
});
|
||||||
|
it('404s', function(cb) {
|
||||||
|
agent.get('/invalid/url/')
|
||||||
|
.expect(404, cb);
|
||||||
|
});
|
||||||
|
it('main', function(cb) {
|
||||||
|
agent.get('/')
|
||||||
|
.expect(200)
|
||||||
|
.expect('bitcore-node API', cb);
|
||||||
|
});
|
||||||
|
it('blocks', function(cb) {
|
||||||
|
agent.get('/v1/blocks/')
|
||||||
|
.expect(200)
|
||||||
|
.expect({
|
||||||
|
'message': 'This is a mocked response'
|
||||||
|
}, cb);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
@ -80,6 +80,7 @@
|
|||||||
"chai": "*",
|
"chai": "*",
|
||||||
"gulp": "^3.8.10",
|
"gulp": "^3.8.10",
|
||||||
"should": "^2.1.1",
|
"should": "^2.1.1",
|
||||||
"sinon": "^1.10.3"
|
"sinon": "^1.10.3",
|
||||||
|
"supertest": "^0.15.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
test/mocha.opts
Normal file
4
test/mocha.opts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
test/
|
||||||
|
api/test
|
||||||
|
--recursive
|
||||||
|
-R spec
|
||||||
Loading…
Reference in New Issue
Block a user