forked from msgpack/msgpack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added
useRawBinaryStrings
option to Decoder to allow override…
… of default UTF-8 behaviour (#3)
- Loading branch information
Showing
3 changed files
with
88 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import assert from "assert"; | ||
import { encode, decode } from "../src"; | ||
import type { DecoderOptions } from "../src"; | ||
|
||
describe("decode with useRawBinaryStrings specified", () => { | ||
const options = { useRawBinaryStrings: true } satisfies DecoderOptions; | ||
|
||
it("decodes string as binary", () => { | ||
const actual = decode(encode("foo"), options); | ||
const expected = Uint8Array.from([0x66, 0x6f, 0x6f]); | ||
assert.deepStrictEqual(actual, expected); | ||
}); | ||
|
||
it("decodes invalid UTF-8 string as binary", () => { | ||
const invalidUtf8String = Uint8Array.from([ | ||
61, 180, 118, 220, 39, 166, 43, 68, 219, 116, 105, 84, 121, 46, 122, 136, 233, 221, 15, 174, 247, 19, 50, 176, | ||
184, 221, 66, 188, 171, 36, 135, 121, | ||
]); | ||
const encoded = Uint8Array.from([ | ||
196, 32, 61, 180, 118, 220, 39, 166, 43, 68, 219, 116, 105, 84, 121, 46, 122, 136, 233, 221, 15, 174, 247, 19, 50, | ||
176, 184, 221, 66, 188, 171, 36, 135, 121, | ||
]); | ||
|
||
const actual = decode(encoded, options); | ||
assert.deepStrictEqual(actual, invalidUtf8String); | ||
}); | ||
|
||
it("decodes object keys as strings", () => { | ||
const actual = decode(encode({ key: "foo" }), options); | ||
const expected = { key: Uint8Array.from([0x66, 0x6f, 0x6f]) }; | ||
assert.deepStrictEqual(actual, expected); | ||
}); | ||
|
||
it("ignores maxStrLength", () => { | ||
const lengthLimitedOptions = { ...options, maxStrLength: 1 } satisfies DecoderOptions; | ||
|
||
const actual = decode(encode("foo"), lengthLimitedOptions); | ||
const expected = Uint8Array.from([0x66, 0x6f, 0x6f]); | ||
assert.deepStrictEqual(actual, expected); | ||
}); | ||
|
||
it("respects maxBinLength", () => { | ||
const lengthLimitedOptions = { ...options, maxBinLength: 1 } satisfies DecoderOptions; | ||
|
||
assert.throws(() => { | ||
decode(encode("foo"), lengthLimitedOptions); | ||
}, /max length exceeded/i); | ||
}); | ||
}); |