-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
184 lines (165 loc) · 5.93 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
;(function (exports) {
"use strict";
var fs = require("fs");
var path = require("path");
var http = require("http");
var mocha = require("mocha");
var connect = require("connect");
var Router = require("mini-router").Router;
var Promise = require("mini-promise-aplus").Promise;
var data = require(__dirname + "/test/data.js");
var PORT = 8080;
function handleRequest(req, res) {
var url = req.url;
var headers = req.headers;
var json = false;
var empty = false;
var body = "";
if (url.match(/^\/auth/)) {
if (!headers.authorization && !headers.Authorization) {
res.writeHead(401);
res.end();
return;
} else {
var header = headers.authorization ? headers.authorization : headers.Authorization;
var token = headers.authorization.split(/\s+/).pop();
var auth = new Buffer(token, 'base64').toString();
var parts = auth.split(/:/);
var user = parts[0];
var passwd = parts[1];
if (user != data.user || passwd != data.password) {
res.writeHead(401);
res.end();
return;
}
}
}
if (url.match(/^(\/auth)?\/json(\/empty)?/)) {
json = true;
}
if (url.match(/^(\/auth)?(\/json)?\/empty/)) {
empty = true;
}
switch (req.method) {
case "GET":
if (empty) {
if (json) {
res.writeHead(204, { "content-type": "application/json" });
} else {
res.writeHead(204);
}
} else {
if (json) {
res.writeHead(200, { "content-type": "application/json" });
res.write(JSON.stringify(data.jsonMessage, null, '\t'));
} else {
res.write(data.message);
}
}
res.end();
break;
case "POST":
req.on("data", function (data) {
body += data;
});
req.on("end", function () {
if (json) {
try {
body = JSON.parse(body);
if (body.message == data.jsonMessage.message) {
res.writeHead(204);
res.end();
} else {
res.writeHead(400);
res.end("Message does not match expected");
}
} catch (e) {
res.writeHead(400);
res.end("Failed to parse message");
}
} else {
if (body === data.message) {
res.writeHead(204);
res.end();
} else {
res.writeHead(400);
res.end();
}
}
});
req.on("error", function (err) {
res.writeHead(500);
res.end();
});
break;
case "PUT":
req.on("data", function (data) {
body += data;
});
req.on("end", function () {
if (json) {
try {
body = JSON.parse(body);
if (body.message == data.jsonMessage.message) {
res.writeHead(204);
res.end();
} else {
res.writeHead(400);
res.end("Message does not match expected");
}
} catch (e) {
res.writeHead(400);
res.end("Failed to parse message");
}
} else {
if (body === data.message) {
res.writeHead(204);
res.end();
} else {
res.writeHead(400);
res.end();
}
}
});
req.on("error", function (err) {
res.writeHead(500);
res.end();
});
break;
case "DELETE":
res.writeHead(204);
res.end();
break;
default:
res.writeHead(400);
res.end("Unsupported request");
break;
}
}
new Promise(function (resolve, reject) {
var router = new Router("TestRouter");
resolve(router);
}).then(function (router) {
var m = require.resolve("mocha");
router.static("/mocha/mocha.css", path.dirname(m) + "/mocha.css");
router.static("/mocha/mocha.js", path.dirname(m) + "/mocha.js");
router.static("/mini-promise.js", require.resolve("mini-promise-aplus"));
router.static("/mini-module.js", require.resolve("mini-module"));
router.static("/mini-http.js", "./mini-http.js");
router.static("/test", "./test/");
router.add(["GET", "POST", "PUT", "DELETE"], "/", handleRequest);
router.add(["GET", "POST", "PUT", "DELETE"], "/json", handleRequest);
router.add(["GET", "POST", "PUT", "DELETE"], "/json/empty", handleRequest);
router.add(["GET", "POST", "PUT", "DELETE"], "/auth", handleRequest);
return router;
}).then(function (router) {
var app = connect();
app.use(router.connect());
return app;
}).then(function (app) {
http.createServer(app).listen(PORT);
console.log("Ready for requests");
}, function (reason) {
console.error("Failed to start server: ", reason);
});
})();