Skip to content

Commit

Permalink
added https support and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hovhannes Babayan committed Dec 30, 2014
1 parent 680c3d0 commit fd875fb
Show file tree
Hide file tree
Showing 20 changed files with 132 additions and 68 deletions.
4 changes: 3 additions & 1 deletion dist/attask.min.js

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions examples/browser/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
<script src="/dist/attask.min.js"></script>
<script>
function doLogin() {
var host = document.getElementById('hostname').value;
var port = document.getElementById('port').value;
var url = document.getElementById('url').value;
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;

var instance = new window.AtTask.Api({hostname: host, version: '5.0', port: port});
var instance = new window.AtTask.Api({url: url, version: '5.0'});
instance.login(username, password).then(function(data){
console.log(data)
}, console.error);
Expand All @@ -27,12 +26,8 @@
<div class="container">
<div>
<div class="form-group">
<label for="hostname">Hostname</label>
<input type="text" class="form-control" id="hostname" value="localhost" placeholder="Hostname to use for connection to AtTask">
</div>
<div class="form-group">
<label for="port">Port</label>
<input type="text" class="form-control" id="port" value="8080" placeholder="Port to use for connection to AtTask">
<label for="url">Url</label>
<input type="text" class="form-control" id="url" value="http://localhost:8080" placeholder="Url to use for connection to AtTask">
</div>

<div class="form-group">
Expand Down
5 changes: 2 additions & 3 deletions examples/node/create-new-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in, then creates a new project with name "API Project"\n');
Expand Down
5 changes: 2 additions & 3 deletions examples/node/get-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in, returns metadata about available API resources\n');
Expand Down
5 changes: 2 additions & 3 deletions examples/node/get-multiple-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in, queries details of multiple tasks\n');
Expand Down
5 changes: 2 additions & 3 deletions examples/node/get-project-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in, queries project and project owner details by project id\n');
Expand Down
5 changes: 2 additions & 3 deletions examples/node/get-task-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in, queries metadata for the task object (objCode: TASK)\n');
Expand Down
5 changes: 2 additions & 3 deletions examples/node/get-user-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ var ApiConstants = require('./../../').ApiConstants;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in, returns number of active users\n');
Expand Down
5 changes: 2 additions & 3 deletions examples/node/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in\n');
Expand Down
5 changes: 2 additions & 3 deletions examples/node/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in and logs out\n');
Expand Down
5 changes: 2 additions & 3 deletions examples/node/search-for-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

var instance = ApiFactory.getInstance({
hostname: 'localhost',
version: '5.0',
port: 8080
url: 'http://localhost:8080',
version: '5.0'
});

util.print('Logs in, then search for projects with percentComplete > 0\n');
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"devDependencies": {
"browserify": "^7.0.3",
"chai": "^1.10.0",
"chai-as-promised": "^4.1.1",
"del": "^1.1.0",
"gulp": "^3.8.10",
"gulp-coveralls": "^0.1.3",
Expand All @@ -17,6 +18,7 @@
"gulp-mocha": "^2.0.0",
"gulp-uglify": "^1.0.2",
"gulp-webserver": "^0.9.0",
"nock": "^0.52.4",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.0.0"
},
Expand Down
17 changes: 12 additions & 5 deletions src/Api.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
var url = require('url'),
http = require('http'),
https = require('https');

/**
* Creates new Api instance.
* @param {Object} config An object with the following keys:<br/>
* <code>hostname</code> {String} - Required. A name of host to connect to<br/>
* <code>port</code> {String} - Optional. A port on host to connect to. Defaults to 80.<br/>
* <code>url</code> {String} - Required. An url to AtTask server (for example: http://localhost:8080)<br/>
* <code>version</code> {String} - Optional. Which version of api to use. At the moment of writing can be 1.0, 2.0, 3.0, 4.0. Pass 'internal' to use AtTask internal API (this is the latest version, maybe unstable)
* @constructor
*/
function Api(config) {
var parsed = url.parse(config.url);

// Create the request
this.httpTransport = parsed.protocol === 'https:' ? https : http;

this.httpOptions = {
hostname: config.hostname,
port: config.port || 80,
method: 'GET',
host: parsed.hostname,
port: parsed.port || 80,
withCredentials: false
};

Expand Down
3 changes: 1 addition & 2 deletions src/ApiFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ module.exports = {
* One can use this if Api is intended to be used as singleton.
* @memberOf AtTask.ApiFactory
* @param {Object} config An object with the following keys:<br/>
* <code>hostname</code> {String} - Required. A name of host to connect to<br/>
* <code>port</code> {String} - Optional. A port on host to connect to. Defaults to 80.<br/>
* <code>url</code> {String} - Required. An url to AtTask server (for example: http://localhost:8080)<br/>
* <code>version</code> {String} - Optional. Which version of api to use. At the moment of writing can be 1.0, 2.0, 3.0, 4.0. Pass 'internal' to use AtTask internal API (this is the latest version, maybe unstable)
* @param {Boolean} [returnNewInstance] If true, always creates a new instance
* @return {Api}
Expand Down
17 changes: 11 additions & 6 deletions src/plugins/request.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
var queryString = require('querystring'),
http = require('http'),
util = require('util');

module.exports = function(Api) {
Api.prototype.request = function(path, params, fields, method) {
params = params || {};
fields = fields || [];
method = method || this.httpOptions.method;
method = method || 'GET';

var options = {};
util._extend(options, this.httpOptions);
options.method = method;
options.path = this.httpOptions.path + '/' + path;
if (path.indexOf('/') === 0) {
options.path = this.httpOptions.path + path;
}
else {
options.path = this.httpOptions.path + '/' + path;
}

if (fields.length !== 0) {
params.fields = fields.join();
}

options.path += '?' + queryString.stringify(params);

var httpTransport = this.httpTransport;

return new Promise(function (resolve, reject) {
/*options = {
hostname: 'echo.jsontest.com',
port: 80,
url: 'http://echo.jsontest.com',
path: '/data/123',
withCredentials: false
};*/

var request = http.request(options, function (response) {
var request = httpTransport.request(options, function (response) {
var body = '';
if (typeof response.setEncoding === 'function') {
response.setEncoding('utf8');
Expand Down
14 changes: 6 additions & 8 deletions test/ApiFactorySpec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var expect = require('chai').expect;

var ApiFactory = require('./../src/ApiFactory');
var Api = require('./../src/Api');
require('./common');
var ApiFactory = require('./../').ApiFactory;
var Api = require('./../').Api;


describe('ApiFactory.getInstance() when called for the frist time', function() {
Expand All @@ -18,7 +17,7 @@ describe('ApiFactory.getInstance() when called multiple times', function() {

beforeEach(function() {
firstInstance = ApiFactory.getInstance({
hostname: 'foo'
url: 'http://foo'
});
});

Expand All @@ -28,7 +27,6 @@ describe('ApiFactory.getInstance() when called multiple times', function() {
});

it('should return Api instance when called for the first time', function(){
expect(firstInstance).to.exist();
expect(firstInstance).to.be.instanceOf(Api);
});

Expand All @@ -39,14 +37,14 @@ describe('ApiFactory.getInstance() when called multiple times', function() {

it('should return the same Api instance when called for the second time with another config and with 1 argument', function(){
var instance2 = ApiFactory.getInstance({
hostname: 'bar'
url: 'http://bar'
});
expect(instance2).to.equal(firstInstance);
});

it('should return another instance when called with 2 arguments for the second time', function(){
var instance2 = ApiFactory.getInstance({
hostname: 'bar'
url: 'http://bar'
}, true);
expect(instance2).not.to.equal(firstInstance);
});
Expand Down
13 changes: 6 additions & 7 deletions test/ApiSpec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
var expect = require('chai').expect;

var Api = require('./../src/Api');
require('./common');
var Api = require('./../').Api;


describe('Create new instance for API', function() {
it('should have methods', function() {
var api = new Api({hostname: 'localhost'});
var api = new Api({url: 'http://localhost'});
expect(api).to.respondTo('get');
expect(api).to.respondTo('login');
expect(api).to.respondTo('logout');
Expand All @@ -23,17 +22,17 @@ describe('Create new instance for API', function() {
});

it('should set correct API path based on passed configuration (version is passed)', function() {
var api = new Api({version: '2.0'});
var api = new Api({url: 'http://localhost', version: '2.0'});
expect(api.httpOptions.path).to.equal('/attask/api/v2.0');
});

it('should set correct API path based on passed configuration (version is not passed)', function() {
var api = new Api({});
var api = new Api({url: 'http://localhost'});
expect(api.httpOptions.path).to.equal('/attask/api');
});

it('should set correct API path based on passed configuration (version="internal")', function() {
var api = new Api({version: 'internal'});
var api = new Api({url: 'http://localhost', version: 'internal'});
expect(api.httpOptions.path).to.equal('/attask/api-internal');
});
});
5 changes: 2 additions & 3 deletions test/ApiUtilSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var expect = require('chai').expect;

var ApiUtil = require('./../src/ApiUtil');
require('./common');
var ApiUtil = require('./../').ApiUtil;


describe('ApiUtil', function() {
Expand Down
9 changes: 9 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var chai = require('chai');
var chaiAsPromised = require("chai-as-promised");

chai.use(chaiAsPromised);

var expect = chai.expect;

global.chai = chai;
global.expect = expect;
58 changes: 58 additions & 0 deletions test/plugins/RequestSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require('./../common');
var nock = require('nock');

var Api = require('./../../').Api;


describe('Api. request() method', function() {

it('should resolve returned promise with data if everything went ok', function(done) {
var url = 'http://foobar:8080',
path = '/test',
params = {
'foo': 1,
'bar': 'baz'
},
fields = [
'*', 'owner:*'
],
method = 'GET';

nock(url)
.get('/attask/api' + path + '?foo=1&bar=baz&fields=*%2Cowner%3A*')
.reply(200, {
data: {
'got': 'ok'
}
});

var api = new Api({url: url});
var promise = api.request(path, params, fields, method);
expect(promise).to.eventually.deep.equal({'got': 'ok'}).and.notify(done);
});

it('should reject returned promise with error data on error', function(done) {
var url = 'http://foobar:8080',
path = '/test',
params = {
'foo': 1,
'bar': 'baz'
},
fields = [
'*', 'owner:*'
],
method = 'GET';

nock(url)
.get('/attask/api' + path + '?foo=1&bar=baz&fields=*%2Cowner%3A*')
.reply(500, {
error: {
'message': 'fail'
}
});

var api = new Api({url: url});
var promise = api.request(path, params, fields, method);
expect(promise).to.be.rejectedWith({'message': 'fail'}).and.notify(done);
});
});

0 comments on commit fd875fb

Please sign in to comment.