Skip to content

Commit beb92b5

Browse files
author
Hovhannes Babayan
committed
more tests
1 parent fd875fb commit beb92b5

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

src/plugins/request.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = function(Api) {
55
Api.prototype.request = function(path, params, fields, method) {
66
params = params || {};
77
fields = fields || [];
8-
method = method || 'GET';
98

109
var options = {};
1110
util._extend(options, this.httpOptions);
@@ -21,7 +20,10 @@ module.exports = function(Api) {
2120
params.fields = fields.join();
2221
}
2322

24-
options.path += '?' + queryString.stringify(params);
23+
params = queryString.stringify(params);
24+
if (params) {
25+
options.path += '?' + params;
26+
}
2527

2628
var httpTransport = this.httpTransport;
2729

test/plugins/RequestSpec.js

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

66

7-
describe('Api. request() method', function() {
7+
describe('Api.request() method', function() {
88

99
it('should resolve returned promise with data if everything went ok', function(done) {
1010
var url = 'http://foobar:8080',
@@ -31,6 +31,97 @@ describe('Api. request() method', function() {
3131
expect(promise).to.eventually.deep.equal({'got': 'ok'}).and.notify(done);
3232
});
3333

34+
35+
it('should resolve returned promise with data if everything went ok (empty fields)', function(done) {
36+
var url = 'http://foobar:8080',
37+
path = '/test',
38+
params = {
39+
'foo': 1,
40+
'bar': 'baz'
41+
},
42+
fields = [],
43+
method = 'GET';
44+
45+
nock(url)
46+
.get('/attask/api' + path + '?foo=1&bar=baz')
47+
.reply(200, {
48+
data: {
49+
'got': 'ok'
50+
}
51+
});
52+
53+
var api = new Api({url: url});
54+
var promise = api.request(path, params, fields, method);
55+
expect(promise).to.eventually.deep.equal({'got': 'ok'}).and.notify(done);
56+
});
57+
58+
59+
it('should resolve returned promise with data if everything went ok (without fields)', function(done) {
60+
var url = 'http://foobar:8080',
61+
path = '/test',
62+
params = {
63+
'foo': 1,
64+
'bar': 'baz'
65+
},
66+
method = 'GET';
67+
68+
nock(url)
69+
.get('/attask/api' + path + '?foo=1&bar=baz')
70+
.reply(200, {
71+
data: {
72+
'got': 'ok'
73+
}
74+
});
75+
76+
var api = new Api({url: url});
77+
var promise = api.request(path, params, null, method);
78+
expect(promise).to.eventually.deep.equal({'got': 'ok'}).and.notify(done);
79+
});
80+
81+
82+
it('should resolve returned promise with data if everything went ok (without fields and params)', function(done) {
83+
var url = 'http://foobar:8080',
84+
path = '/test',
85+
method = 'GET';
86+
87+
nock(url)
88+
.get('/attask/api' + path)
89+
.reply(200, {
90+
data: {
91+
'got': 'ok'
92+
}
93+
});
94+
95+
var api = new Api({url: url});
96+
var promise = api.request(path, null, null, method);
97+
expect(promise).to.eventually.deep.equal({'got': 'ok'}).and.notify(done);
98+
});
99+
100+
101+
it('should resolve returned promise with data if everything went ok (path without leading /)', function(done) {
102+
var url = 'http://foobar:8080',
103+
path = 'test',
104+
params = {
105+
'foo': 1,
106+
'bar': 'baz'
107+
},
108+
fields = [],
109+
method = 'GET';
110+
111+
nock(url)
112+
.get('/attask/api/' + path + '?foo=1&bar=baz')
113+
.reply(200, {
114+
data: {
115+
'got': 'ok'
116+
}
117+
});
118+
119+
var api = new Api({url: url});
120+
var promise = api.request(path, params, fields, method);
121+
expect(promise).to.eventually.deep.equal({'got': 'ok'}).and.notify(done);
122+
});
123+
124+
34125
it('should reject returned promise with error data on error', function(done) {
35126
var url = 'http://foobar:8080',
36127
path = '/test',

0 commit comments

Comments
 (0)