Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Defer the request until the server listen callback fires
  • Loading branch information
rauchg committed Dec 28, 2010
1 parent 6dd1a52 commit 253ecf4
Showing 1 changed file with 62 additions and 54 deletions.
116 changes: 62 additions & 54 deletions lib/browser.js
Expand Up @@ -119,12 +119,6 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){
if (!server) throw new Error('no .server present');
++server.pending;

// HTTP client
if (!server.fd) {
server.listen(++port, host);
server.client = http.createClient(port);
}

// Save history
if (false !== saveHistory) this.history.push(path);

Expand All @@ -139,63 +133,77 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){

// Request
headers.Host = host;
var req = server.client.request(method, path, headers);
req.on('response', function(res){
var status = res.statusCode
, buf = '';

// Cookies
if (res.headers['set-cookie']) {
self.cookieJar.add(new Cookie(res.headers['set-cookie']));
}

// Success
if (status >= 200 && status < 300) {
var contentType = res.headers['content-type'];
// HTTP client
if (!server.fd) {
server.listen(++port, host, issue);
} else {
issue();
}

// JSON support
if (~contentType.indexOf('json')) {
res.body = '';
res.on('data', function(chunk){ res.body += chunk; });
res.on('end', function(){
try {
res.body = JSON.parse(res.body);
fn(res, res.body);
} catch (err) {
self.emit('error', err);
}
});
return;
}
function issue(){
if (!server.client) {
server.client = http.createClient(port);
}

// Ensure html
if (!~contentType.indexOf('text/html')) {
return fn(res);
var req = server.client.request(method, path, headers);
req.on('response', function(res){
var status = res.statusCode
, buf = '';

// Cookies
if (res.headers['set-cookie']) {
self.cookieJar.add(new Cookie(res.headers['set-cookie']));
}

// Buffer html
res.setEncoding('utf8');
res.on('data', function(chunk){ buf += chunk; });
res.on('end', function(){
self.parse(buf);
fn(res, function(selector){
return self.context.find(selector);
// Success
if (status >= 200 && status < 300) {
var contentType = res.headers['content-type'];

// JSON support
if (~contentType.indexOf('json')) {
res.body = '';
res.on('data', function(chunk){ res.body += chunk; });
res.on('end', function(){
try {
res.body = JSON.parse(res.body);
fn(res, res.body);
} catch (err) {
self.emit('error', err);
}
});
return;
}

// Ensure html
if (!~contentType.indexOf('text/html')) {
return fn(res);
}

// Buffer html
res.setEncoding('utf8');
res.on('data', function(chunk){ buf += chunk; });
res.on('end', function(){
self.parse(buf);
fn(res, function(selector){
return self.context.find(selector);
});
});
});

// Redirect
} else if (status >= 300 && status < 400) {
var location = res.headers.location;
self.emit('redirect', location);
self.request('GET', location, options, fn);
// Redirect
} else if (status >= 300 && status < 400) {
var location = res.headers.location;
self.emit('redirect', location);
self.request('GET', location, options, fn);

// Error
} else {
fn(res);
}
});
// Error
} else {
fn(res);
}
});

req.end(options.body);
req.end(options.body);
}

return this;
};
Expand Down

0 comments on commit 253ecf4

Please sign in to comment.