Skip to content

Commit a1eda3c

Browse files
author
Andy
authored
Merge pull request microsoft#10341 from Microsoft/runtests_browser_response_code
Use proper response codes in web tests
2 parents 78cea2a + 592805f commit a1eda3c

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

tests/webTestServer.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,21 @@ function handleResolutionRequest(filePath: string, res: http.ServerResponse) {
117117
let resolvedPath = path.resolve(filePath, "");
118118
resolvedPath = resolvedPath.substring(resolvedPath.indexOf("tests"));
119119
resolvedPath = switchToForwardSlashes(resolvedPath);
120-
send("success", res, resolvedPath);
121-
return;
120+
send(ResponseCode.Success, res, resolvedPath);
122121
}
123122

124-
function send(result: "fail", res: http.ServerResponse, contents: string, contentType?: string): void;
125-
function send(result: "success", res: http.ServerResponse, contents: string, contentType?: string): void;
126-
function send(result: "unknown", res: http.ServerResponse, contents: string, contentType?: string): void;
127-
function send(result: string, res: http.ServerResponse, contents: string, contentType?: string): void
128-
function send(result: string, res: http.ServerResponse, contents: string, contentType = "binary"): void {
129-
const responseCode = result === "success" ? 200 : result === "fail" ? 500 : result === "unknown" ? 404 : parseInt(result);
123+
const enum ResponseCode {
124+
Success = 200,
125+
BadRequest = 400,
126+
NotFound = 404,
127+
MethodNotAllowed = 405,
128+
PayloadTooLarge = 413,
129+
Fail = 500
130+
}
131+
132+
function send(responseCode: number, res: http.ServerResponse, contents: string, contentType = "binary"): void {
130133
res.writeHead(responseCode, { "Content-Type": contentType });
131134
res.end(contents);
132-
return;
133135
}
134136

135137
// Reads the data from a post request and passes it to the given callback
@@ -142,7 +144,7 @@ function processPost(req: http.ServerRequest, res: http.ServerResponse, callback
142144
queryData += data;
143145
if (queryData.length > 1e8) {
144146
queryData = "";
145-
send("413", res, undefined);
147+
send(ResponseCode.PayloadTooLarge, res, undefined);
146148
console.log("ERROR: destroying connection");
147149
req.connection.destroy();
148150
}
@@ -155,7 +157,7 @@ function processPost(req: http.ServerRequest, res: http.ServerResponse, callback
155157

156158
}
157159
else {
158-
send("405", res, undefined);
160+
send(ResponseCode.MethodNotAllowed, res, undefined);
159161
}
160162
}
161163

@@ -201,16 +203,16 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
201203
switch (operation) {
202204
case RequestType.GetDir:
203205
const filesInFolder = dir(reqPath, "", { recursive: true });
204-
send("success", res, filesInFolder.join(","));
206+
send(ResponseCode.Success, res, filesInFolder.join(","));
205207
break;
206208
case RequestType.GetFile:
207209
fs.readFile(reqPath, (err, file) => {
208210
const contentType = contentTypeForExtension(path.extname(reqPath));
209211
if (err) {
210-
send("fail", res, err.message, contentType);
212+
send(ResponseCode.NotFound, res, err.message, contentType);
211213
}
212214
else {
213-
send("success", res, <any>file, contentType);
215+
send(ResponseCode.Success, res, <any>file, contentType);
214216
}
215217
});
216218
break;
@@ -222,33 +224,33 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
222224
processPost(req, res, (data) => {
223225
writeFile(reqPath, data, { recursive: true });
224226
});
225-
send("success", res, undefined);
227+
send(ResponseCode.Success, res, undefined);
226228
break;
227229
case RequestType.WriteDir:
228230
fs.mkdirSync(reqPath);
229-
send("success", res, undefined);
231+
send(ResponseCode.Success, res, undefined);
230232
break;
231233
case RequestType.DeleteFile:
232234
if (fs.existsSync(reqPath)) {
233235
fs.unlinkSync(reqPath);
234236
}
235-
send("success", res, undefined);
237+
send(ResponseCode.Success, res, undefined);
236238
break;
237239
case RequestType.DeleteDir:
238240
if (fs.existsSync(reqPath)) {
239241
fs.rmdirSync(reqPath);
240242
}
241-
send("success", res, undefined);
243+
send(ResponseCode.Success, res, undefined);
242244
break;
243245
case RequestType.AppendFile:
244246
processPost(req, res, (data) => {
245247
fs.appendFileSync(reqPath, data);
246248
});
247-
send("success", res, undefined);
249+
send(ResponseCode.Success, res, undefined);
248250
break;
249251
case RequestType.Unknown:
250252
default:
251-
send("unknown", res, undefined);
253+
send(ResponseCode.BadRequest, res, undefined);
252254
break;
253255
}
254256

0 commit comments

Comments
 (0)