tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

request-init-002.any.js (2632B)


      1 // META: global=window,worker
      2 // META: title=Request init: headers and body
      3 
      4 test(function() {
      5  var headerDict = {"name1": "value1",
      6                    "name2": "value2",
      7                    "name3": "value3"
      8                    };
      9  var headers = new Headers(headerDict);
     10  var request = new Request("", { "headers" : headers })
     11  for (var name in headerDict) {
     12    assert_equals(request.headers.get(name), headerDict[name],
     13      "request's headers has " + name + " : " + headerDict[name]);
     14  }
     15 }, "Initialize Request with headers values");
     16 
     17 function makeRequestInit(body, method) {
     18  return {"method": method, "body": body};
     19 }
     20 
     21 function checkRequestInit(body, bodyType, expectedTextBody) {
     22  promise_test(function(test) {
     23    var request = new Request("", makeRequestInit(body, "POST"));
     24    if (body) {
     25      assert_throws_js(TypeError, function() { new Request("", makeRequestInit(body, "GET")); });
     26      assert_throws_js(TypeError, function() { new Request("", makeRequestInit(body, "HEAD")); });
     27    } else {
     28      new Request("", makeRequestInit(body, "GET")); // should not throw
     29    }
     30    var reqHeaders = request.headers;
     31    var mime = reqHeaders.get("Content-Type");
     32    assert_true(!body || (mime && mime.search(bodyType) > -1), "Content-Type header should be \"" + bodyType + "\", not \"" + mime + "\"");
     33    return request.text().then(function(bodyAsText) {
     34      //not equals: cannot guess formData exact value
     35      assert_true( bodyAsText.search(expectedTextBody) > -1, "Retrieve and verify request body");
     36    });
     37  }, `Initialize Request's body with "${body}", ${bodyType}`);
     38 }
     39 
     40 var blob = new Blob(["This is a blob"], {type: "application/octet-binary"});
     41 var formaData = new FormData();
     42 formaData.append("name", "value");
     43 var usvString = "This is a USVString"
     44 
     45 checkRequestInit(undefined, undefined, "");
     46 checkRequestInit(null, null, "");
     47 checkRequestInit(blob, "application/octet-binary", "This is a blob");
     48 checkRequestInit(formaData, "multipart/form-data", "name=\"name\"\r\n\r\nvalue");
     49 checkRequestInit(usvString, "text/plain;charset=UTF-8", "This is a USVString");
     50 checkRequestInit({toString: () => "hi!"}, "text/plain;charset=UTF-8", "hi!");
     51 
     52 // Ensure test does not time out in case of missing URLSearchParams support.
     53 if (self.URLSearchParams) {
     54  var urlSearchParams = new URLSearchParams("name=value");
     55  checkRequestInit(urlSearchParams, "application/x-www-form-urlencoded;charset=UTF-8", "name=value");
     56 } else {
     57  promise_test(function(test) {
     58    return Promise.reject("URLSearchParams not supported");
     59  }, "Initialize Request's body with application/x-www-form-urlencoded;charset=UTF-8");
     60 }