Skip to content
This repository was archived by the owner on Jun 13, 2020. It is now read-only.

Commit 981f07e

Browse files
committed
Merge pull request #13 from matiassingers/nock-http-tests
Use Nock for unit test
2 parents 637ce38 + 7fd5a7f commit 981f07e

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed

index.js

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,16 @@ module.exports = function(options) {
7878
* @param {Function} callback Function to callback with response.
7979
*/
8080
var api_get = function(options, callback) {
81-
if(!options.fake) {
82-
request({
83-
url: options.url,
84-
headers: {
85-
'Authorization': 'Bearer ' + access_token,
86-
'Accept': 'application/json'
87-
}
88-
},
89-
function(error, response, body) {
90-
callback(error, body);
91-
});
92-
}
93-
else {
94-
callback(null, options.url);
95-
}
81+
request({
82+
url: options.url,
83+
headers: {
84+
'Authorization': 'Bearer ' + access_token,
85+
'Accept': 'application/json'
86+
}
87+
},
88+
function(error, response, body) {
89+
callback(error, body);
90+
});
9691
};
9792

9893
/**
@@ -105,23 +100,18 @@ module.exports = function(options) {
105100
* @param {Function} callback Function to callback with response.
106101
*/
107102
var api_post = function(options, callback) {
108-
if(!options.fake) {
109-
request({
110-
method: 'post',
111-
url: options.url,
112-
headers: {
113-
'Authorization': 'Bearer ' + access_token,
114-
'Content-Type': 'application/x-www-form-urlencoded'
115-
},
116-
form: options.data
103+
request({
104+
method: 'post',
105+
url: options.url,
106+
headers: {
107+
'Authorization': 'Bearer ' + access_token,
108+
'Content-Type': 'application/x-www-form-urlencoded'
117109
},
118-
function(error, response, body) {
119-
callback(error, body);
120-
});
121-
}
122-
else {
123-
callback(null, options.url);
124-
}
110+
form: options.data
111+
},
112+
function(error, response, body) {
113+
callback(error, body);
114+
});
125115
};
126116

127117
/**
@@ -132,76 +122,71 @@ module.exports = function(options) {
132122
* @param {Function} callback Function to callback with response.
133123
*/
134124
var api_delete = function(options, callback) {
135-
if(!options.fake) {
136-
request({
137-
method: 'delete',
138-
url: options.url,
139-
headers: {
140-
'Authorization': 'Bearer ' + access_token
141-
}
142-
},
143-
function(error, response, body) {
144-
callback(error, body);
145-
});
146-
}
147-
else {
148-
callback(null, options.url);
149-
}
125+
request({
126+
method: 'delete',
127+
url: options.url,
128+
headers: {
129+
'Authorization': 'Bearer ' + access_token
130+
}
131+
},
132+
function(error, response, body) {
133+
callback(error, body);
134+
});
150135
};
151136

152137
function create_getter_xid(str) {
153-
return function(options, callback, fake) {
138+
return function(options, callback) {
154139
if(good_params(options, callback)) {
155140
var xid = options.xid;
156141
var url = BASE_URL + (xid ? '/' + str + '/' + xid : '/users/@me/' + str + '?' + serialize(options));
157-
api_get({ url: url, fake: fake }, callback);
142+
api_get({ url: url }, callback);
158143
}
159144
};
160145
}
161146

162147
function create_getter(str) {
163-
return function(options, callback, fake) {
148+
return function(options, callback) {
164149
if(good_params(options, callback)) {
165150
var url = BASE_URL + '/users/@me/' + str;
166-
api_get({ url: url, fake: fake }, callback);
151+
api_get({ url: url }, callback);
167152
}
168153
};
169154
}
170155

171156
function create_getter_type(obj_str, str_type) {
172-
return function(options, callback, fake) {
157+
return function(options, callback) {
173158
if(good_params(options, callback)) {
174159
var xid = options.xid;
175160
if(!xid) {
176161
callback({ error: true, message: ERROR_NO_XID }, null);
177162
}
178163
else {
179164
var url = BASE_URL + '/'+ obj_str + '/' + xid + '/' + str_type;
180-
api_get({ url: url, fake: fake }, callback);
165+
api_get({ url: url }, callback);
181166
}
182167
}
183168
};
184169
}
185170

186171
function create_creator(obj_str) {
187-
return function(data, callback, fake) {
172+
return function(data, callback) {
188173
if(good_params(data, callback)) {
189174
var url = BASE_URL + '/users/@me/' + obj_str;
190-
api_post({ url: url, fake: fake, data: data }, callback);
175+
api_post({ url: url, data: data }, callback);
191176
}
192177
};
193178
}
194179

195180
function create_deletor(obj_str) {
196-
return function(options, callback, fake) {
181+
return function(options, callback) {
197182
if(good_params(options, callback)) {
198183
var xid = options.xid;
199184
if(!xid) {
200185
return callback({ error: true, message: ERROR_NO_XID }, null);
201186
}
202187
else {
203188
var url = BASE_URL + '/' + obj_str + '/' + xid;
204-
api_delete({ url: url, fake: fake }, callback);
189+
api_delete({ url: url }, callback);
205190
}
206191
}
207192
};
@@ -225,7 +210,7 @@ module.exports = function(options) {
225210
var get_refreshToken = function(callback) {
226211
if(client_secret) {
227212
var url = BASE_URL + '/users/@me/refreshToken';
228-
api_post({ url: url, fake: false, data: { secret: client_secret } }, callback);
213+
api_post({ url: url, data: { secret: client_secret } }, callback);
229214
}
230215
else {
231216
var error = { error: true, message: ERROR_NO_CLIENT_SECRET };
@@ -246,10 +231,10 @@ module.exports = function(options) {
246231
}
247232
};
248233

249-
var delete_webhook = function(callback, fake) {
234+
var delete_webhook = function(callback) {
250235
if(good_params(options, callback)) {
251236
var url = BASE_URL + '/users/@me/pubsub';
252-
api_delete({ url: url, fake: fake }, callback);
237+
api_delete({ url: url }, callback);
253238
}
254239
};
255240

@@ -271,7 +256,7 @@ module.exports = function(options) {
271256

272257
/** @class me */
273258
me: {
274-
get: create_getter(''),
259+
get: create_getter('')
275260
},
276261
/** @class moves */
277262
moves: {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"main": "index.js",
1111
"devDependencies": {
1212
"jsdoc": "git+https://github.com/jsdoc3/jsdoc.git",
13-
"should": "~2.1.0",
14-
"mocha": "~1.14.0"
13+
"mocha": "^2.0.1",
14+
"nock": "^0.51.0"
1515
},
1616
"optionalDependencies": {},
1717
"engines": {

test/config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/index.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1-
var config = require('./config');
2-
var should = require('should');
3-
var debug = false;
4-
var up = require('../index')(config);
1+
var up = require('../index')({});
2+
var assert = require('assert');
3+
var nock = require('nock');
54

6-
describe('up', function(){
5+
nock.disableNetConnect();
6+
var baseApi = nock('https://jawbone.com')
7+
.matchHeader('Authorization', /.*/);
78

9+
describe('up', function(){
810
describe('.moves', function(){
911
describe('.get()', function(){
10-
it('should return correct url', function(){
12+
it('should call correct url', function(done){
13+
var api = baseApi.matchHeader('Accept', 'application/json')
14+
.get('/nudge/api/v.1.1/users/@me/moves?')
15+
.reply(200, 'OK!');
16+
1117
up.moves.get({}, function(err, body) {
12-
body.should.equal('https://jawbone.com/nudge/api/v.1.1/users/@me/moves?');
13-
}, debug);
18+
assert.equal(err, void 0);
19+
assert.equal(body, 'OK!');
20+
21+
api.done();
22+
23+
done();
24+
});
1425
});
1526
});
1627

1728
describe('.get({ xid: 123 })', function(){
18-
it('should return correct url', function(){
29+
it('should return correct url', function(done){
30+
var api = baseApi.matchHeader('Accept', 'application/json')
31+
.get('/nudge/api/v.1.1/moves/123')
32+
.reply(200, 'OK!');
33+
1934
up.moves.get({ xid: 123 }, function(err, body) {
20-
body.should.equal('https://jawbone.com/nudge/api/v.1.1/moves/123');
21-
}, debug);
35+
assert.equal(err, void 0);
36+
assert.equal(body, 'OK!');
37+
38+
api.done();
39+
40+
done();
41+
});
2242
});
2343
});
2444
});

0 commit comments

Comments
 (0)