Permalink
Browse files

Added unit test for insertFile

Formatting and whitespace
  • Loading branch information...
1 parent 105b12f commit a6609425fe0e48971d2dc0537ef9a2be112e2807 @tenorviol committed Jan 3, 2013
Showing with 42 additions and 32 deletions.
  1. +25 −31 lib/Parse.js
  2. +17 −1 test/Parse.test.js
View
@@ -11,17 +11,17 @@ Parse.prototype = {
_api_protocol: require('https'),
_api_host: 'api.parse.com',
_api_port: 443,
-
+
// add object to class store
insert: function (className, object, callback) {
parseRequest.call(this, 'POST', '/1/classes/' + className, object, callback);
},
-
- // add files
- insertFile: function(fileName, data, contentType, callback){
- parseRequest.call(this, 'POST', '/1/files/' + fileName, data, callback, contentType);
+
+ // add files
+ insertFile: function(fileName, data, contentType, callback) {
+ parseRequest.call(this, 'POST', '/1/files/' + fileName, data, callback, contentType);
},
-
+
// get objects from class store
find: function (className, query, callback) {
if (typeof query === 'string') {
@@ -30,12 +30,12 @@ Parse.prototype = {
parseRequest.call(this, 'GET', '/1/classes/' + className, { where: JSON.stringify(query) }, callback);
}
},
-
+
// update an object in the class store
update: function (className, objectId, object, callback) {
parseRequest.call(this, 'PUT', '/1/classes/' + className + '/' + objectId, object, callback);
},
-
+
// remove an object from the class store
'delete': function (className, objectId, callback) {
parseRequest.call(this, 'DELETE', '/1/classes/' + className + '/' + objectId, null, callback);
@@ -49,9 +49,9 @@ function parseRequest(method, path, data, callback, contentType) {
Authorization: auth,
Connection: 'Keep-alive'
};
-
+
var body = null;
-
+
switch (method) {
case 'GET':
if (data) {
@@ -60,36 +60,30 @@ function parseRequest(method, path, data, callback, contentType) {
break;
case 'POST':
case 'PUT':
- if(contentType){
- body = data;
- headers['Content-type'] = contentType;
- console.log('Sending data type: ' + contentType + ' of length: ' + body.length);
- }else{
- headers['Content-type'] = 'application/json';
- body = JSON.stringify(data);
- }
+ body = typeof data === 'object' ? JSON.stringify(data) : data;
headers['Content-length'] = body.length;
+ headers['Content-type'] = contentType || 'application/json';
break;
case 'DELETE':
headers['Content-length'] = 0;
break;
default:
throw new Error('Unknown method, "' + method + '"');
}
-
+
var options = {
host: this._api_host,
port: this._api_port,
headers: headers,
path: path,
method: method
};
-
+
var req = this._api_protocol.request(options, function (res) {
if (!callback) {
return;
}
-
+
if (res.statusCode < 200 || res.statusCode >= 300) {
var err = new Error('HTTP error ' + res.statusCode);
err.arguments = arguments;
@@ -98,34 +92,34 @@ function parseRequest(method, path, data, callback, contentType) {
err.body = body;
return callback(err);
}
-
+
var json = '';
res.setEncoding('utf8');
-
+
res.on('data', function (chunk) {
json += chunk;
});
-
+
res.on('end', function () {
var err = null;
var data = null;
try {
var data = JSON.parse(json);
} catch (err) {
+ // pass to callback
}
callback(err, data);
});
-
+
res.on('close', function (err) {
callback(err);
});
});
-
- if(contentType)
- {
- body && req.write(body,'binary');
- }else{
- body && req.write(body);
+
+ if (contentType) {
+ body && req.write(body,'binary');
+ } else {
+ body && req.write(body);
}
req.end();
View
@@ -49,7 +49,7 @@ exports.update = function (assert) {
var num = Math.floor(Math.random() * 10000);
} while (num == object.foo);
object.foo = num;
-
+
parse.update(className, stub.objectId, object, function (err, response) {
err && console.log(err);
assert.ok(response);
@@ -67,3 +67,19 @@ exports['delete'] = function (assert) {
});
});
};
+
+var fileName = 'NodeParseApiFile';
+var fileData = 'Hello world!';
+var fileContentType = 'text/plain';
+var fileStub;
+
+exports.insertFile = function (assert) {
+ parse.insertFile(fileName, fileData, fileContentType, function (err, response) {
+ err && console.log(err);
+ assert.ok(response);
+ assert.equals(0, response.url.indexOf('http'));
+ assert.ok(response.url.indexOf(fileName));
+ assert.ok(response.name.indexOf(fileName));
+ assert.done();
+ });
+};

0 comments on commit a660942

Please sign in to comment.