response-init-002.any.js (2529B)
1 // META: global=window,worker 2 // META: title=Response init: body and headers 3 // META: script=../resources/utils.js 4 5 test(function() { 6 var headerDict = {"name1": "value1", 7 "name2": "value2", 8 "name3": "value3" 9 }; 10 var headers = new Headers(headerDict); 11 var response = new Response("", { "headers" : headers }) 12 for (var name in headerDict) { 13 assert_equals(response.headers.get(name), headerDict[name], 14 "response's headers has " + name + " : " + headerDict[name]); 15 } 16 }, "Initialize Response with headers values"); 17 18 function checkResponseInit(body, bodyType, expectedTextBody) { 19 promise_test(function(test) { 20 var response = new Response(body); 21 var resHeaders = response.headers; 22 var mime = resHeaders.get("Content-Type"); 23 assert_true(mime && mime.search(bodyType) > -1, "Content-Type header should be \"" + bodyType + "\" "); 24 return response.text().then(function(bodyAsText) { 25 //not equals: cannot guess formData exact value 26 assert_true(bodyAsText.search(expectedTextBody) > -1, "Retrieve and verify response body"); 27 }); 28 }, "Initialize Response's body with " + bodyType); 29 } 30 31 var blob = new Blob(["This is a blob"], {type: "application/octet-binary"}); 32 var formaData = new FormData(); 33 formaData.append("name", "value"); 34 var urlSearchParams = "URLSearchParams are not supported"; 35 //avoid test timeout if not implemented 36 if (self.URLSearchParams) 37 urlSearchParams = new URLSearchParams("name=value"); 38 var usvString = "This is a USVString" 39 40 checkResponseInit(blob, "application/octet-binary", "This is a blob"); 41 checkResponseInit(formaData, "multipart/form-data", "name=\"name\"\r\n\r\nvalue"); 42 checkResponseInit(urlSearchParams, "application/x-www-form-urlencoded;charset=UTF-8", "name=value"); 43 checkResponseInit(usvString, "text/plain;charset=UTF-8", "This is a USVString"); 44 45 promise_test(function(test) { 46 var body = "This is response body"; 47 var response = new Response(body); 48 return validateStreamFromString(response.body.getReader(), body); 49 }, "Read Response's body as readableStream"); 50 51 promise_test(function(test) { 52 var response = new Response("This is my fork", {"headers" : [["Content-Type", ""]]}); 53 return response.blob().then(function(blob) { 54 assert_equals(blob.type, "", "Blob type should be the empty string"); 55 }); 56 }, "Testing empty Response Content-Type header"); 57 58 test(function() { 59 var response = new Response(null, {status: 204}); 60 assert_equals(response.body, null); 61 }, "Testing null Response body");