Skip to content

Commit

Permalink
all main tests now passing
Browse files Browse the repository at this point in the history
  • Loading branch information
danwrong committed Feb 2, 2011
1 parent 9a86de5 commit a3dbabd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
16 changes: 9 additions & 7 deletions lib/multipartform.js
Expand Up @@ -107,8 +107,10 @@ Part.prototype = {
//Now write out the body of the Part
if (this.value instanceof File) {
fs.open(this.value.path, "r", 0666, function (err, fd) {
if (err) throw err;
if (err) throw err;

position = 0;

(function reader () {
fs.read(fd, 1024 * 4, position, "binary", function (er, chunk) {
if (er) callback(err);
Expand Down Expand Up @@ -151,13 +153,13 @@ MultiPartRequest.prototype = {

// wrap the stream in our own Stream object
// See the Stream function above for the benefits of this
var _stream = new Stream(stream);
var stream = new Stream(stream);

// Let each part write itself out to the stream
(function writePart() {
partName = partNames[partCount];
partName = this.partNames[partCount];
part = new Part(partName, self.data[partName], self.boundary);
part.write(_stream, function (err) {
part.write(stream, function (err) {
if (err) {
callback(err);
return;
Expand All @@ -166,9 +168,9 @@ MultiPartRequest.prototype = {
if (partCount < self.partNames.length)
writePart();
else {
_stream.write('--' + self.boundary + '--' + "\r\n");
stream.write('--' + self.boundary + '--' + "\r\n");

if(callback) callback(_stream.string || "");
if (callback) callback(stream.string || "");
}
});
})();
Expand All @@ -188,7 +190,7 @@ var exportMethods = {
for (var name in parts) totalSize += new Part(name, parts[name], boundary).sizeOf();
return totalSize + boundary.length + 6;
},
write: function(stream, data, boundary, callback) {
write: function(stream, data, callback, boundary) {
var r = new MultiPartRequest(data, boundary);
r.write(stream, callback);
return r;
Expand Down
19 changes: 9 additions & 10 deletions lib/restler.js
Expand Up @@ -38,14 +38,14 @@ function Request(uri, options) {

this._applyBasicAuth();

if (typeof this.options.data == 'object') {
this.options.data = qs.stringify(this.options.data);
this.headers['Content-Type'] = 'application/x-www-form-urlencoded';
this.headers['Content-Length'] = this.options.data.length;
}

if (this.options.multipart) {
this.headers['Content-Type'] = 'multipart/form-data; boundary=' + multipart.defaultBoundary;
} else {
if (typeof this.options.data == 'object') {
this.options.data = qs.stringify(this.options.data);
this.headers['Content-Type'] = 'application/x-www-form-urlencoded';
this.headers['Content-Length'] = this.options.data.length;
}
}

var proto = (this.url.protocol == 'https:') ? https : http;
Expand Down Expand Up @@ -141,10 +141,6 @@ mixin(Request.prototype, {
this.request.addListener("response", function(response) {
self._responseHandler(response);
});

if (this.options.data) {
this.request.write(this.options.data.toString(), this.options.encoding || 'utf8');
}
},
run: function() {
var self = this;
Expand All @@ -154,6 +150,9 @@ mixin(Request.prototype, {
self.request.end();
});
} else {
if (this.options.data) {
this.request.write(this.options.data.toString(), this.options.encoding || 'utf8');
}
this.request.end();
}

Expand Down
3 changes: 2 additions & 1 deletion test/test_helper.js
Expand Up @@ -10,8 +10,9 @@ exports.echoServer = function() {
echo += header + ": " + request.headers[header] + "\r\n";
}
echo += '\r\n';

request.addListener('data', function(chunk) {
echo += chunk;
echo += chunk.toString('binary');
});
request.addListener('end', function() {

Expand Down

0 comments on commit a3dbabd

Please sign in to comment.