Skip to content

Commit 8c65c3f

Browse files
committed
fix(test): implement echo-server
1 parent bec1be9 commit 8c65c3f

File tree

5 files changed

+80
-17
lines changed

5 files changed

+80
-17
lines changed

Ch1_WhatsPromises/test/xhr-promise-test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
var assert = require('power-assert');
22
var getURL = require("../src/xhr-promise").getURL;
3-
4-
require("http-echo");
3+
var echoServer = require("../../test/echo-server");
54
describe('#getURL', function () {
5+
beforeEach(() => {
6+
return echoServer.start();
7+
});
8+
afterEach(() => {
9+
return echoServer.stop();
10+
});
611
describe("when get data", function () {
712
it("should resolve with value", function () {
813
var URL = "http://localhost:3000/?status=200&body=text";
@@ -19,4 +24,4 @@ describe('#getURL', function () {
1924
});
2025
});
2126
});
22-
});
27+
});

Ch4_AdvancedPromises/test/xhr-deferred-test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
"use strict";
22
var assert = require('power-assert');
33
var getURL = require("../src/deferred/xhr-deferred").getURL;
4+
var echoServer = require("../../test/echo-server");
45

5-
require("http-echo");
66
describe('#cancelableXHR', function () {
7+
beforeEach(() => {
8+
return echoServer.start();
9+
});
10+
afterEach(() => {
11+
return echoServer.stop();
12+
});
713
describe("when get data", function () {
814
it("should resolve with value", function () {
915
var URL = "http://localhost:3000/?status=200&body=text";
@@ -21,4 +27,4 @@ describe('#cancelableXHR', function () {
2127
});
2228
});
2329

24-
});
30+
});

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"build-js-min": "gulp build-js-min",
1414
"embed": "gulp embed",
1515
"lint-html": "gulp lint-html",
16-
"textlint": "textlint -f pretty-error Appendix-*/ Ch*/",
17-
"textlint:fix": "textlint --fix Appendix-*/ Ch*/",
18-
"test": "mocha ./Ch**/test/*.js && npm run textlint"
16+
"textlint": "textlint \"Appendix-*/*/**.adoc\" \"Ch*/*/**.adoc\"",
17+
"textlint:fix": "textlint --fix \"Appendix-*/*/**.adoc\" \"Ch*/*/**.adoc\"",
18+
"test": "mocha \"./Ch**/test/*.js\""
1919
},
2020
"directories": {
2121
"test": "Ch*/test/"
@@ -29,7 +29,7 @@
2929
"codemirror": "^5.46.0",
3030
"codemirror-console-ui": "^2.0.5",
3131
"native-promise-only": "^0.8.1",
32-
"w3c-xmlhttprequest": "^2.1.0",
32+
"w3c-xmlhttprequest": "^2.1.3",
3333
"ypromise": "^0.3.0"
3434
},
3535
"devDependencies": {
@@ -47,7 +47,6 @@
4747
"gulp-remove-use-strict": "0.0.2",
4848
"gulp-rename": "~1.4.0",
4949
"handlebars": "^4.1.2",
50-
"http-echo": "0.0.2",
5150
"inlining-node-require": "^0.1.0",
5251
"intelli-espower-loader": "^1.0.0",
5352
"minifyify": "^7.0.1",

test/echo-server.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var http = require("http");
2+
var url = require("url");
3+
var querystring = require("querystring");
4+
var server = null;
5+
var port = process.env.PORT || 3000;
6+
function start() {
7+
server = http.createServer();
8+
server.on("request", function (request, response) {
9+
var uri = url.parse(request.url);
10+
var qs = uri.query ? querystring.parse(uri.query) : {};
11+
12+
var status = qs.status || 200;
13+
var contentType = qs.contentType || "text/plain";
14+
var body = qs.body || "hello there!";
15+
16+
response.writeHead(status, {
17+
"Content-Type": contentType,
18+
"Content-Length": body.length
19+
});
20+
21+
console.log(uri.pathname + " - HTTP " + status + " (" + contentType + "): " + body);
22+
23+
response.end(body);
24+
});
25+
26+
return new Promise((resolve, reject) => {
27+
server.listen(port, function (error) {
28+
if (error) {
29+
return reject(error);
30+
}
31+
console.log("listening on port " + port);
32+
resolve();
33+
});
34+
});
35+
36+
}
37+
38+
function stop() {
39+
return new Promise((resolve, reject) => {
40+
if (!server) {
41+
return resolve();
42+
}
43+
server.close((error) => {
44+
if (error) {
45+
reject(error);
46+
} else {
47+
resolve();
48+
}
49+
});
50+
});
51+
}
52+
53+
module.exports = {
54+
start,
55+
stop
56+
};

yarn.lock

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,10 +2519,6 @@ htmlparser2@^3.9.1:
25192519
inherits "^2.0.1"
25202520
readable-stream "^2.0.2"
25212521

2522-
2523-
version "0.0.2"
2524-
resolved "https://registry.yarnpkg.com/http-echo/-/http-echo-0.0.2.tgz#42f4d4b785ae5ec83e9ccad8128163f8561af05d"
2525-
25262522
https-browserify@^1.0.0:
25272523
version "1.0.0"
25282524
resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-1.0.0.tgz#ec06c10e0a34c0f2faf199f7fd7fc78fffd03c73"
@@ -5798,9 +5794,10 @@ vm-browserify@^1.0.0:
57985794
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.0.tgz#bd76d6a23323e2ca8ffa12028dc04559c75f9019"
57995795
integrity sha512-iq+S7vZJE60yejDYM0ek6zg308+UZsdtPExWP9VZoCFCz1zkJoXFnAX7aZfd/ZwrkidzdUZL0C/ryW+JwAiIGw==
58005796

5801-
w3c-xmlhttprequest@^2.1.0:
5802-
version "2.1.2"
5803-
resolved "https://registry.yarnpkg.com/w3c-xmlhttprequest/-/w3c-xmlhttprequest-2.1.2.tgz#f0bc8937562828e2c2b9b40366486a1351fe41a0"
5797+
w3c-xmlhttprequest@^2.1.3:
5798+
version "2.1.3"
5799+
resolved "https://registry.yarnpkg.com/w3c-xmlhttprequest/-/w3c-xmlhttprequest-2.1.3.tgz#b0aa5e060577ab874295529e4e2d037312874122"
5800+
integrity sha512-NOCpSoUIcGQl/D3tMLUizhytW9J0mLUqw/nf1lzOpub7X5wgg6+T5jxnLz9foineWGs8baLt+Ldxf8DDyuQHjg==
58045801

58055802
watchify@^3.11.1:
58065803
version "3.11.1"

0 commit comments

Comments
 (0)