rob@77: var sys = require("util") rob@77: , assert = require("assert") rob@77: , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest rob@77: , http = require("http") rob@77: , xhr; rob@77: rob@77: // Test server rob@77: var server = http.createServer(function (req, res) { rob@77: // Check request method and URL rob@77: assert.equal(methods[curMethod], req.method); rob@77: assert.equal("/" + methods[curMethod], req.url); rob@77: rob@77: var body = (req.method != "HEAD" ? "Hello World" : ""); rob@77: rob@77: res.writeHead(200, { rob@77: "Content-Type": "text/plain", rob@77: "Content-Length": Buffer.byteLength(body) rob@77: }); rob@77: // HEAD has no body rob@77: if (req.method != "HEAD") { rob@77: res.write(body); rob@77: } rob@77: res.end(); rob@77: rob@77: if (curMethod == methods.length - 1) { rob@77: this.close(); rob@77: sys.puts("done"); rob@77: } rob@77: }).listen(8000); rob@77: rob@77: // Test standard methods rob@77: var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"]; rob@77: var curMethod = 0; rob@77: rob@77: function start(method) { rob@77: // Reset each time rob@77: xhr = new XMLHttpRequest(); rob@77: rob@77: xhr.onreadystatechange = function() { rob@77: if (this.readyState == 4) { rob@77: if (method == "HEAD") { rob@77: assert.equal("", this.responseText); rob@77: } else { rob@77: assert.equal("Hello World", this.responseText); rob@77: } rob@77: rob@77: curMethod++; rob@77: rob@77: if (curMethod < methods.length) { rob@77: sys.puts("Testing " + methods[curMethod]); rob@77: start(methods[curMethod]); rob@77: } rob@77: } rob@77: }; rob@77: rob@77: var url = "http://localhost:8000/" + method; rob@77: xhr.open(method, url); rob@77: xhr.send(); rob@77: } rob@77: rob@77: sys.puts("Testing " + methods[curMethod]); rob@77: start(methods[curMethod]);