Bug 1108181 - Make Headers iterable; r=bzbarsky

This commit is contained in:
Ehsan Akhgari 2015-10-19 13:58:03 -04:00
parent e076384a90
commit e98576e61b
23 changed files with 99 additions and 307 deletions

View File

@ -102,6 +102,19 @@ public:
mInternalHeaders->Set(aName, aValue, aRv);
}
uint32_t GetIterableLength() const
{
return mInternalHeaders->GetIterableLength();
}
const nsString GetKeyAtIndex(unsigned aIndex) const
{
return mInternalHeaders->GetKeyAtIndex(aIndex);
}
const nsString GetValueAtIndex(unsigned aIndex) const
{
return mInternalHeaders->GetValueAtIndex(aIndex);
}
// ChromeOnly
HeadersGuardEnum Guard() const
{

View File

@ -70,6 +70,21 @@ public:
bool Has(const nsACString& aName, ErrorResult& aRv) const;
void Set(const nsACString& aName, const nsACString& aValue, ErrorResult& aRv);
uint32_t GetIterableLength() const
{
return mList.Length();
}
const NS_ConvertASCIItoUTF16 GetKeyAtIndex(unsigned aIndex) const
{
MOZ_ASSERT(aIndex < mList.Length());
return NS_ConvertASCIItoUTF16(mList[aIndex].mName);
}
const NS_ConvertASCIItoUTF16 GetValueAtIndex(unsigned aIndex) const
{
MOZ_ASSERT(aIndex < mList.Length());
return NS_ConvertASCIItoUTF16(mList[aIndex].mValue);
}
void Clear();
HeadersGuardEnum Guard() const { return mGuard; }

View File

@ -13,16 +13,27 @@ function shouldThrow(func, expected, msg) {
}
}
function recursiveArrayCompare(actual, expected) {
is(Array.isArray(actual), Array.isArray(expected), "Both should either be arrays, or not");
if (Array.isArray(actual) && Array.isArray(expected)) {
var diff = actual.length !== expected.length;
for (var i = 0, n = actual.length; !diff && i < n; ++i) {
diff = recursiveArrayCompare(actual[i], expected[i]);
}
return diff;
} else {
return actual !== expected;
}
}
function arrayEquals(actual, expected, msg) {
if (actual === expected) {
return;
}
var diff = actual.length !== expected.length;
for (var i = 0, n = actual.length; !diff && i < n; ++i) {
diff = actual[i] !== expected[i];
}
var diff = recursiveArrayCompare(actual, expected);
ok(!diff, msg);
if (diff) {
@ -169,8 +180,60 @@ function TestFilledHeaders() {
}, TypeError, "Fill with non-tuple sequence should throw TypeError.");
}
function iterate(iter) {
var result = [];
for (var val = iter.next(); !val.done;) {
result.push(val.value);
val = iter.next();
}
return result;
}
function iterateForOf(iter) {
var result = [];
for (var value of iter) {
result.push(value);
}
return result;
}
function byteInflate(str) {
var encoder = new TextEncoder("utf-8");
var encoded = encoder.encode(str);
var result = "";
for (var i = 0; i < encoded.length; ++i) {
result += String.fromCharCode(encoded[i]);
}
return result
}
function TestHeadersIterator() {
var ehsanInflated = byteInflate("احسان");
var headers = new Headers();
headers.set("foo0", "bar0");
headers.append("foo", "bar");
headers.append("foo", ehsanInflated);
headers.append("Foo2", "bar2");
headers.set("Foo2", "baz2");
headers.set("foo3", "bar3");
headers.delete("foo0");
headers.delete("foo3");
var key_iter = headers.keys();
var value_iter = headers.values();
var entries_iter = headers.entries();
arrayEquals(iterate(key_iter), ["foo", "foo", "foo2"], "Correct key iterator");
arrayEquals(iterate(value_iter), ["bar", ehsanInflated, "baz2"], "Correct value iterator");
arrayEquals(iterate(entries_iter), [["foo", "bar"], ["foo", ehsanInflated], ["foo2", "baz2"]], "Correct entries iterator");
arrayEquals(iterateForOf(headers), [["foo", "bar"], ["foo", ehsanInflated], ["foo2", "baz2"]], "Correct entries iterator");
arrayEquals(iterateForOf(new Headers(headers)), [["foo", "bar"], ["foo", ehsanInflated], ["foo2", "baz2"]], "Correct entries iterator");
}
function runTest() {
TestEmptyHeaders();
TestFilledHeaders();
TestHeadersIterator();
return Promise.resolve();
}

View File

