Skip to content

Commit

Permalink
migrated to node v0.1.30-23-g2b91f8d
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjjwright authored and danwrong committed Mar 1, 2010
1 parent 0f02443 commit ad3ff85
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/multipartform.js
Expand Up @@ -96,11 +96,11 @@ Request.prototype = {
var sentCount = 0, self = this;
this.eachPart(function(part) {
part.encode(function(encoded) {
request.sendBody(self.boundary + '\r\n' + encoded, 'binary');
request.write(self.boundary + '\r\n' + encoded, 'binary');
sentCount++;

if (sentCount == self.fieldCount) {
request.sendBody(self.boundary + '--', self.encoding, 'binary');
request.write(self.boundary + '--', self.encoding, 'binary');
callback.call(self);
}
});
Expand Down
37 changes: 17 additions & 20 deletions lib/restler.js
Expand Up @@ -38,7 +38,6 @@ function Request(url, options) {

if (this.options.multipart) this._createMultipartRequest();
else this._createRequest();

}

Request.prototype = new process.EventEmitter();
Expand Down Expand Up @@ -88,19 +87,18 @@ process.mixin(Request.prototype, {
} else {
var body = '';

response.addListener('body', function(chunk) {
body += chunk;
response.addListener('data', function(chunk) {
body += chunk;
});

response.addListener('complete', function() {
if (self.options.parser) body = self.options.parser.call(response, body);
response.addListener('end', function() {
if (self.options.parser) body = self.options.parser.call(response, body);

if (parseInt(response.statusCode) >= 400) self._respond('error', body, response);
else self._respond('success', body, response);

self._respond(response.statusCode.toString().replace(/\d{2}$/, 'XX'), body, response);
self._respond(response.statusCode.toString(), body, response);

self._respond('complete', body, response);
});
}
Expand All @@ -110,14 +108,20 @@ process.mixin(Request.prototype, {
else this.emit(type, data, response);
},
_makeRequest: function(method) {
var self = this;
// Support new and old interface for making requests for now
var request;
if (typeof this.client.request == 'function') {
return this.client.request(method, this._fullPath(), this.headers);
request = this.client.request(method, this._fullPath(), this.headers);
} else {
method = method.toLowerCase();
if (method == 'delete') method = 'del';
return this.client[method](this._fullPath(), this.headers);
request = this.client[method](this._fullPath(), this.headers);
}
request.addListener("response", function(response) {
self._responseHandler(response);
});
return request;
},
_createRequest: function() {
if (typeof this.options.data == 'object') {
Expand All @@ -127,8 +131,7 @@ process.mixin(Request.prototype, {
}

this.request = this._makeRequest(this.options.method);

if (this.options.data) this.request.sendBody(this.options.data.toString(), this.options.encoding || 'utf8');
if (this.options.data) this.request.write(this.options.data.toString(), this.options.encoding || 'utf8');
},
_createMultipartRequest: function() {
this.headers['Content-Type'] = 'multipart/form-data; boundary=' + multipart.defaultBoundary;
Expand All @@ -138,19 +141,13 @@ process.mixin(Request.prototype, {
},
run: function() {
var self = this;

if (this.options.multipart) {
multipart.send(this.request, this.options.data, function() {
self.request.finish(function(response) {
self._responseHandler(response);
});
self.request.close();
});
} else {
this.request.finish(function(response) {
self._responseHandler(response);
});
this.request.close();
}

return this;
}
});
Expand All @@ -163,11 +160,11 @@ function shortcutOptions(options, method) {
}

function request(url, options) {
return (new Request(url, options)).run();
return (new Request(url, options)).run();
}

function get(url, options) {
return request(url, shortcutOptions(options, 'GET'));
return request(url, shortcutOptions(options, 'GET'));;
}

function post(url, options) {
Expand Down
26 changes: 13 additions & 13 deletions test/test_helper.js
Expand Up @@ -10,10 +10,10 @@ exports.echoServer = function() {
echo += header + ": " + request.headers[header] + "\r\n";
}
echo += '\r\n';
request.addListener('body', function(chunk) {
request.addListener('data', function(chunk) {
echo += chunk;
});
request.addListener('complete', function() {
request.addListener('end', function() {

var requestedCode = request.headers['x-give-me-status'];

Expand All @@ -22,8 +22,8 @@ exports.echoServer = function() {
'Content-Length': echo.length
});

response.sendBody(echo);
response.finish();
response.write(echo);
response.close();
server.close();
});
});
Expand All @@ -42,18 +42,18 @@ exports.dataServer = function() {
response.sendHeader(200, { 'Content-Type': request.headers['accepts'] });

if (request.headers['accepts'] == 'application/json') {
response.sendBody(json);
response.write(json);
}

if (request.headers['accepts'] == 'application/xml') {
response.sendBody(xml);
response.write(xml);
}

if (request.headers['accepts'] == 'application/yaml') {
response.sendBody(yaml);
response.write(yaml);
}

response.finish();
response.close();
server.close();
});

Expand All @@ -68,13 +68,13 @@ exports.redirectServer = function() {
var server = http.createServer(function(request, response) {
if (request.url == '/redirected') {
response.sendHeader(200, { 'Content-Type': 'text/plain' });
response.sendBody('Hell Yeah!');
response.finish();
response.write('Hell Yeah!');
response.close();
server.close();
} else {
response.sendHeader(301, { 'Location': 'http://localhost:' + port + '/redirected' });
response.sendBody('Redirecting...');
response.finish();
response.write('Redirecting...');
response.close();
}

});
Expand Down Expand Up @@ -136,7 +136,7 @@ exports.testCase = function(caseName, serverFunc, tests) {

process.addListener('exit', function() {
var passFail = (testCount == passes) ? ' \033[0;32mGOOD!\033[1;37m' : ' \033[0;31mBAD!\033[1;37m';
sys.puts(caseName + " - Assertions: " + testCount + " Passed: " + passes + " Failed: " + fails + passFail);
sys.puts(caseName + " - Assertions: " + testCount + " Passed: " + passes + " Failed: " + fails);
});
}

0 comments on commit ad3ff85

Please sign in to comment.