Skip to content

Instantly share code, notes, and snippets.

@JavascriptMick
Last active August 29, 2015 14:14
Show Gist options
  • Save JavascriptMick/759ad37fe4f9e77244da to your computer and use it in GitHub Desktop.
Save JavascriptMick/759ad37fe4f9e77244da to your computer and use it in GitHub Desktop.
Integration Testing a Restful Endpoint with Request and Jasmine and Node.JS
//*****useage*******
//var config = require('config_' + (process.env.NODE_ENV || 'dev'));
console.log('*** using dev config ***');
exports.mongo_db_address = 'localhost';
exports.mongo_db_port = 27017;
exports.mongo_db_name = 'myDB';
exports.mongo_db_user = null;
exports.mongo_db_pass = null;
var request = require("request");
var config = require('../config_' + (process.env.NODE_ENV || 'dev'));
var MongoClient = require('mongodb').MongoClient;
describe('Test Rules API',function(){
beforeEach(function(done){
MongoClient.connect("mongodb://" + config.mongo_db_address + ":" + config.mongo_db_port + "/" + config.mongo_db_name, function (err, db) {
var collection = db.collection('rules');
collection.insert({"clientId": "test","ruleId": "test1"}, function (err, result) {
db.close();
done();
});
});
});
afterEach(function(done) {
MongoClient.connect("mongodb://" + config.mongo_db_address + ":" + config.mongo_db_port + "/" + config.mongo_db_name, function (err, db) {
var collection = db.collection('rules');
collection.remove({ "clientId": "test" }, function (err, result) {
db.close();
done();
});
});
});
it("should retrieve a specific rule", function(done) {
request({
uri: "http://localhost:3000/api/rules/test/test1",
json: true
}, function(error, response, body){
expect(body.clientId).toEqual("test");
expect(body.ruleId).toEqual("test1");
done();
});
});
it("should retrieve all rules for the test client", function(done) {
request({
uri: "http://localhost:3000/api/rules/test",
json: true
}, function(error, response, body){
expect(body[0].clientId).toEqual("test");
done();
});
});
it("should store a rule", function(done) {
request({
method:'POST',
uri: "http://localhost:3000/api/rules",
json:{"clientId": "test","ruleId": "test2"}
}, function(error, response, bodyPost){
request({
url: "http://localhost:3000/api/rules/test/test2",
json:true
}, function(error, response, body){
expect(body.ruleId).toEqual("test2");
done();
});
});
});
it("should delete a rule", function(done) {
request({
method:'DELETE',
uri: "http://localhost:3000/api/rules/test/test1"
}, function(error, response, deleteBody){
request({
url: "http://localhost:3000/api/rules/test",
json: true
}, function(error, response, body){
expect(body.length).toEqual(0);
done();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment