Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convert all tests to Jasmine.

Also add coverage testing.
  • Loading branch information...
commit d361d09fff3253f41490afa3db9c1e89bb30d633 1 parent 501ccec
@kriskowal authored
View
4 .gitignore
@@ -1,2 +1,4 @@
node_modules
-.idea
+.idea
+.coverage_data
+cover_html
View
6 http.js
@@ -281,7 +281,7 @@ exports.request = function (request) {
"method": request.method || "GET",
"headers": headers
}, function (_response) {
- deferred.resolve(exports.ClientResponse(_response));
+ deferred.resolve(exports.ClientResponse(_response, request.charset));
_response.on("error", function (error) {
// TODO find a better way to channel
// this into the response
@@ -346,7 +346,7 @@ exports.read = function (request, qualifier) {
* by the Q HTTP Client API, suitable for use by the
* Q HTTP Server API.
*/
-exports.ClientResponse = function (_response) {
+exports.ClientResponse = function (_response, charset) {
var response = Object.create(exports.ClientResponse.prototype);
/*** {Number} HTTP status code */
response.status = _response.statusCode;
@@ -360,7 +360,7 @@ exports.ClientResponse = function (_response) {
response.node = _response;
response.nodeResponse = _response; // Deprecated
response.nodeConnection = _response.connection; // Deprecated
- return Q.when(Reader(_response), function (body) {
+ return Q.when(Reader(_response, charset), function (body) {
response.body = body;
return response;
});
View
10 package.json
@@ -27,10 +27,16 @@
"collections": "0.1.x"
},
"devDependencies": {
- "test": "*"
+ "jshint": ">=0.9.1",
+ "cover": "*",
+ "jasmine-node": "*",
+ "opener": "*"
},
"scripts": {
- "test": "node test/all.js"
+ "test": "jasmine-node spec",
+ "test-browser": "opener spec/q-spec.html",
+ "lint": "jshint q.js",
+ "cover": "cover run node_modules/jasmine-node/bin/jasmine-node spec && cover report html && opener cover_html/index.html"
},
"engines": {
"node": ">=0.6.0"
View
52 spec/http-apps/cookie-spec.js
@@ -0,0 +1,52 @@
+
+require("../lib/jasmine-promise");
+var Q = require("q");
+var Http = require("../../http");
+var Apps = require("../../http-apps");
+
+describe("http cookies", function () {
+
+ var hosts = ["localhost", "127.0.0.1"];
+
+ hosts.forEach(function (host) {
+ it("should work on host" + host, function () {
+
+ var server = Http.Server(function (request) {
+ return {
+ status: 200,
+ headers: {
+ "set-cookie": "a=10; MaxAge=1"
+ },
+ body: [request.headers.cookie || ""]
+ };
+ });
+
+ var request = Apps.Normalize(Apps.CookieJar(Http.request));
+
+ return server.listen(0)
+ .then(function (server) {
+ var address = server.node.address();
+ return request("http://" + host + ":" + address.port)
+ .get("body")
+ .invoke("read")
+ .invoke("toString", "utf-8")
+ .then(function (content) {
+ expect(content).toEqual(""); // no cookie first time
+ })
+ .then(function () {
+ return request("http://" + host + ":" + address.port)
+ .get("body")
+ .invoke("read")
+ .invoke("toString", "utf-8")
+ })
+ .then(function (content) {
+ expect(content).toEqual("a=10"); // cookie set second time
+ })
+ })
+ .timeout(1000)
+ .finally(server.stop)
+ });
+ });
+
+});
+
View
0  test/http-apps/fixtures/1234.txt → spec/http-apps/fixtures/1234.txt
File renamed without changes
View
14 test/http-apps/interpret-range.js → spec/http-apps/interpret-range-spec.js
@@ -1,7 +1,6 @@
// http://labs.apache.org/webarch/http/draft-fielding-http/p5-range.html#range.units
-var assert = require("assert");
var Apps = require("../../http-apps");
var size = 10000;
@@ -43,12 +42,11 @@ var tests = [
}
];
-tests.forEach(function (test) {
- exports['test ' + test.input] = function () {
- assert.deepEqual(Apps.interpretFirstRange(test.input, size), test.oracle, test.description);
- };
+describe("range interpretation", function () {
+ tests.forEach(function (test) {
+ it("should interpret " + test.input, function () {
+ expect(Apps.interpretFirstRange(test.input, size)).toEqual(test.oracle);
+ });
+ });
});
-if (require.main === module)
- require("test").run(exports);
-
View
32 spec/http-apps/partial-range-spec.js
@@ -0,0 +1,32 @@
+
+require("../lib/jasmine-promise");
+var Q = require("q");
+var Http = require("../../http");
+var Apps = require("../../http-apps");
+var FS = require("../../fs");
+
+describe("http client and server apps", function () {
+
+ it("should read a partial range", function () {
+ var fixture = FS.join(module.directory || __dirname, "fixtures", "1234.txt");
+ return Http.Server(Apps.Cap(Apps.File(fixture)))
+ .listen(0)
+ .then(function (server) {
+ var port = server.node.address().port;
+ return Http.read({
+ "url": "http://127.0.0.1:" + port + "/",
+ "headers": {
+ "range": "bytes=1-2"
+ }
+ }, function (response) {
+ return response.status === 206;
+ })
+ .then(function (content) {
+ expect(content.toString('utf-8')).toEqual('23');
+ })
+ .finally(server.stop)
+ })
+ });
+
+});
+
View
77 spec/http-apps/proxy-spec.js
@@ -0,0 +1,77 @@
+
+require("../lib/jasmine-promise");
+var Q = require("q");
+var Http = require("../../http");
+var Apps = require("../../http-apps");
+var FS = require("../../fs");
+
+describe("http proxy", function () {
+
+ it("should work", function () {
+
+ var requestProxy;
+ var responseProxy;
+ var requestActual;
+ var responseActual;
+
+ var server1 = Http.Server(
+ Apps.Trap(
+ Apps.Tap(
+ Apps.Branch({
+ "foo": Apps.Branch({
+ "bar": Apps.Cap(Apps.Content(["Hello, World!"]))
+ })
+ }),
+ function (request) {
+ requestActual = request;
+ }
+ ),
+ function (response) {
+ responseActual = response;
+ return response;
+ }
+ )
+ );
+
+ return Q.when(server1.listen(0))
+ .then(function (server1) {
+ var port = server1.node.address().port;
+
+ var server2 = Http.Server(
+ Apps.Trap(
+ Apps.Tap(
+ Apps.ProxyTree("http://127.0.0.1:" + port + "/foo/"),
+ function (request) {
+ requestProxy = request;
+ }
+ ),
+ function (response) {
+ responseProxy = response;
+ return response;
+ }
+ )
+ );
+
+ return [server1, server2.listen(0)];
+ })
+ .spread(function (server1, server2) {
+ var port = server2.node.address().port;
+ return Http.read({
+ url: "http://127.0.0.1:" + port + "/bar",
+ charset: "utf-8"
+ })
+ .then(function (content) {
+ expect(content).toBe("Hello, World!");
+ expect(requestActual).toBeTruthy();
+ expect(responseActual).toBeTruthy();
+ expect(requestProxy).toBeTruthy();
+ expect(responseProxy).toBeTruthy();
+ })
+ .finally(server1.stop)
+ .finally(server2.stop)
+ })
+
+ });
+
+});
+
View
96 spec/http/basic-spec.js
@@ -0,0 +1,96 @@
+
+require("../lib/jasmine-promise");
+var Q = require("q");
+var HTTP = require("../../http");
+
+describe("http server and client", function () {
+
+ it("should work as both server and client", function () {
+ var response = {
+ "status": 200,
+ "headers": {
+ "content-type": "text/plain"
+ },
+ "body": [
+ "Hello, World!"
+ ]
+ };
+
+ var server = HTTP.Server(function () {
+ return response;
+ });
+
+ return server.listen(0)
+ .then(function (server) {
+ var port = server.node.address().port;
+
+ var request = {
+ "host": "localhost",
+ "port": port,
+ "headers": {
+ "host": "localhost"
+ }
+ };
+
+ return HTTP.request(request)
+ .then(function (response) {
+ expect(Q.isPromise(response.body)).toBe(false);
+ var acc = [];
+ return response.body.read()
+ .then(function (body) {
+ expect(body.toString("utf-8")).toBe("Hello, World!");
+ });
+ })
+ })
+ .finally(server.stop)
+ });
+
+ it("should defer a response", function () {
+ var response = {
+ "status": 200,
+ "headers": {
+ "content-type": "text/plain; charset=utf-8"
+ },
+ "body": {
+ "forEach": function (write) {
+ var deferred = Q.defer();
+ write("Hello, World!");
+ setTimeout(function () {
+ deferred.resolve();
+ }, 100);
+ return deferred.promise;
+ }
+ }
+ };
+
+ var server = HTTP.Server(function () {
+ return response;
+ });
+
+ return server.listen(0).then(function (server) {
+ var port = server.node.address().port;
+
+ var request = {
+ "host": "localhost",
+ "port": port,
+ "headers": {
+ "host": "localhost"
+ },
+ "charset": "utf-8"
+ };
+
+ return HTTP.request(request)
+ .then(function (response) {
+ var acc = [];
+ return response.body.read()
+ .then(function (body) {
+ expect(body).toBe("Hello, World!");
+ });
+ })
+ })
+ .finally(server.stop)
+ });
+
+
+});
+
View
8 test/all.js
@@ -1,8 +0,0 @@
-
-exports["test http"] = require("./http/all");
-exports["test http apps"] = require("./http-apps/all");
-
-if (require.main === module) {
- require("test").run(exports);
-}
-
View
9 test/http-apps/all.js
@@ -1,9 +0,0 @@
-
-exports['test cookie'] = require("./cookie");
-exports['test interpret range'] = require("./interpret-range");
-exports['test partial range'] = require("./partial-range");
-exports['test proxy'] = require("./proxy");
-
-if (require.main = module)
- require("test").run(exports);
-
View
52 test/http-apps/cookie.js
@@ -1,52 +0,0 @@
-
-var Http = require("../../http");
-var Apps = require("../../http-apps");
-
-["localhost", "127.0.0.1"].forEach(function (host) {
-
- exports["test cookie " + host] = function (assert, done) {
-
- var server = Http.Server(function (request) {
- return {
- status: 200,
- headers: {
- "set-cookie": "a=10; MaxAge=1"
- },
- body: [request.headers.cookie || ""]
- };
- });
-
- var request = Apps.Normalize(Apps.CookieJar(Http.request));
-
- server.listen(0)
- .then(function (server) {
- var address = server.node.address();
- return request("http://" + host + ":" + address.port)
- .get("body")
- .invoke("read")
- .invoke("toString", "utf-8")
- .then(function (content) {
- assert.equal(content, "", "no cookie first time");
- return request("http://" + host + ":" + address.port)
- .get("body")
- .invoke("read")
- .invoke("toString", "utf-8")
- })
- .then(function (content) {
- assert.equal(content, "a=10", "cookie set second time");
- })
- })
- .timeout(1000)
- .finally(server.stop)
- .fail(function (reason) {
- assert.ok(false, reason);
- })
- .finally(done)
-
- };
-
-});
-
-if (require.main === module)
- require("test").run(exports);
-
View
35 test/http-apps/partial-range.js
@@ -1,35 +0,0 @@
-
-var Http = require("../../http");
-var FS = require("../../fs");
-var Apps = require("../../http-apps");
-
-var fixture = FS.join(module.directory || __dirname, "fixtures", "1234.txt");
-
-exports['test partial range request'] = function (assert, done) {
- Http.Server(Apps.Cap(Apps.File(fixture)))
- .listen(0)
- .then(function (server) {
- var port = server.node.address().port;
- return Http.read({
- "url": "http://127.0.0.1:" + port + "/",
- "headers": {
- "range": "bytes=1-2"
- }
- }, function (response) {
- return response.status === 206;
- })
- .then(function (content) {
- assert.equal(content.toString('utf-8'), '23', '1234[1-2] = 23');
- }, function (error) {
- console.log(error);
- throw error;
- })
- .fin(server.stop)
- })
- .finally(done)
- .done();
-};
-
-if (require.main === module)
- require("test").run(exports);
-
View
75 test/http-apps/proxy.js
@@ -1,75 +0,0 @@
-
-var Q = require("q");
-var Http = require("../../http");
-var Apps = require("../../http-apps");
-
-exports['test proxy'] = function (assert, done) {
-
- var requestProxy;
- var responseProxy;
- var requestActual;
- var responseActual;
-
- var server1 = Http.Server(
- Apps.Trap(
- Apps.Tap(
- Apps.Branch({
- "foo": Apps.Branch({
- "bar": Apps.Cap(Apps.Content(["Hello, World!"]))
- })
- }),
- function (request) {
- requestActual = request;
- }
- ),
- function (response) {
- responseActual = response;
- return response;
- }
- )
- );
-
- Q.when(server1.listen(0))
- .then(function (server1) {
- var port = server1.node.address().port;
-
- var server2 = Http.Server(
- Apps.Trap(
- Apps.Tap(
- Apps.ProxyTree("http://127.0.0.1:" + port + "/foo/"),
- function (request) {
- requestProxy = request;
- }
- ),
- function (response) {
- responseProxy = response;
- return response;
- }
- )
- );
-
- return [server1, server2.listen(0)];
- })
- .spread(function (server1, server2) {
- var port = server2.node.address().port;
- return Http.read("http://127.0.0.1:" + port + "/bar")
- .then(function (content) {
- assert.equal(content, "Hello, World!", "content");
- assert.ok(requestActual, "request actual");
- assert.ok(responseActual, "response actual");
- assert.ok(requestProxy, "request proxy");
- assert.ok(responseProxy, "response proxy");
- })
- .finally(server1.stop)
- .finally(server2.stop)
- })
- .fail(function (reason) {
- assert.ok(false, reason);
- })
- .finally(done)
-
-};
-
-if (require.main === module)
- require("test").run(exports);
-
View
7 test/http/all.js
@@ -1,7 +0,0 @@
-
-exports.testBasic = require("./basic");
-
-if (module === require.main) {
- require("test").run(exports);
-}
-
View
106 test/http/basic.js
@@ -1,106 +0,0 @@
-
-var Q = require("q");
-var HTTP = require("../../http");
-
-exports['test basic'] = function (ASSERT, done) {
-
- var response = {
- "status": 200,
- "headers": {
- "content-type": "text/plain"
- },
- "body": [
- "Hello, World!"
- ]
- };
-
- var server = HTTP.Server(function () {
- return response;
- });
-
- Q.when(server.listen(0), function (server) {
- var port = server.node.address().port;
-
- var request = {
- "host": "localhost",
- "port": port,
- "headers": {
- "host": "localhost"
- }
- };
-
- return Q.when(HTTP.request(request))
- .then(function (response) {
- ASSERT.ok(!Q.isPromise(response.body), "body is not a promise")
- var acc = [];
- return response.body.forEach(function (chunk) {
- acc.push(chunk.toString("utf-8"));
- }).then(function () {
- ASSERT.equal(acc.join(""), "Hello, World!", "body is hello world");
- });
- })
- })
- .fin(server.stop)
- .fin(done)
- .fail(function (reason) {
- ASSERT.ok(false, reason);
- })
-
-}
-
-exports['test deferred'] = function (ASSERT, done) {
-
- var response = {
- "status": 200,
- "headers": {
- "content-type": "text/plain; charset=utf-8"
- },
- "body": {
- "forEach": function (write) {
- var deferred = Q.defer();
- write("Hello, World!");
- setTimeout(function () {
- deferred.resolve();
- }, 100);
- return deferred.promise;
- }
- }
- };
-
- var server = HTTP.Server(function () {
- return response;
- });
-
- Q.when(server.listen(0), function (server) {
- var port = server.node.address().port;
-
- var request = {
- "host": "localhost",
- "port": port,
- "headers": {
- "host": "localhost"
- }
- };
-
- return Q.when(HTTP.request(request))
- .then(function (response) {
- var acc = [];
- return response.body.forEach(function (chunk) {
- acc.push(chunk.toString("utf-8"));
- }).then(function () {
- ASSERT.equal(acc.join(""), "Hello, World!", "body is hello world");
- });
- })
- })
- .fin(server.stop)
- .fin(done)
- .fail(function (reason) {
- ASSERT.ok(false, reason);
- })
-
-}
-
-if (module === require.main) {
- require("test").run(exports);
-}
-
View
46 test/http/deferred.js
@@ -1,46 +0,0 @@
-
-var Q = require("q");
-var HTTP = require("../../http");
-
-var response = {
- "status": 200,
- "headers": {
- "content-type": "text/plain; charset=utf-8"
- },
- "body": {
- "forEach": function (write) {
- var deferred = Q.defer();
- write("Hello, World!");
- setTimeout(function () {
- deferred.resolve();
- }, 1000);
- return deferred.promise;
- }
- }
-};
-
-var server = HTTP.Server(function () {
- return response;
-});
-
-Q.done(server.listen(0), function () {
- var port = server.node.address().port;
-
- var request = {
- "host": "localhost",
- "port": port,
- "headers": {
- "host": "localhost"
- }
- };
-
- return Q.when(HTTP.request(request), function (response) {
- return Q.when(response.body, function (body) {
- var done = body.forEach(function (chunk) {
- console.log(chunk.toString("utf-8"));
- });
- Q.when(done, server.stop);
- });
- });
-});
-
View
41 test/http/keep-alive.js
@@ -1,41 +0,0 @@
-
-var Q = require("q");
-var HTTP = require("../../http");
-
-var request = {
- "host": "localhost",
- "port": 8080,
- "headers": {
- "host": "localhost"
- }
-};
-
-var response = {
- "status": 200,
- "headers": {
- "content-type": "text/plain"
- },
- "body": [
- "Hello, World!"
- ]
-};
-
-var server = HTTP.Server(function () {
- return response;
-});
-
-Q.done(server.listen(8080), function () {
-
- var done = [1,2,3].reduce(function (done) {
- return Q.when(HTTP.request(request), function (response) {
- return Q.when(response.body, function (body) {
- return Q.all([body.forEach(function (chunk) {
- console.log(chunk.toString('utf-8'));
- }), done]);
- });
- });
- }, undefined);
-
- return Q.when(done, server.stop);
-});
-
Please sign in to comment.
Something went wrong with that request. Please try again.