Skip to content
This repository
Browse code

Convert all tests to Jasmine.

Also add coverage testing.
  • Loading branch information...
commit d361d09fff3253f41490afa3db9c1e89bb30d633 1 parent 501ccec
Kris Kowal authored
4 .gitignore
... ... @@ -1,2 +1,4 @@
1 1 node_modules
2   -.idea
  2 +.idea
  3 +.coverage_data
  4 +cover_html
6 http.js
@@ -281,7 +281,7 @@ exports.request = function (request) {
281 281 "method": request.method || "GET",
282 282 "headers": headers
283 283 }, function (_response) {
284   - deferred.resolve(exports.ClientResponse(_response));
  284 + deferred.resolve(exports.ClientResponse(_response, request.charset));
285 285 _response.on("error", function (error) {
286 286 // TODO find a better way to channel
287 287 // this into the response
@@ -346,7 +346,7 @@ exports.read = function (request, qualifier) {
346 346 * by the Q HTTP Client API, suitable for use by the
347 347 * Q HTTP Server API.
348 348 */
349   -exports.ClientResponse = function (_response) {
  349 +exports.ClientResponse = function (_response, charset) {
350 350 var response = Object.create(exports.ClientResponse.prototype);
351 351 /*** {Number} HTTP status code */
352 352 response.status = _response.statusCode;
@@ -360,7 +360,7 @@ exports.ClientResponse = function (_response) {
360 360 response.node = _response;
361 361 response.nodeResponse = _response; // Deprecated
362 362 response.nodeConnection = _response.connection; // Deprecated
363   - return Q.when(Reader(_response), function (body) {
  363 + return Q.when(Reader(_response, charset), function (body) {
364 364 response.body = body;
365 365 return response;
366 366 });
10 package.json
@@ -27,10 +27,16 @@
27 27 "collections": "0.1.x"
28 28 },
29 29 "devDependencies": {
30   - "test": "*"
  30 + "jshint": ">=0.9.1",
  31 + "cover": "*",
  32 + "jasmine-node": "*",
  33 + "opener": "*"
31 34 },
32 35 "scripts": {
33   - "test": "node test/all.js"
  36 + "test": "jasmine-node spec",
  37 + "test-browser": "opener spec/q-spec.html",
  38 + "lint": "jshint q.js",
  39 + "cover": "cover run node_modules/jasmine-node/bin/jasmine-node spec && cover report html && opener cover_html/index.html"
34 40 },
35 41 "engines": {
36 42 "node": ">=0.6.0"
52 spec/http-apps/cookie-spec.js
... ... @@ -0,0 +1,52 @@
0  test/http-apps/fixtures/1234.txt → spec/http-apps/fixtures/1234.txt
File renamed without changes
14 test/http-apps/interpret-range.js → spec/http-apps/interpret-range-spec.js
... ... @@ -1,7 +1,6 @@
1 1
2 2 // http://labs.apache.org/webarch/http/draft-fielding-http/p5-range.html#range.units
3 3
4   -var assert = require("assert");
5 4 var Apps = require("../../http-apps");
6 5
7 6 var size = 10000;
@@ -43,12 +42,11 @@ var tests = [
43 42 }
44 43 ];
45 44
46   -tests.forEach(function (test) {
47   - exports['test ' + test.input] = function () {
48   - assert.deepEqual(Apps.interpretFirstRange(test.input, size), test.oracle, test.description);
49   - };
  45 +describe("range interpretation", function () {
  46 + tests.forEach(function (test) {
  47 + it("should interpret " + test.input, function () {
  48 + expect(Apps.interpretFirstRange(test.input, size)).toEqual(test.oracle);
  49 + });
  50 + });
50 51 });
51 52
52   -if (require.main === module)
53   - require("test").run(exports);
54   -
32 spec/http-apps/partial-range-spec.js
... ... @@ -0,0 +1,32 @@
  1 +
  2 +require("../lib/jasmine-promise");
  3 +var Q = require("q");
  4 +var Http = require("../../http");
  5 +var Apps = require("../../http-apps");
  6 +var FS = require("../../fs");
  7 +
  8 +describe("http client and server apps", function () {
  9 +
  10 + it("should read a partial range", function () {
  11 + var fixture = FS.join(module.directory || __dirname, "fixtures", "1234.txt");
  12 + return Http.Server(Apps.Cap(Apps.File(fixture)))
  13 + .listen(0)
  14 + .then(function (server) {
  15 + var port = server.node.address().port;
  16 + return Http.read({
  17 + "url": "http://127.0.0.1:" + port + "/",
  18 + "headers": {
  19 + "range": "bytes=1-2"
  20 + }
  21 + }, function (response) {
  22 + return response.status === 206;
  23 + })
  24 + .then(function (content) {
  25 + expect(content.toString('utf-8')).toEqual('23');
  26 + })
  27 + .finally(server.stop)
  28 + })
  29 + });
  30 +
  31 +});
  32 +
77 spec/http-apps/proxy-spec.js
... ... @@ -0,0 +1,77 @@
  1 +
  2 +require("../lib/jasmine-promise");
  3 +var Q = require("q");
  4 +var Http = require("../../http");
  5 +var Apps = require("../../http-apps");
  6 +var FS = require("../../fs");
  7 +
  8 +describe("http proxy", function () {
  9 +
  10 + it("should work", function () {
  11 +
  12 + var requestProxy;
  13 + var responseProxy;
  14 + var requestActual;
  15 + var responseActual;
  16 +
  17 + var server1 = Http.Server(
  18 + Apps.Trap(
  19 + Apps.Tap(
  20 + Apps.Branch({
  21 + "foo": Apps.Branch({
  22 + "bar": Apps.Cap(Apps.Content(["Hello, World!"]))
  23 + })
  24 + }),
  25 + function (request) {
  26 + requestActual = request;
  27 + }
  28 + ),
  29 + function (response) {
  30 + responseActual = response;
  31 + return response;
  32 + }
  33 + )
  34 + );
  35 +
  36 + return Q.when(server1.listen(0))
  37 + .then(function (server1) {
  38 + var port = server1.node.address().port;
  39 +
  40 + var server2 = Http.Server(
  41 + Apps.Trap(
  42 + Apps.Tap(
  43 + Apps.ProxyTree("http://127.0.0.1:" + port + "/foo/"),
  44 + function (request) {
  45 + requestProxy = request;
  46 + }
  47 + ),
  48 + function (response) {
  49 + responseProxy = response;
  50 + return response;
  51 + }
  52 + )
  53 + );
  54 +
  55 + return [server1, server2.listen(0)];
  56 + })
  57 + .spread(function (server1, server2) {
  58 + var port = server2.node.address().port;
  59 + return Http.read({
  60 + url: "http://127.0.0.1:" + port + "/bar",
  61 + charset: "utf-8"
  62 + })
  63 + .then(function (content) {
  64 + expect(content).toBe("Hello, World!");
  65 + expect(requestActual).toBeTruthy();
  66 + expect(responseActual).toBeTruthy();
  67 + expect(requestProxy).toBeTruthy();
  68 + expect(responseProxy).toBeTruthy();
  69 + })
  70 + .finally(server1.stop)
  71 + .finally(server2.stop)
  72 + })
  73 +
  74 + });
  75 +
  76 +});
  77 +
96 spec/http/basic-spec.js
... ... @@ -0,0 +1,96 @@
  1 +
  2 +require("../lib/jasmine-promise");
  3 +var Q = require("q");
  4 +var HTTP = require("../../http");
  5 +
  6 +describe("http server and client", function () {
  7 +
  8 + it("should work as both server and client", function () {
  9 + var response = {
  10 + "status": 200,
  11 + "headers": {
  12 + "content-type": "text/plain"
  13 + },
  14 + "body": [
  15 + "Hello, World!"
  16 + ]
  17 + };
  18 +
  19 + var server = HTTP.Server(function () {
  20 + return response;
  21 + });
  22 +
  23 + return server.listen(0)
  24 + .then(function (server) {
  25 + var port = server.node.address().port;
  26 +
  27 + var request = {
  28 + "host": "localhost",
  29 + "port": port,
  30 + "headers": {
  31 + "host": "localhost"
  32 + }
  33 + };
  34 +
  35 + return HTTP.request(request)
  36 + .then(function (response) {
  37 + expect(Q.isPromise(response.body)).toBe(false);
  38 + var acc = [];
  39 + return response.body.read()
  40 + .then(function (body) {
  41 + expect(body.toString("utf-8")).toBe("Hello, World!");
  42 + });
  43 + })
  44 + })
  45 + .finally(server.stop)
  46 + });
  47 +
  48 + it("should defer a response", function () {
  49 + var response = {
  50 + "status": 200,
  51 + "headers": {
  52 + "content-type": "text/plain; charset=utf-8"
  53 + },
  54 + "body": {
  55 + "forEach": function (write) {
  56 + var deferred = Q.defer();
  57 + write("Hello, World!");
  58 + setTimeout(function () {
  59 + deferred.resolve();
  60 + }, 100);
  61 + return deferred.promise;
  62 + }
  63 + }
  64 + };
  65 +
  66 + var server = HTTP.Server(function () {
  67 + return response;
  68 + });
  69 +
  70 + return server.listen(0).then(function (server) {
  71 + var port = server.node.address().port;
  72 +
  73 + var request = {
  74 + "host": "localhost",
  75 + "port": port,
  76 + "headers": {
  77 + "host": "localhost"
  78 + },
  79 + "charset": "utf-8"
  80 + };
  81 +
  82 + return HTTP.request(request)
  83 + .then(function (response) {
  84 + var acc = [];
  85 + return response.body.read()
  86 + .then(function (body) {
  87 + expect(body).toBe("Hello, World!");
  88 + });
  89 + })
  90 + })
  91 + .finally(server.stop)
  92 + });
  93 +
  94 +
  95 +});
  96 +
8 test/all.js
... ... @@ -1,8 +0,0 @@
1   -
2   -exports["test http"] = require("./http/all");
3   -exports["test http apps"] = require("./http-apps/all");
4   -
5   -if (require.main === module) {
6   - require("test").run(exports);
7   -}
8   -
9 test/http-apps/all.js
... ... @@ -1,9 +0,0 @@
1   -
2   -exports['test cookie'] = require("./cookie");
3   -exports['test interpret range'] = require("./interpret-range");
4   -exports['test partial range'] = require("./partial-range");
5   -exports['test proxy'] = require("./proxy");
6   -
7   -if (require.main = module)
8   - require("test").run(exports);
9   -
52 test/http-apps/cookie.js
... ... @@ -1,52 +0,0 @@
35 test/http-apps/partial-range.js
... ... @@ -1,35 +0,0 @@
1   -
2   -var Http = require("../../http");
3   -var FS = require("../../fs");
4   -var Apps = require("../../http-apps");
5   -
6   -var fixture = FS.join(module.directory || __dirname, "fixtures", "1234.txt");
7   -
8   -exports['test partial range request'] = function (assert, done) {
9   - Http.Server(Apps.Cap(Apps.File(fixture)))
10   - .listen(0)
11   - .then(function (server) {
12   - var port = server.node.address().port;
13   - return Http.read({
14   - "url": "http://127.0.0.1:" + port + "/",
15   - "headers": {
16   - "range": "bytes=1-2"
17   - }
18   - }, function (response) {
19   - return response.status === 206;
20   - })
21   - .then(function (content) {
22   - assert.equal(content.toString('utf-8'), '23', '1234[1-2] = 23');
23   - }, function (error) {
24   - console.log(error);
25   - throw error;
26   - })
27   - .fin(server.stop)
28   - })
29   - .finally(done)
30   - .done();
31   -};
32   -
33   -if (require.main === module)
34   - require("test").run(exports);
35   -
75 test/http-apps/proxy.js
... ... @@ -1,75 +0,0 @@
1   -
2   -var Q = require("q");
3   -var Http = require("../../http");
4   -var Apps = require("../../http-apps");
5   -
6   -exports['test proxy'] = function (assert, done) {
7   -
8   - var requestProxy;
9   - var responseProxy;
10   - var requestActual;
11   - var responseActual;
12   -
13   - var server1 = Http.Server(
14   - Apps.Trap(
15   - Apps.Tap(
16   - Apps.Branch({
17   - "foo": Apps.Branch({
18   - "bar": Apps.Cap(Apps.Content(["Hello, World!"]))
19   - })
20   - }),
21   - function (request) {
22   - requestActual = request;
23   - }
24   - ),
25   - function (response) {
26   - responseActual = response;
27   - return response;
28   - }
29   - )
30   - );
31   -
32   - Q.when(server1.listen(0))
33   - .then(function (server1) {
34   - var port = server1.node.address().port;
35   -
36   - var server2 = Http.Server(
37   - Apps.Trap(
38   - Apps.Tap(
39   - Apps.ProxyTree("http://127.0.0.1:" + port + "/foo/"),
40   - function (request) {
41   - requestProxy = request;
42   - }
43   - ),
44   - function (response) {
45   - responseProxy = response;
46   - return response;
47   - }
48   - )
49   - );
50   -
51   - return [server1, server2.listen(0)];
52   - })
53   - .spread(function (server1, server2) {
54   - var port = server2.node.address().port;
55   - return Http.read("http://127.0.0.1:" + port + "/bar")
56   - .then(function (content) {
57   - assert.equal(content, "Hello, World!", "content");
58   - assert.ok(requestActual, "request actual");
59   - assert.ok(responseActual, "response actual");
60   - assert.ok(requestProxy, "request proxy");
61   - assert.ok(responseProxy, "response proxy");
62   - })
63   - .finally(server1.stop)
64   - .finally(server2.stop)
65   - })
66   - .fail(function (reason) {
67   - assert.ok(false, reason);
68   - })
69   - .finally(done)
70   -
71   -};
72   -
73   -if (require.main === module)
74   - require("test").run(exports);
75   -
7 test/http/all.js
... ... @@ -1,7 +0,0 @@
1   -
2   -exports.testBasic = require("./basic");
3   -
4   -if (module === require.main) {
5   - require("test").run(exports);
6   -}
7   -
106 test/http/basic.js
... ... @@ -1,106 +0,0 @@
1   -
2   -var Q = require("q");
3   -var HTTP = require("../../http");
4   -
5   -exports['test basic'] = function (ASSERT, done) {
6   -
7   - var response = {
8   - "status": 200,
9   - "headers": {
10   - "content-type": "text/plain"
11   - },
12   - "body": [
13   - "Hello, World!"
14   - ]
15   - };
16   -
17   - var server = HTTP.Server(function () {
18   - return response;
19   - });
20   -
21   - Q.when(server.listen(0), function (server) {
22   - var port = server.node.address().port;
23   -
24   - var request = {
25   - "host": "localhost",
26   - "port": port,
27   - "headers": {
28   - "host": "localhost"
29   - }
30   - };
31   -
32   - return Q.when(HTTP.request(request))
33   - .then(function (response) {
34   - ASSERT.ok(!Q.isPromise(response.body), "body is not a promise")
35   - var acc = [];
36   - return response.body.forEach(function (chunk) {
37   - acc.push(chunk.toString("utf-8"));
38   - }).then(function () {
39   - ASSERT.equal(acc.join(""), "Hello, World!", "body is hello world");
40   - });
41   - })
42   - })
43   - .fin(server.stop)
44   - .fin(done)
45   - .fail(function (reason) {
46   - ASSERT.ok(false, reason);
47   - })
48   -
49   -}
50   -
51   -exports['test deferred'] = function (ASSERT, done) {
52   -
53   - var response = {
54   - "status": 200,
55   - "headers": {
56   - "content-type": "text/plain; charset=utf-8"
57   - },
58   - "body": {
59   - "forEach": function (write) {
60   - var deferred = Q.defer();
61   - write("Hello, World!");
62   - setTimeout(function () {
63   - deferred.resolve();
64   - }, 100);
65   - return deferred.promise;
66   - }
67   - }
68   - };
69   -
70   - var server = HTTP.Server(function () {
71   - return response;
72   - });
73   -
74   - Q.when(server.listen(0), function (server) {
75   - var port = server.node.address().port;
76   -
77   - var request = {
78   - "host": "localhost",
79   - "port": port,
80   - "headers": {
81   - "host": "localhost"
82   - }
83   - };
84   -
85   - return Q.when(HTTP.request(request))
86   - .then(function (response) {
87   - var acc = [];
88   - return response.body.forEach(function (chunk) {
89   - acc.push(chunk.toString("utf-8"));
90   - }).then(function () {
91   - ASSERT.equal(acc.join(""), "Hello, World!", "body is hello world");
92   - });
93   - })
94   - })
95   - .fin(server.stop)
96   - .fin(done)
97   - .fail(function (reason) {
98   - ASSERT.ok(false, reason);
99   - })
100   -
101   -}
102   -
103   -if (module === require.main) {
104   - require("test").run(exports);
105   -}
106   -
46 test/http/deferred.js
... ... @@ -1,46 +0,0 @@
1   -
2   -var Q = require("q");
3   -var HTTP = require("../../http");
4   -
5   -var response = {
6   - "status": 200,
7   - "headers": {
8   - "content-type": "text/plain; charset=utf-8"
9   - },
10   - "body": {
11   - "forEach": function (write) {
12   - var deferred = Q.defer();
13   - write("Hello, World!");
14   - setTimeout(function () {
15   - deferred.resolve();
16   - }, 1000);
17   - return deferred.promise;
18   - }
19   - }
20   -};
21   -
22   -var server = HTTP.Server(function () {
23   - return response;
24   -});
25   -
26   -Q.done(server.listen(0), function () {
27   - var port = server.node.address().port;
28   -
29   - var request = {
30   - "host": "localhost",
31   - "port": port,
32   - "headers": {
33   - "host": "localhost"
34   - }
35   - };
36   -
37   - return Q.when(HTTP.request(request), function (response) {
38   - return Q.when(response.body, function (body) {
39   - var done = body.forEach(function (chunk) {
40   - console.log(chunk.toString("utf-8"));
41   - });
42   - Q.when(done, server.stop);
43   - });
44   - });
45   -});
46   -
41 test/http/keep-alive.js
... ... @@ -1,41 +0,0 @@
1   -
2   -var Q = require("q");
3   -var HTTP = require("../../http");
4   -
5   -var request = {
6   - "host": "localhost",
7   - "port": 8080,
8   - "headers": {
9   - "host": "localhost"
10   - }
11   -};
12   -
13   -var response = {
14   - "status": 200,
15   - "headers": {
16   - "content-type": "text/plain"
17   - },
18   - "body": [
19   - "Hello, World!"
20   - ]
21   -};
22   -
23   -var server = HTTP.Server(function () {
24   - return response;
25   -});
26   -
27   -Q.done(server.listen(8080), function () {
28   -
29   - var done = [1,2,3].reduce(function (done) {
30   - return Q.when(HTTP.request(request), function (response) {
31   - return Q.when(response.body, function (body) {
32   - return Q.all([body.forEach(function (chunk) {
33   - console.log(chunk.toString('utf-8'));
34   - }), done]);
35   - });
36   - });
37   - }, undefined);
38   -
39   - return Q.when(done, server.stop);
40   -});
41   -

0 comments on commit d361d09

Please sign in to comment.
Something went wrong with that request. Please try again.