twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

test-request-methods.js (1457B)


      1 var sys = require("util")
      2   , assert = require("assert")
      3   , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
      4   , http = require("http")
      5   , xhr;
      6 
      7 // Test server
      8 var 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(body);
     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
     32 var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"];
     33 var curMethod = 0;
     34 
     35 function 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         sys.puts("Testing " + methods[curMethod]);
     51         start(methods[curMethod]);
     52       }
     53     }
     54   };
     55 
     56   var url = "http://localhost:8000/" + method;
     57   xhr.open(method, url);
     58   xhr.send();
     59 }
     60 
     61 sys.puts("Testing " + methods[curMethod]);
     62 start(methods[curMethod]);