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