Skip to content

Commit 9f73f73

Browse files
author
Simon Taranto
committed
Implement file upload without name test
* Adds a test for `files.upload` (without custom name). * Adds `nock` as a `devDependency` for testing. I'd enjoy any feedback on the approach I took and would also be happy to add coverage to some of the other functions if we're happy with the general direction here. Conversation started in Skycatch#3 (comment)
1 parent 9988c2c commit 9f73f73

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Search.prototype.new = function (query, params, callback) {
9090
var keys = Object.keys(params);
9191
for (var i = 0, len = keys.length; i < len; i++) {
9292
var key = keys[i];
93-
93+
9494
uri += '&'+key+'='+params[key];
9595
}
9696
}
@@ -107,7 +107,7 @@ Search.prototype.new = function (query, params, callback) {
107107

108108
callback(null, res.body);
109109
});
110-
}
110+
};
111111

112112

113113
// Files Resource

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"superagent": "~0.18.0"
2525
},
2626
"devDependencies": {
27-
"prova": "latest"
27+
"nock": "latest",
28+
"prova": "latest",
2829
}
2930
}

test/index.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var Box = require('../lib/'),
2-
test = require('prova'),
2+
nock = require('nock'),
33
path = require('path'),
4-
async = require('async');
4+
test = require('prova');
55

66
var fs = require('fs'),
77
request = require('superagent');
@@ -10,3 +10,26 @@ var fs = require('fs'),
1010
var box = new Box({
1111
access_token: 'null'
1212
});
13+
14+
test('file.upload without custom filename', function (t) {
15+
t.plan(1);
16+
var boxFileId = 'box-file-id',
17+
uploadFileName = 'a.txt',
18+
filepath = __dirname + '/' + uploadFileName,
19+
folderId = '1234';
20+
21+
nock(box.options.upload_url)
22+
.post('/files/content', function(body) {
23+
folderIdParamRegex = 'name="parent_id"' + folderId;
24+
filenameParamRegex = 'name="filename"; filename="' + uploadFileName + '"';
25+
return body.match(folderIdParamRegex) && body.match(filenameParamRegex);
26+
})
27+
.reply(201, { id: boxFileId });
28+
29+
box.files.upload(filepath, folderId, function(err, success) {
30+
if (err) {
31+
throw err;
32+
}
33+
t.equal(success.id, boxFileId);
34+
});
35+
});

0 commit comments

Comments
 (0)