1 | var sys = require("util")
|
2 | ,assert = require("assert")
|
3 | ,XMLHttpRequest = require("../XMLHttpRequest").XMLHttpRequest
|
4 | ,http = require("http")
|
5 | ,xhr;
|
6 |
|
7 |
|
8 | var server = http.createServer(function (req, res) {
|
9 |
|
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 |
|
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 |
|
32 | var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"];
|
33 | var curMethod = 0;
|
34 |
|
35 | function start(method) {
|
36 |
|
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 |
|
60 | start(methods[curMethod]); |
\ | No newline at end of file |