Skip to content

Commit 91b404b

Browse files
author
Hovhannes Babayan
committed
Added tests for Api constructor, moved non-exported methods into plugins/ folder
1 parent f51765a commit 91b404b

8 files changed

Lines changed: 54 additions & 17 deletions

File tree

src/Api.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
1+
/**
2+
* Creates new Api instance. Accepts configuration object with the following keys:
3+
* hostname {String} - Required. A name of host to connect to
4+
* port {String} - Optional. A port on host to connect to. Defaults to 80.
5+
* version {String} - Optional. Which version of api to use.
6+
* At the moment of writing can be 1.0, 2.0, 3.0, 4.0, 5.0.
7+
* Pass 'internal' to use AtTask internal API (this is the latest version, maybe unstable)
8+
* @param {Object} config
9+
* @constructor
10+
*/
111
function Api(config) {
212
this.httpOptions = {
313
hostname: config.hostname,
414
port: config.port || 80,
5-
path: '/attask/api',
615
method: 'GET',
716
withCredentials: false
817
};
918

10-
// Append version to hostname if provided
11-
if (config.version) {
12-
this.httpOptions.path = this.httpOptions.path + '/v' + config.version;
19+
// Append version to path if provided
20+
var path;
21+
if (config.version === 'internal') {
22+
path = '/attask/api-internal'
1323
}
24+
else {
25+
path = '/attask/api';
26+
if (config.version) {
27+
path = path + '/v' + config.version;
28+
}
29+
}
30+
this.httpOptions.path = path;
1431
}
1532

16-
require('./request')(Api);
17-
require('./login')(Api);
18-
require('./logout')(Api);
19-
require('./search')(Api);
20-
require('./get')(Api);
21-
require('./post')(Api);
33+
var plugins = [
34+
'request',
35+
'login',
36+
'logout',
37+
'search',
38+
'get',
39+
'post'
40+
];
41+
for(var i=0; i<plugins.length; ++i) {
42+
require('./plugins/' + plugins[i])(Api);
43+
}
2244

2345
module.exports = Api;

src/get.js renamed to src/plugins/get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Api = require('./Api');
1+
var Api = require('./../Api');
22

33
module.exports = function(Api) {
44
/**

src/login.js renamed to src/plugins/login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Api = require('./Api');
1+
var Api = require('./../Api');
22

33
module.exports = function(Api) {
44
Api.prototype.login = function (username, password) {

src/logout.js renamed to src/plugins/logout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Api = require('./Api');
1+
var Api = require('./../Api');
22

33
module.exports = function(Api) {
44
Api.prototype.logout = function () {

src/post.js renamed to src/plugins/post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Api = require('./Api');
1+
var Api = require('./../Api');
22

33
module.exports = function(Api) {
44
/**

src/request.js renamed to src/plugins/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Api = require('./Api'),
1+
var Api = require('./../Api'),
22
queryString = require('querystring'),
33
http = require('http'),
44
util = require('util');

src/search.js renamed to src/plugins/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Api = require('./Api');
1+
var Api = require('./../Api');
22

33
module.exports = function(Api) {
44
Api.prototype.search = function (objCode, query, fields) {

test/ApiSpec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Api = require('./../src/Api');
44

55

66
describe('Create new instance for API', function() {
7-
it('should have methods', function(){
7+
it('should have methods', function() {
88
var api = new Api({hostname: 'localhost'});
99
expect(api).to.respondTo('get');
1010
expect(api).to.respondTo('login');
@@ -21,4 +21,19 @@ describe('Create new instance for API', function() {
2121
expect(api).to.respondTo('namedQuery');
2222
expect(api).to.respondTo('metadata');
2323
});
24+
25+
it('should set correct API path based on passed configuration (version is passed)', function() {
26+
var api = new Api({version: '2.0'});
27+
expect(api.httpOptions.path).to.equal('/attask/api/v2.0');
28+
});
29+
30+
it('should set correct API path based on passed configuration (version is not passed)', function() {
31+
var api = new Api({});
32+
expect(api.httpOptions.path).to.equal('/attask/api');
33+
});
34+
35+
it('should set correct API path based on passed configuration (version="internal")', function() {
36+
var api = new Api({version: 'internal'});
37+
expect(api.httpOptions.path).to.equal('/attask/api-internal');
38+
});
2439
});

0 commit comments

Comments
 (0)