Skip to content

Commit

Permalink
Changed to use req.end and res.end instead of req.close and res.close…
Browse files Browse the repository at this point in the history
…; fixed bug with error handling for attempt to read a directory in serve_static_file.
  • Loading branch information
gjritter committed May 4, 2010
1 parent c94791a commit 9e7151c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
20 changes: 15 additions & 5 deletions lib/nerve.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}
this.writeHeader(response_data.status_code || 200, headers);
this.write(response_data.content || response_data, 'binary');
this.close();
this.end();
};

function match_request(matcher, req) {
Expand Down Expand Up @@ -88,12 +88,22 @@
function serve_static_file(pathname, res) {
path.exists(pathname, function (exists) {
if (exists) {
fs.readFile(pathname, 'binary', function (err, data) {
if(err) {
fs.stat(pathname, function (err, stats) {
if (err) {
res.respond({content: '<html><head><title>Exception</title></head><body><h1>Exception</h1><pre>' + sys.inspect(err) + '</pre></body></html>', status_code: 501});
sys.p(err);
} else {
res.respond({content: data, headers: {'Content-Type': mime.mime_type(pathname)}});
if (stats.isFile()) {
fs.readFile(pathname, 'binary', function (err, data) {
if(err) {
res.respond({content: '<html><head><title>Exception</title></head><body><h1>Exception</h1><pre>' + sys.inspect(err) + '</pre></body></html>', status_code: 501});
sys.p(err);
} else {
res.respond({content: data, headers: {'Content-Type': mime.mime_type(pathname)}});
}
});
} else {
res.respond({content: '<html><head><title>Error</title></head><body><h1>Not a file</h1></body></html>', status_code: 501});
}
}
});
} else {
Expand Down
56 changes: 28 additions & 28 deletions test/test_nerve.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'Hello, World!');
});
req.close();
req.end();
}());

(function test_unmatched_request_string() {
Expand All @@ -88,7 +88,7 @@
req.addListener( 'response', function (res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_get_to_get_matcher() {
Expand All @@ -100,7 +100,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'GET matcher');
});
req.close();
req.end();
}());

(function test_post_to_get_matcher() {
Expand All @@ -110,7 +110,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_put_to_get_matcher() {
Expand All @@ -120,7 +120,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_delete_to_get_matcher() {
Expand All @@ -130,7 +130,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_get_to_get_string_matcher() {
Expand All @@ -142,7 +142,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'GET string matcher');
});
req.close();
req.end();
}());

(function test_post_to_get_string_matcher() {
Expand All @@ -152,7 +152,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_put_to_get_string_matcher() {
Expand All @@ -162,7 +162,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_delete_to_get_string_matcher() {
Expand All @@ -172,7 +172,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_nonmatching_get_to_get_string_matcher() {
Expand All @@ -182,7 +182,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_post_to_post_string_matcher() {
Expand All @@ -194,7 +194,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'POST string matcher');
});
req.close();
req.end();
}());

(function test_get_to_post_string_matcher() {
Expand All @@ -204,7 +204,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_put_to_post_string_matcher() {
Expand All @@ -214,7 +214,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_delete_to_post_string_matcher() {
Expand All @@ -224,7 +224,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_nonmatching_post_to_post_string_matcher() {
Expand All @@ -234,7 +234,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_put_to_put_string_matcher() {
Expand All @@ -246,7 +246,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'PUT string matcher');
});
req.close();
req.end();
}());

(function test_get_to_put_string_matcher() {
Expand All @@ -256,7 +256,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_post_to_put_string_matcher() {
Expand All @@ -266,7 +266,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_delete_to_put_string_matcher() {
Expand All @@ -276,7 +276,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_nonmatching_put_to_put_string_matcher() {
Expand All @@ -286,7 +286,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_del_to_del_string_matcher() {
Expand All @@ -298,7 +298,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'DEL string matcher');
});
req.close();
req.end();
}());

(function test_get_to_del_string_matcher() {
Expand All @@ -308,7 +308,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_post_to_del_string_matcher() {
Expand All @@ -318,7 +318,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_put_to_del_string_matcher() {
Expand All @@ -328,7 +328,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_nonmatching_del_to_del_string_matcher() {
Expand All @@ -338,7 +338,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
req.close();
req.end();
}());

(function test_static_file() {
Expand All @@ -350,7 +350,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'hello world\n');
});
req.close();
req.end();
}());

(function test_read_dir() {
Expand All @@ -361,7 +361,7 @@
test.assertEquals(501, res.statusCode);
receive_callback();
});
req.close();
req.end();
}());

// assert that all callbacks were called within the alloted time and exit
Expand Down

1 comment on commit 9e7151c

@gjritter
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks FSX for pointing out the change from close -> end!

Please sign in to comment.