deno.land / std@0.224.0 / json / concatenated_json_parse_stream_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals } from "../assert/mod.ts";import { ConcatenatedJsonParseStream } from "./concatenated_json_parse_stream.ts";import { assertInvalidParse, assertValidParse } from "./_test_common.ts";
Deno.test({ name: "ConcatenatedJsonParseStream", async fn() { await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"}'], [{ foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"} '], [{ foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, [' {"foo": "bar"}'], [{ foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['[{"foo": "bar"}]'], [[{ foo: "bar" }]], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"}{"foo": "bar"}'], [{ foo: "bar" }, { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"} {"foo": "bar"}'], [{ foo: "bar" }, { foo: "bar" }], ); },});
Deno.test({ name: "ConcatenatedJsonParseStream handles primitive", async fn() { await assertValidParse( ConcatenatedJsonParseStream, ["0"], [0], ); await assertValidParse( ConcatenatedJsonParseStream, ["100"], [100], ); await assertValidParse( ConcatenatedJsonParseStream, ['100 200"foo"'], [100, 200, "foo"], ); await assertValidParse( ConcatenatedJsonParseStream, ['100 200{"foo": "bar"}'], [100, 200, { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['100 200["foo"]'], [100, 200, ["foo"]], );
await assertValidParse( ConcatenatedJsonParseStream, ['"foo"'], ["foo"], ); await assertValidParse( ConcatenatedJsonParseStream, ['"foo""bar"{"foo": "bar"}'], ["foo", "bar", { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['"foo""bar"["foo"]'], ["foo", "bar", ["foo"]], ); await assertValidParse( ConcatenatedJsonParseStream, ['"foo""bar"0'], ["foo", "bar", 0], );
await assertValidParse( ConcatenatedJsonParseStream, ["null"], [null], ); await assertValidParse( ConcatenatedJsonParseStream, ['null null{"foo": "bar"}'], [null, null, { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['null null["foo"]'], [null, null, ["foo"]], ); await assertValidParse( ConcatenatedJsonParseStream, ["null null 0"], [null, null, 0], ); await assertValidParse( ConcatenatedJsonParseStream, ['null null"foo"'], [null, null, "foo"], ); await assertValidParse( ConcatenatedJsonParseStream, ["nullnull"], [null, null], ); await assertValidParse( ConcatenatedJsonParseStream, ["nullnull0"], [null, null, 0], ); await assertValidParse( ConcatenatedJsonParseStream, ['nullnull"foo"'], [null, null, "foo"], );
await assertValidParse( ConcatenatedJsonParseStream, ["true"], [true], ); await assertValidParse( ConcatenatedJsonParseStream, ['true true{"foo": "bar"}'], [true, true, { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['true true["foo"]'], [true, true, ["foo"]], ); await assertValidParse( ConcatenatedJsonParseStream, ["true true 0"], [true, true, 0], ); await assertValidParse( ConcatenatedJsonParseStream, ['true true"foo"'], [true, true, "foo"], ); await assertValidParse( ConcatenatedJsonParseStream, ["truetrue"], [true, true], ); await assertValidParse( ConcatenatedJsonParseStream, ["truetrue0"], [true, true, 0], ); await assertValidParse( ConcatenatedJsonParseStream, ['truetrue"foo"'], [true, true, "foo"], );
await assertValidParse( ConcatenatedJsonParseStream, ["false"], [false], ); await assertValidParse( ConcatenatedJsonParseStream, ['false false{"foo": "bar"}'], [false, false, { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['false false["foo"]'], [false, false, ["foo"]], ); await assertValidParse( ConcatenatedJsonParseStream, ["false false 0"], [false, false, 0], ); await assertValidParse( ConcatenatedJsonParseStream, ['false false"foo"'], [false, false, "foo"], ); await assertValidParse( ConcatenatedJsonParseStream, ["falsefalse"], [false, false], ); await assertValidParse( ConcatenatedJsonParseStream, ["falsefalse0"], [false, false, 0], ); await assertValidParse( ConcatenatedJsonParseStream, ['falsefalse"foo"'], [false, false, "foo"], );
await assertValidParse( ConcatenatedJsonParseStream, ['nullfalsetrue0true"foo"falsenullnull'], [null, false, true, 0, true, "foo", false, null, null], ); },});
Deno.test({ name: "ConcatenatedJsonParseStream handles chunk", async fn() { await assertValidParse( ConcatenatedJsonParseStream, ["", '{"foo": "bar"}'], [{ foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ["{", '"foo": "bar"}'], [{ foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "b', 'ar"}'], [{ foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"', "}"], [{ foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"}', ""], [{ foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"}', '{"foo": "bar"}'], [{ foo: "bar" }, { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"', '}{"foo": "bar"}'], [{ foo: "bar" }, { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"}{', '"foo": "bar"}'], [{ foo: "bar" }, { foo: "bar" }], ); await assertValidParse( ConcatenatedJsonParseStream, ["tr", 'ue{"foo": "bar"}'], [true, { foo: "bar" }], ); },});
Deno.test({ name: "ConcatenatedJsonParseStream handles surrogate pair", async fn() { await assertValidParse( ConcatenatedJsonParseStream, ['{"foo": "👪"}{"foo": "👪"}'], [{ foo: "👪" }, { foo: "👪" }], ); },});
Deno.test({ name: "ConcatenatedJsonParseStream handles symbol between double quotes", async fn() { await assertValidParse( ConcatenatedJsonParseStream, ['"[], {}"'], ["[], {}"], ); },});
Deno.test({ name: "ConcatenatedJsonParseStream handles primitives in containers", async fn() { await assertValidParse( ConcatenatedJsonParseStream, ["[ true ]"], [[true]], ); },});
Deno.test({ name: "ConcatenatedJsonParseStream handles halfway chunk", async fn() { await assertInvalidParse( ConcatenatedJsonParseStream, ['{"foo": "bar"} {"foo": '], {}, SyntaxError, `Unexpected end of JSON input (parsing: ' {"foo": ')`, ); },});
Deno.test({ name: "ConcatenatedJsonParseStream truncates error message", async fn() { await assertInvalidParse( ConcatenatedJsonParseStream, [`{${"foo".repeat(100)}}`], {}, SyntaxError, `Expected property name or '}' in JSON at position 1 (line 1 column 2) (parsing: '{foofoofoofoofoofoofoofoofoofo...')`, ); },});
Deno.test({ // Read the test data file name: "parse() handles concatenated-json testdata", async fn() { const url = "./testdata/test.concatenated-json"; const { body } = await fetch(new URL(url, import.meta.url).toString()); const readable = body! .pipeThrough(new TextDecoderStream()) .pipeThrough(new ConcatenatedJsonParseStream());
const result = await Array.fromAsync(readable);
assertEquals(result, [ { "hello": "world" }, ["👋", "👋", "👋"], { "deno": "🦕" }, ]); },});
Version Info