Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial simple integration test #153

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@
"querystring": "0.1.0",
"supervisor": ">= 0.5.x"
},
"devDependencies": {},
"devDependencies": {
"mocha": "~1.15.1",
"should": "~2.1.1",
"supertest": "~0.8.2",
"nock": "~0.25.0"
},
"main": "index",
"engines": {
"node": ">= 0.4.0",
"npm": ">= 1.1.49"
},
"scripts": {
"start": "node_modules/.bin/supervisor -e 'js|json' app",
"startwin": "supervisor -e 'js' app"
"startwin": "supervisor -e 'js' app",
"test": "node_modules/.bin/mocha --ui bdd --reporter spec"
}
}
48 changes: 48 additions & 0 deletions test/SimpleApiIntegrationSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var nock = require('nock'),
should = require('should'),
processRequestBuilderFactory = require('./helpers/process-request-builder.js'),
configSpawner = require('./helpers/config-spawner.js');

describe('Integrating I/O Docs with a simple unauthenticated API', function() {
var app;
var service = nock('http://localhost:3001');
var processRequestBuilder;

before(function() {
configSpawner.setUpConfig(function(config){
config.apiConfigDir = 'test/configs/simple';
});
app = require('../app.js');
processRequestBuilder = processRequestBuilderFactory('simple');
});

after(function() {
configSpawner.resetConfig();
});

it('relays request to GET resource by ID', function(done) {
var id = 1234;
var resource = {
id: 1234,
name: "test resource"
};
var getById = service
.get('//resources/'+id)
.reply(200, resource);

processRequestBuilder
.get("/resources/:id")
.withQueryParam('id', ''+id)
.makeRequest(app)
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
res.body.headers.should.eql({});
JSON.parse(res.body.response).should.eql(resource);
res.body.call.should.eql('localhost:3001//resources/1234');
res.body.code.should.eql(200);
getById.done();
done(err);
});
});
});
11 changes: 11 additions & 0 deletions test/configs/simple/apiconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"simple": {
"name": "Simple",
"protocol": "http",
"baseURL": "localhost:3001",
"publicPath": "/",
"headers" : {},
"auth": "",
"keyParam": "key"
}
}
25 changes: 25 additions & 0 deletions test/configs/simple/simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"endpoints":[
{
"name": "CRUD operations",
"methods": [
{
"MethodName":"Get some resource by ID",
"Synopsis":"Lorem ipsum dolor sit amet",
"HTTPMethod":"GET",
"URI":"/resources/:id",
"RequiresOAuth":"N",
"parameters":[
{
"Name":"id",
"Required":"Y",
"Default":"",
"Type":"string",
"Description":"A resource ID (e.g. 500042487)"
}
]
}
]
}
]
}
36 changes: 36 additions & 0 deletions test/helpers/config-spawner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var fs = require('fs'),
path = require('path');

var configJsonPath = path.join(process.cwd(), 'config.json');
var configJsonBackupPath = configJsonPath + '.bak';
var configJsonSamplePath = configJsonPath + '.sample';
var configWasPresentBeforeTest = false;

/**
* Back-up any existing config.json and write a new one based on config.json.sample (modified by updateCallback).
*
* @param updateCallback {function}
*/
exports.setUpConfig = function(updateCallback) {
if (fs.existsSync(configJsonPath)) {
configWasPresentBeforeTest = true;
fs.renameSync(configJsonPath, configJsonBackupPath);
}
var config = JSON.parse(fs.readFileSync(configJsonSamplePath));
updateCallback(config);
fs.writeFileSync(configJsonPath, JSON.stringify(config, null, 4));
};

/**
* Removes any config.json that may exist, and replaces any back-up that may have existed previously
*/
exports.resetConfig = function() {
if (fs.existsSync(configJsonPath)) {
if (fs.existsSync(configJsonBackupPath)) {
fs.unlinkSync(configJsonPath);
fs.renameSync(configJsonBackupPath, configJsonPath);
} else if (!configWasPresentBeforeTest) {
fs.unlinkSync(configJsonPath);
}
}
};
58 changes: 58 additions & 0 deletions test/helpers/process-request-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var request = require('supertest');

function ServiceBuilder(apiName) {
var get = function(URI) {
return ProcessRequestBuilder(apiName, "GET", URI);
};

return {
get: get
};
}

function ProcessRequestBuilder(apiName, httpMethod, URI) {
var params = {};
var locations = {};

/**
* @param key {string}
* @param value {string}
* @returns {ProcessRequestBuilder}
*/
var withQueryParam = function(key, value) {
params[key] = value;
locations[key] = 'query';
return this;
};

var makeRequest = function(app) {
return request(app)
.post('/processReq')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({
httpMethod: httpMethod,
oauth: "",
methodUri: URI,
accessToken: "",
params: params,
locations: locations,
apiKey: "undefined",
apiSecret: "undefined",
apiName: apiName
});
};

return {
withQueryParam: withQueryParam,
makeRequest: makeRequest
};
}

/**
*
* @param apiName {string}
* @returns {ServiceBuilder}
*/
module.exports = function(apiName) {
return ServiceBuilder(apiName);
};