netwerk/test/unit/test_assoc.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_assoc.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +var httpserver = new HttpServer();
     1.7 +var currentTestIndex = 0;
     1.8 +
     1.9 +XPCOMUtils.defineLazyGetter(this, "port", function() {
    1.10 +    return httpserver.identity.primaryPort;
    1.11 +});
    1.12 +
    1.13 +XPCOMUtils.defineLazyGetter(this, "tests", function() {
    1.14 +    return [
    1.15 +            // this is valid
    1.16 +            {url: "/assoc/assoctest?valid",
    1.17 +             responseheader: ["Assoc-Req: GET http://localhost:" + port +
    1.18 +                              "/assoc/assoctest?valid",
    1.19 +                              "Pragma: X-Verify-Assoc-Req"],
    1.20 +             flags: 0},
    1.21 +
    1.22 +            // this is invalid because the method is wrong
    1.23 +            {url: "/assoc/assoctest?invalid",
    1.24 +             responseheader: ["Assoc-Req: POST http://localhost:" + port +
    1.25 +                              "/assoc/assoctest?invalid",
    1.26 +                              "Pragma: X-Verify-Assoc-Req"],
    1.27 +             flags: CL_EXPECT_LATE_FAILURE},
    1.28 +
    1.29 +            // this is invalid because the url is wrong
    1.30 +            {url: "/assoc/assoctest?notvalid",
    1.31 +             responseheader: ["Assoc-Req: GET http://localhost:" + port +
    1.32 +                              "/wrongpath/assoc/assoctest?notvalid",
    1.33 +                              "Pragma: X-Verify-Assoc-Req"],
    1.34 +             flags: CL_EXPECT_LATE_FAILURE},
    1.35 +
    1.36 +             // this is invalid because the space between method and URL is missing
    1.37 +            {url: "/assoc/assoctest?invalid2",
    1.38 +             responseheader: ["Assoc-Req: GEThttp://localhost:" + port +
    1.39 +                              "/assoc/assoctest?invalid2",
    1.40 +                              "Pragma: X-Verify-Assoc-Req"],
    1.41 +             flags: CL_EXPECT_LATE_FAILURE},
    1.42 +    ];
    1.43 +});
    1.44 +
    1.45 +var oldPrefVal;
    1.46 +var domBranch;
    1.47 +
    1.48 +function setupChannel(url)
    1.49 +{
    1.50 +    var ios = Components.classes["@mozilla.org/network/io-service;1"].
    1.51 +                         getService(Ci.nsIIOService);
    1.52 +    var chan = ios.newChannel("http://localhost:" + port + url, "", null);
    1.53 +    return chan;
    1.54 +}
    1.55 +
    1.56 +function startIter()
    1.57 +{
    1.58 +    var channel = setupChannel(tests[currentTestIndex].url);
    1.59 +    channel.asyncOpen(new ChannelListener(completeIter,
    1.60 +                                          channel, tests[currentTestIndex].flags), null);
    1.61 +}
    1.62 +
    1.63 +function completeIter(request, data, ctx)
    1.64 +{
    1.65 +    if (++currentTestIndex < tests.length ) {
    1.66 +        startIter();
    1.67 +    } else {
    1.68 +        domBranch.setBoolPref("enforce", oldPrefVal);
    1.69 +        httpserver.stop(do_test_finished);
    1.70 +    }
    1.71 +}
    1.72 +
    1.73 +function run_test()
    1.74 +{
    1.75 +    var prefService =
    1.76 +        Components.classes["@mozilla.org/preferences-service;1"]
    1.77 +        .getService(Components.interfaces.nsIPrefService);
    1.78 +    domBranch = prefService.getBranch("network.http.assoc-req.");
    1.79 +    oldPrefVal = domBranch.getBoolPref("enforce");
    1.80 +    domBranch.setBoolPref("enforce", true);
    1.81 +
    1.82 +    httpserver.registerPathHandler("/assoc/assoctest", handler);
    1.83 +    httpserver.start(-1);
    1.84 +
    1.85 +    startIter();
    1.86 +    do_test_pending();
    1.87 +}
    1.88 +
    1.89 +function handler(metadata, response)
    1.90 +{
    1.91 +    var body = "thequickbrownfox";
    1.92 +    response.setHeader("Content-Type", "text/plain", false);
    1.93 +
    1.94 +    var header = tests[currentTestIndex].responseheader;
    1.95 +    if (header != undefined) {
    1.96 +        for (var i = 0; i < header.length; i++) {
    1.97 +            var splitHdr = header[i].split(": ");
    1.98 +            response.setHeader(splitHdr[0], splitHdr[1], false);
    1.99 +        }
   1.100 +    }
   1.101 +    
   1.102 +    response.setStatusLine(metadata.httpVersion, 200, "OK");
   1.103 +    response.bodyOutputStream.write(body, body.length);
   1.104 +}

mercurial