Skip to content

Commit 53cd4ee

Browse files
committed
refactor(fetch): use idiomatic downcast for Headers iterator
1 parent 12e3fa2 commit 53cd4ee

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

core/runtime/src/fetch/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,13 @@ fn headers_symbol_iterator(
213213
|| js_error!(TypeError: "`Headers.prototype[Symbol.iterator]` requires a `Headers` object"),
214214
)?;
215215

216-
if !this_object.is::<JsHeaders>() {
216+
let Ok(headers) = this_object.clone().downcast::<JsHeaders>() else {
217217
return Err(
218218
js_error!(TypeError: "`Headers.prototype[Symbol.iterator]` requires a `Headers` object"),
219219
);
220-
}
220+
};
221221

222-
HeadersIterator::create_headers_iterator(
223-
this_object.clone().downcast().expect("checked above"),
224-
IterationKind::KeyAndValue,
225-
context,
226-
)
222+
HeadersIterator::create_headers_iterator(headers, IterationKind::KeyAndValue, context)
227223
}
228224

229225
/// Register the `fetch` function in the realm, as well as ALL supporting classes.

core/runtime/src/fetch/tests/headers.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,30 @@ fn headers_invalid_inputs_throw_type_error_objects() {
119119
),
120120
]);
121121
}
122+
123+
#[test]
124+
fn headers_iterator_throws_on_invalid_this() {
125+
run_test_actions([
126+
TestAction::harness(),
127+
TestAction::inspect_context(register),
128+
TestAction::run(
129+
r#"
130+
try {
131+
const iterator = Headers.prototype[Symbol.iterator].call({});
132+
throw Error("expected the call above to throw");
133+
} catch (e) {
134+
assert(e instanceof TypeError);
135+
assertEq(e.message, "`Headers.prototype[Symbol.iterator]` requires a `Headers` object");
136+
}
137+
138+
try {
139+
const iterator = Headers.prototype[Symbol.iterator].call(1);
140+
throw Error("expected the call above to throw");
141+
} catch (e) {
142+
assert(e instanceof TypeError);
143+
assertEq(e.message, "`Headers.prototype[Symbol.iterator]` requires a `Headers` object");
144+
}
145+
"#,
146+
),
147+
]);
148+
}

0 commit comments

Comments
 (0)