Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'content-length'

  • Loading branch information...
commit a8e16a5260ea24a0509de6005486d20ce45f01b2 2 parents 3405238 + 21767d6
@mbostock mbostock authored
View
2  lib/cube/index.js
@@ -1,4 +1,4 @@
-exports.version = "0.0.1";
+exports.version = "0.0.2";
exports.emitter = require("./server/emitter");
exports.server = require("./server/server");
exports.collector = require("./server/collector");
View
20 lib/cube/server/endpoint.js
@@ -48,19 +48,23 @@ function file() {
});
function respond() {
- var status = modified <= new Date(request.headers["if-modified-since"]) ? 304 : 200;
+ var status = modified <= new Date(request.headers["if-modified-since"]) ? 304 : 200,
+ hasBody = status === 200 && request.method !== "HEAD";
- response.writeHead(status, {
+ var headers = {
"Content-Type": type + ";charset=utf-8",
- "Content-Length": size,
+ "Date": new Date().toUTCString(),
"Last-Modified": modified.toUTCString()
- });
+ };
- if ((status === 200) && (request.method !== "HEAD")) {
- return read(0);
+ if (hasBody) {
+ headers["Content-Length"] = size;
+ response.writeHead(status, headers);
+ read(0);
+ } else {
+ response.writeHead(status, headers);
+ response.end();
}
-
- return response.end();
}
function read(i) {
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "cube",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "A system for time series visualization using MongoDB, Node and D3.",
"keywords": ["time series", "visualization"],
"homepage": "http://square.github.com/cube/",
View
83 test/endpoint-test.js
@@ -0,0 +1,83 @@
+var vows = require("vows"),
+ assert = require("assert"),
+ endpoint = require("../lib/cube/server/endpoint");
+
+var suite = vows.describe("endpoint");
+
+suite.addBatch({
+ "file": {
+ "GET": {
+ topic: testFile("GET", {}),
+ "the status should be 200": function(response) {
+ assert.equal(response.status, 200);
+ },
+ "the expected headers should be set": function(response) {
+ assert.equal(response.headers["Content-Type"], "text/javascript;charset=utf-8");
+ assert.equal(response.headers["Content-Length"], 2);
+ assert.ok(new Date(response.headers["Date"]) > Date.UTC(2011, 0, 1));
+ assert.ok(new Date(response.headers["Last-Modified"]) > Date.UTC(2011, 0, 1));
+ },
+ "the expected content should be returned": function(response) {
+ assert.equal(response.body, ";;");
+ }
+ },
+ "GET If-Modified-Since": {
+ topic: testFile("GET", {"if-modified-since": new Date(2101, 0, 1).toUTCString()}),
+ "the status should be 304": function(response) {
+ assert.equal(response.status, 304);
+ },
+ "the expected headers should be set": function(response) {
+ assert.equal(response.headers["Content-Type"], "text/javascript;charset=utf-8");
+ assert.ok(!("Content-Length" in response.headers));
+ assert.ok(new Date(response.headers["Date"]) > Date.UTC(2011, 0, 1));
+ assert.ok(new Date(response.headers["Last-Modified"]) > Date.UTC(2011, 0, 1));
+ },
+ "no content should be returned": function(response) {
+ assert.equal(response.body, "");
+ }
+ },
+ "HEAD": {
+ topic: testFile("HEAD", {"if-modified-since": new Date(2001, 0, 1).toUTCString()}),
+ "the status should be 200": function(response) {
+ assert.equal(response.status, 200);
+ },
+ "the expected headers should be set": function(response) {
+ assert.equal(response.headers["Content-Type"], "text/javascript;charset=utf-8");
+ assert.ok(!("Content-Length" in response.headers));
+ assert.ok(new Date(response.headers["Date"]) > Date.UTC(2011, 0, 1));
+ assert.ok(new Date(response.headers["Last-Modified"]) > Date.UTC(2011, 0, 1));
+ },
+ "no content should be returned": function(response) {
+ assert.equal(response.body, "");
+ }
+ }
+ }
+});
+
+function testFile(method, headers) {
+ return function() {
+ var file = endpoint.file("../client/semicolon.js", "../client/semicolon.js"),
+ request = {headers: headers, method: method},
+ response = {writeHead: head, write: write, end: end},
+ status,
+ body = "",
+ cb = this.callback;
+
+ function head(s, h) {
+ status = s;
+ headers = h;
+ }
+
+ function write(data) {
+ body += data;
+ }
+
+ function end() {
+ cb(null, {status: status, headers: headers, body: body});
+ }
+
+ file(request, response);
+ };
+}
+
+suite.export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.