@ -27,6 +27,7 @@ interface Headers {
[Throws] sequence<ByteString> getAll(ByteString name);
[Throws] boolean has(ByteString name);
[Throws] void set(ByteString name, ByteString value);
iterable<ByteString, ByteString>;
// Used to test different guard states from mochitest.
// Note: Must be set prior to populating headers or will throw.

View File

@ -0,0 +1 @@
prefs: [dom.serviceWorkers.enabled: true, dom.serviceWorkers.interception.enabled: true, dom.serviceWorkers.exemptFromPerDomainMax:true, dom.caches.enabled:true]

View File

@ -1,3 +0,0 @@
[cache-add.https.html]
type: testharness
prefs: [dom.serviceWorkers.enabled: true, dom.serviceWorkers.interception.enabled: true, dom.serviceWorkers.exemptFromPerDomainMax:true, dom.caches.enabled:true]

View File

@ -1,3 +0,0 @@
[cache-delete.https.html]
type: testharness
prefs: [dom.serviceWorkers.enabled: true, dom.serviceWorkers.interception.enabled: true, dom.serviceWorkers.exemptFromPerDomainMax:true, dom.caches.enabled:true]

View File

@ -1,54 +0,0 @@
[cache-match.https.html]
type: testharness
prefs: [dom.serviceWorkers.enabled: true, dom.serviceWorkers.interception.enabled: true, dom.serviceWorkers.exemptFromPerDomainMax:true, dom.caches.enabled:true]
[Cache.match and Cache.matchAll]
bug: https://github.com/w3c/web-platform-tests/issues/2098
[Cache.matchAll with URL]
expected: FAIL
[Cache.match with URL]
expected: FAIL
[Cache.matchAll with Request]
expected: FAIL
[Cache.match with Request]
expected: FAIL
[Cache.matchAll with new Request]
expected: FAIL
[Cache.match with new Request]
expected: FAIL
[Cache.matchAll with ignoreSearch option (request with no search parameters)]
expected: FAIL
[Cache.match with ignoreSearch option (request with no search parameters)]
expected: FAIL
[Cache.matchAll with ignoreSearch option (request with search parameter)]
expected: FAIL
[Cache.match with ignoreSearch option (request with search parameter)]
expected: FAIL
[Cache.matchAll with URL containing fragment]
expected: FAIL
[Cache.match with URL containing fragment]
expected: FAIL
[Cache.matchAll with responses containing "Vary" header]
expected: FAIL
[Cache.match with responses containing "Vary" header]
expected: FAIL
[Cache.matchAll with "ignoreVary" parameter]
expected: FAIL
[Cache.match with Request and Response objects with different URLs]
expected: FAIL

View File

@ -1,25 +0,0 @@
[cache-put.https.html]
type: testharness
prefs: [dom.serviceWorkers.enabled: true, dom.serviceWorkers.interception.enabled: true, dom.serviceWorkers.exemptFromPerDomainMax:true, dom.caches.enabled:true]
[Cache.put with request URLs containing embedded credentials]
expected: FAIL
bug: https://github.com/w3c/web-platform-tests/issues/2098
[Cache.put called with Request and Response from fetch()]
expected: FAIL
[Cache.put with a Response containing an empty URL]
expected: FAIL
[Cache.put with HTTP 500 response]
expected: FAIL
[Cache.put called twice with matching Requests and different Responses]
expected: FAIL
[Cache.put called twice with request URLs that differ only by a fragment]
expected: FAIL
[Cache.put with a relative URL]
expected: FAIL

View File

@ -1,3 +0,0 @@
[cache-storage-keys.https.html]
type: testharness
prefs: [dom.serviceWorkers.enabled: true, dom.serviceWorkers.interception.enabled: true, dom.serviceWorkers.exemptFromPerDomainMax:true, dom.caches.enabled:true]

View File

@ -1,15 +0,0 @@
[cache-storage-match.https.html]
type: testharness
prefs: [dom.serviceWorkers.enabled: true, dom.serviceWorkers.interception.enabled: true, dom.serviceWorkers.exemptFromPerDomainMax:true, dom.caches.enabled:true]
[CacheStorageMatch with no cache name provided]
expected: FAIL
[CacheStorageMatch from one of many caches]
expected: FAIL
[CacheStorageMatch from one of many caches by name]
expected: FAIL
[CacheStorageMatch a string request]
expected: FAIL

View File

@ -1,3 +0,0 @@
[cache-storage.https.html]
type: testharness
prefs: [dom.serviceWorkers.enabled: true, dom.serviceWorkers.interception.enabled: true, dom.serviceWorkers.exemptFromPerDomainMax:true, dom.caches.enabled:true]

View File

@ -1,52 +0,0 @@
[cache-match.https.html]
type: testharness
prefs: [dom.caches.enabled:true]
bug: https://github.com/w3c/web-platform-tests/issues/2098
[Cache.matchAll with URL]
expected: FAIL
[Cache.match with URL]
expected: FAIL
[Cache.matchAll with Request]
expected: FAIL
[Cache.match with Request]
expected: FAIL
[Cache.matchAll with new Request]
expected: FAIL
[Cache.match with new Request]
expected: FAIL
[Cache.matchAll with ignoreSearch option (request with no search parameters)]
expected: FAIL
[Cache.match with ignoreSearch option (request with no search parameters)]
expected: FAIL
[Cache.matchAll with ignoreSearch option (request with search parameter)]
expected: FAIL
[Cache.match with ignoreSearch option (request with search parameter)]
expected: FAIL
[Cache.matchAll with URL containing fragment]
expected: FAIL
[Cache.match with URL containing fragment]
expected: FAIL
[Cache.matchAll with responses containing "Vary" header]
expected: FAIL
[Cache.match with responses containing "Vary" header]
expected: FAIL
[Cache.matchAll with "ignoreVary" parameter]
expected: FAIL
[Cache.match with Request and Response objects with different URLs]
expected: FAIL

View File

@ -1,25 +0,0 @@
[cache-put.https.html]
type: testharness
prefs: [dom.caches.enabled:true]
[Cache.put with request URLs containing embedded credentials]
expected: FAIL
bug: https://github.com/w3c/web-platform-tests/issues/2098
[Cache.put called with Request and Response from fetch()]
expected: FAIL
[Cache.put with a Response containing an empty URL]
expected: FAIL
[Cache.put with HTTP 500 response]
expected: FAIL
[Cache.put called twice with matching Requests and different Responses]
expected: FAIL
[Cache.put called twice with request URLs that differ only by a fragment]
expected: FAIL
[Cache.put with a relative URL]
expected: FAIL

View File

@ -1,15 +0,0 @@
[cache-storage-match.https.html]
type: testharness
prefs: [dom.caches.enabled:true]
[CacheStorageMatch with no cache name provided]
expected: FAIL
[CacheStorageMatch from one of many caches]
expected: FAIL
[CacheStorageMatch from one of many caches by name]
expected: FAIL
[CacheStorageMatch a string request]
expected: FAIL

View File

@ -0,0 +1 @@
prefs: [dom.caches.enabled:true]

View File

@ -1,3 +0,0 @@
[cache-add.https.html]
type: testharness
prefs: [dom.caches.enabled:true]

View File

@ -1,3 +0,0 @@
[cache-delete.https.html]
type: testharness
prefs: [dom.caches.enabled:true]

View File

@ -1,52 +0,0 @@
[cache-match.https.html]
type: testharness
prefs: [dom.caches.enabled:true]
bug: https://github.com/w3c/web-platform-tests/issues/2098
[Cache.matchAll with URL]
expected: FAIL
[Cache.match with URL]
expected: FAIL
[Cache.matchAll with Request]
expected: FAIL
[Cache.match with Request]
expected: FAIL
[Cache.matchAll with new Request]
expected: FAIL
[Cache.match with new Request]
expected: FAIL
[Cache.matchAll with ignoreSearch option (request with no search parameters)]
expected: FAIL
[Cache.match with ignoreSearch option (request with no search parameters)]
expected: FAIL
[Cache.matchAll with ignoreSearch option (request with search parameter)]
expected: FAIL
[Cache.match with ignoreSearch option (request with search parameter)]
expected: FAIL
[Cache.matchAll with URL containing fragment]
expected: FAIL
[Cache.match with URL containing fragment]
expected: FAIL
[Cache.matchAll with responses containing "Vary" header]
expected: FAIL
[Cache.match with responses containing "Vary" header]
expected: FAIL
[Cache.matchAll with "ignoreVary" parameter]
expected: FAIL
[Cache.match with Request and Response objects with different URLs]
expected: FAIL

View File

@ -1,25 +0,0 @@
[cache-put.https.html]
type: testharness
prefs: [dom.caches.enabled:true]
[Cache.put with request URLs containing embedded credentials]
expected: FAIL
bug: https://github.com/w3c/web-platform-tests/issues/2098
[Cache.put called with Request and Response from fetch()]
expected: FAIL
[Cache.put with a Response containing an empty URL]
expected: FAIL
[Cache.put with HTTP 500 response]
expected: FAIL
[Cache.put called twice with matching Requests and different Responses]
expected: FAIL
[Cache.put called twice with request URLs that differ only by a fragment]
expected: FAIL
[Cache.put with a relative URL]
expected: FAIL

View File

@ -1,3 +0,0 @@
[cache-storage-keys.https.html]
type: testharness
prefs: [dom.caches.enabled:true]

View File

@ -1,15 +0,0 @@
[cache-storage-match.https.html]
type: testharness
prefs: [dom.caches.enabled:true]
[CacheStorageMatch with no cache name provided]
expected: FAIL
[CacheStorageMatch from one of many caches]
expected: FAIL
[CacheStorageMatch from one of many caches by name]
expected: FAIL
[CacheStorageMatch a string request]
expected: FAIL

View File

@ -1,3 +0,0 @@
[cache-storage.https.html]
type: testharness
prefs: [dom.caches.enabled:true]