Giter Club home page Giter Club logo

mock-server's Introduction

Mock Server

NPM version Build Status Coverage Status Dependencies

Mock OpenTSDB server.

NOTE: this server is not comprehensive and does not mock all aspects of OpenTSDB. The server mainly mocks the REST API and provides some useful routes when testing response handlers.

Install

For use in Node.js,

$ npm install opentsdb-mock-server

For use in the browser, use browserify.

Usage

To use the module,

var createApp = require( 'opentsdb-mock-server' );

To create a new mock OpenTSDB application,

var db = createApp();

The mock OpenTSDB application has the following attributes and methods...

db.url

The mock OpenTSDB URL endpoint.

console.log( db.url );

db.query

A mock OpenTSDB HTTP request query URL.

console.log( db.query );

db.aggregators

Mock OpenTSDB aggregation functions.

console.log( db.aggregators );
// returns [...]

db.metrics

Mock OpenTSDB metrics.

console.log( db.metrics );
// returns [...]

db.config

Mock OpenTSDB configuration.

console.log( db.config );
// returns {...}

db.version

Mock OpenTSDB version information.

console.log( db.version );
// returns {...}

db.dropcaches

Mock OpenTSDB response to a request to drop caches.

console.log( db.dropcaches );
// returns {...}

db.createServer( clbk )

Creates a mock OpenTSDB application HTTP server. The provided callback is invoked once the server is listening and ready to receive HTTP requests.

var server = db.createServer( function onListen() {
	console.log( '...listening...' );
});

Routes

The application has the following routes...

GET /api/query

Mocks the OpenTSDB query API.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.query
}, function onResponse( error, response, body ) {
	console.log( body );
});

GET /api/aggregators

Returns the list of aggregators supported by the mock OpenTSDB.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/api/aggregators'
}, function onResponse( error, response, body ) {
	console.log( body );
});

GET /api/suggest

Returns a list of metrics.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/api/suggest'
}, function onResponse( error, response, body ) {
	console.log( body );
});

GET /api/config

Returns a mock OpenTSDB configuration.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/api/config'
}, function onResponse( error, response, body ) {
	console.log( body );
});

GET /api/version

Returns mock OpenTSDB version information.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/api/version'
}, function onResponse( error, response, body ) {
	console.log( body );
});

GET /api/dropcaches

Returns a mock OpenTSDB response to a drop cache request.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/api/dropcaches'
}, function onResponse( error, response, body ) {
	console.log( body );
});

GET /bad_body

Returns an empty body; useful when testing HTTP response handling.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/bad_body'
}, function onResponse( error, response, body ) {
	console.log( body );
});

GET /bad_json

Returns improperly formatted JSON; useful when testing HTTP response handling.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/bad_json'
}, function onResponse( error, response, body ) {
	try {
		JSON.parse( body );
	} catch ( err ) {
		console.log( err );
	}
});

GET /no_data

Returns an empty array; useful when testing HTTP response handling in the event of no data.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/no_data'
}, function onResponse( error, response, body ) {
	console.log( body );
});

GET /good_data

Returns a sample successful data response.

var request = require( 'request' );

request({
	'method': 'GET',
	'uri': db.url + '/good_data'
}, function onResponse( error, response, body ) {
	console.log( body );
});

Examples

var createApp = require( 'opentsdb-mock-server' ),
	request = require( 'request' );

// Create a new mock OpenTSDB application:
var db = createApp();

// Get the mock version:
console.log( db.version );

// Get the mock URL:
console.log( db.url );

// Start the mock server:
var server = db.createServer( function onListen() {
	console.log( '...listening...' );

	request({
		'method': 'GET',
		'uri': db.query
	}, function onResponse( error, response, body ) {
		console.log( body );

		// Close the server:
		server.close();
	});
});

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.


Copyright

Copyright © 2014. Athan Reines.

mock-server's People

Contributors

jabyrd3 avatar kgryte avatar

Watchers

James Cloos avatar  avatar Christian Saide avatar Renee Orser avatar Tom Tran avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.