summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Mannehed <samuel@cendio.se>2020-01-02 17:29:41 +0100
committerSamuel Mannehed <samuel@cendio.se>2020-01-02 17:29:41 +0100
commit80c72e92d2b8b1ac7cac552dd6bd431cfedbd3bb (patch)
treed303488fe038962b2143c605dce269f675ee714b
parentcbf090fe701adc7776270be6725f886e185d2b85 (diff)
downloadnovnc-80c72e92d2b8b1ac7cac552dd6bd431cfedbd3bb.tar.gz
Add unit tests for encodeUTF8 and decodeUTF8
-rw-r--r--tests/test.util.js43
1 files changed, 42 insertions, 1 deletions
diff --git a/tests/test.util.js b/tests/test.util.js
index 201acc8..1f670bd 100644
--- a/tests/test.util.js
+++ b/tests/test.util.js
@@ -2,6 +2,7 @@
const expect = chai.expect;
import * as Log from '../core/util/logging.js';
+import { encodeUTF8, decodeUTF8 } from '../core/util/strings.js';
describe('Utils', function () {
"use strict";
@@ -58,8 +59,48 @@ describe('Utils', function () {
});
});
+ describe('string functions', function () {
+ it('should decode UTF-8 to DOMString correctly', function () {
+ // The capital cyrillic letter 'PE' has the hexcode 'd0 9f' in UTF-8
+ const utf8string = String.fromCodePoint(parseInt("d0", 16),
+ parseInt("9f", 16));
+ const domstring = decodeUTF8(utf8string);
+ expect(domstring).to.equal("П");
+ });
+
+ it('should encode DOMString to UTF-8 correctly', function () {
+ const domstring = "åäöa";
+ const utf8string = encodeUTF8(domstring);
+
+ // The hexcode in UTF-8 for 'å' is 'c3 a5'
+ expect(utf8string.codePointAt(0).toString(16)).to.equal("c3");
+ expect(utf8string.codePointAt(1).toString(16)).to.equal("a5");
+ // ä
+ expect(utf8string.codePointAt(2).toString(16)).to.equal("c3");
+ expect(utf8string.codePointAt(3).toString(16)).to.equal("a4");
+ // ö
+ expect(utf8string.codePointAt(4).toString(16)).to.equal("c3");
+ expect(utf8string.codePointAt(5).toString(16)).to.equal("b6");
+ // a
+ expect(utf8string.codePointAt(6).toString(16)).to.equal("61");
+ });
+
+ it('should keep the string intact if encoding to UTF-8 and then decoding', function() {
+ const domstring = "κόσμε";
+ const utf8string = encodeUTF8(domstring);
+ expect(decodeUTF8(utf8string)).to.equal(domstring);
+ });
+
+ it('should ignore URIErrors when UTF-8 decoding if allowLatin1 is set', function() {
+ expect(() => decodeUTF8("�")).to.throw(URIError);
+ expect(() => decodeUTF8("�", true)).to.not.throw(URIError);
+
+ // Only URIError should be ignored
+ expect(() => decodeUTF8(undefVar, true)).to.throw(Error);
+ });
+ });
+
// TODO(directxman12): test the conf_default and conf_defaults methods
- // TODO(directxman12): test decodeUTF8
// TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
// TODO(directxman12): figure out a good way to test getPosition and getEventPosition
// TODO(directxman12): figure out how to test the browser detection functions properly