UNPKG

1.31 kBJavaScriptView Raw
1var sys = require("util")
2 ,assert = require("assert")
3 ,XMLHttpRequest = require("../XMLHttpRequest").XMLHttpRequest
4 ,http = require("http")
5 ,xhr;
6
7// Test server
8var server = http.createServer(function (req, res) {
9 // Check request method and URL
10 assert.equal(methods[curMethod], req.method);
11 assert.equal("/" + methods[curMethod], req.url);
12
13 var body = (req.method != "HEAD" ? "Hello World" : "");
14
15 res.writeHead(200, {
16 "Content-Type": "text/plain",
17 "Content-Length": Buffer.byteLength(body)
18 });
19 // HEAD has no body
20 if (req.method != "HEAD") {
21 res.write("Hello World");
22 }
23 res.end();
24
25 if (curMethod == methods.length - 1) {
26 this.close();
27 sys.puts("done");
28 }
29}).listen(8000);
30
31// Test standard methods
32var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"];
33var curMethod = 0;
34
35function start(method) {
36 // Reset each time
37 xhr = new XMLHttpRequest();
38
39 xhr.onreadystatechange = function() {
40 if (this.readyState == 4) {
41 if (method == "HEAD") {
42 assert.equal("", this.responseText);
43 } else {
44 assert.equal("Hello World", this.responseText);
45 }
46
47 curMethod++;
48
49 if (curMethod < methods.length) {
50 start(methods[curMethod]);
51 }
52 }
53 };
54
55 var url = "http://localhost:8000/" + method;
56 xhr.open(method, url);
57 xhr.send();
58}
59
60start(methods[curMethod]);
\No newline at end of file