response-init-001.any.js (2484B)
1 // META: global=window,worker 2 // META: title=Response init: simple cases 3 4 var defaultValues = { "type" : "default", 5 "url" : "", 6 "ok" : true, 7 "status" : 200, 8 "statusText" : "", 9 "body" : null 10 }; 11 12 var statusCodes = { "givenValues" : [200, 300, 400, 500, 599], 13 "expectedValues" : [200, 300, 400, 500, 599] 14 }; 15 var statusTexts = { "givenValues" : ["", "OK", "with space", String.fromCharCode(0x80)], 16 "expectedValues" : ["", "OK", "with space", String.fromCharCode(0x80)] 17 }; 18 var initValuesDict = { "status" : statusCodes, 19 "statusText" : statusTexts 20 }; 21 22 function isOkStatus(status) { 23 return 200 <= status && 299 >= status; 24 } 25 26 var response = new Response(); 27 for (var attributeName in defaultValues) { 28 test(function() { 29 var expectedValue = defaultValues[attributeName]; 30 assert_equals(response[attributeName], expectedValue, 31 "Expect default response." + attributeName + " is " + expectedValue); 32 }, "Check default value for " + attributeName + " attribute"); 33 } 34 35 for (var attributeName in initValuesDict) { 36 test(function() { 37 var valuesToTest = initValuesDict[attributeName]; 38 for (var valueIdx in valuesToTest["givenValues"]) { 39 var givenValue = valuesToTest["givenValues"][valueIdx]; 40 var expectedValue = valuesToTest["expectedValues"][valueIdx]; 41 var responseInit = {}; 42 responseInit[attributeName] = givenValue; 43 var response = new Response("", responseInit); 44 assert_equals(response[attributeName], expectedValue, 45 "Expect response." + attributeName + " is " + expectedValue + 46 " when initialized with " + givenValue); 47 assert_equals(response.ok, isOkStatus(response.status), 48 "Expect response.ok is " + isOkStatus(response.status)); 49 } 50 }, "Check " + attributeName + " init values and associated getter"); 51 } 52 53 test(function() { 54 const response1 = new Response(""); 55 assert_equals(response1.headers, response1.headers); 56 57 const response2 = new Response("", {"headers": {"X-Foo": "bar"}}); 58 assert_equals(response2.headers, response2.headers); 59 const headers = response2.headers; 60 response2.headers.set("X-Foo", "quux"); 61 assert_equals(headers, response2.headers); 62 headers.set("X-Other-Header", "baz"); 63 assert_equals(headers, response2.headers); 64 }, "Test that Response.headers has the [SameObject] extended attribute");