From 2d800f2cb936d2974982248e589a888250b5b604 Mon Sep 17 00:00:00 2001 From: Craig Morten <46491566+asos-craigmorten@users.noreply.github.com> Date: Mon, 24 Aug 2020 19:09:31 +0100 Subject: fix(op_crates/web): throw TypeError on invalid input types in TextDecoder.decode() (#7179) --- op_crates/web/text_encoding_test.js | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'op_crates/web/text_encoding_test.js') diff --git a/op_crates/web/text_encoding_test.js b/op_crates/web/text_encoding_test.js index f741fe409..06537df24 100644 --- a/op_crates/web/text_encoding_test.js +++ b/op_crates/web/text_encoding_test.js @@ -131,6 +131,44 @@ function textDecoderErrorEncoding() { assert(didThrow); } +function textDecoderHandlesUndefined() { + const fixture = undefined; + const decoder = new TextDecoder(); + assert(decoder.decode(fixture) === ""); +} + +function textDecoderThrowsOnEmpty() { + const fixture = ""; + const decoder = new TextDecoder(); + let didThrow = false; + try { + decoder.decode(fixture); + } catch (e) { + didThrow = true; + assert( + e.message === + "Provided input is not of type ArrayBuffer or ArrayBufferView", + ); + } + assert(didThrow); +} + +function textDecoderThrowsOnNull() { + const fixture = null; + const decoder = new TextDecoder(); + let didThrow = false; + try { + decoder.decode(fixture); + } catch (e) { + didThrow = true; + assert( + e.message === + "Provided input is not of type ArrayBuffer or ArrayBufferView", + ); + } + assert(didThrow); +} + function textEncoder() { const fixture = "𝓽𝓮𝔁𝓽"; const encoder = new TextEncoder(); @@ -231,6 +269,9 @@ function main() { textDecoderNotBOM(); textDecoderASCII(); textDecoderErrorEncoding(); + textDecoderHandlesUndefined(); + textDecoderThrowsOnEmpty(); + textDecoderThrowsOnNull(); textEncoder(); textEncodeInto(); textEncodeInto2(); -- cgit v1.2.3