Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
internal_url: Encode characters which get encoded by urllib.
Pratik Chandaf1b59ec5f56f
diff --git a/web/src/internal_url.ts b/web/src/internal_url.tsindex 79cdae00d84..b698c17e231 100644--- a/web/src/internal_url.ts+++ b/web/src/internal_url.ts@@ -1,7 +1,12 @@type MaybeGetStreamName = (id: number) => string | undefined;+// ' and ! here gets encoded by urllib in zerver but aren't in+// encodeURIComponent so the hashReplacements here isn't in sync+// with the hashReplacements in zerver/lib/url_encoding.py.const hashReplacements = new Map([["%", "."],+ ["!", ".21"],+ ["'", ".27"],["(", ".28"],[")", ".29"],[".", ".2E"],@@ -12,7 +17,7 @@ const hashReplacements = new Map([// by replacing % with . (like MediaWiki).export function encodeHashComponent(str: string): string {return encodeURIComponent(str).replaceAll(- /[%().]/g,+ /[%!'().]/g,(matched) => hashReplacements.get(matched)!,);}diff --git a/web/tests/internal_url.test.cjs b/web/tests/internal_url.test.cjsindex 1ddebaa2a71..ac226285639 100644--- a/web/tests/internal_url.test.cjs+++ b/web/tests/internal_url.test.cjs@@ -2,16 +2,17 @@const assert = require("node:assert/strict");+const url_encoding_test_cases = require("../../zerver/tests/fixtures/url_encoding_test_cases.json");+const {zrequire} = require("./lib/namespace.cjs");const {run_test} = require("./lib/test.cjs");const internal_url = zrequire("internal_url");run_test("test encodeHashComponent", () => {- const decoded = "https://www.zulipexample.com";- const encoded = "https.3A.2F.2Fwww.2Ezulipexample.2Ecom";- const result = internal_url.encodeHashComponent(decoded);- assert.equal(result, encoded);+ for (const test of url_encoding_test_cases) {+ assert.equal(internal_url.encodeHashComponent(test.input), test.expected_output);+ }});run_test("test decodeHashComponent", () => {diff --git a/web/tests/topic_link_util.test.cjs b/web/tests/topic_link_util.test.cjsindex d541279bbc4..55a30fbf485 100644--- a/web/tests/topic_link_util.test.cjs+++ b/web/tests/topic_link_util.test.cjs@@ -181,7 +181,7 @@ run_test("get_topic_link_content_with_stream_name", () => {{label_text_markdown: "#Sweden > a @ 💬",label_text_plain: "#Sweden > a @ 💬",- url: "#narrow/channel/1-Sweden/topic/a!.5Bb.5D.28c.29/near/123",+ url: "#narrow/channel/1-Sweden/topic/a.21.5Bb.5D.28c.29/near/123",},);});diff --git a/zerver/tests/fixtures/url_encoding_test_cases.json b/zerver/tests/fixtures/url_encoding_test_cases.jsonnew file mode 100644index 00000000000..3397bdd827a--- /dev/null+++ b/zerver/tests/fixtures/url_encoding_test_cases.json@@ -0,0 +1,174 @@+[+ {+ "name": "hyphen",+ "input": "-",+ "expected_output": "-"+ },+ {+ "name": "underscore",+ "input": "_",+ "expected_output": "_"+ },+ {+ "name": "tilde",+ "input": "~",+ "expected_output": "~"+ },+ {+ "name": "asterisk",+ "input": "*",+ "expected_output": "*"+ },++ {+ "name": "space",+ "input": " ",+ "expected_output": ".20"+ },+ {+ "name": "exclamation",+ "input": "!",+ "expected_output": ".21"+ },+ {+ "name": "double_quote",+ "input": "\"",+ "expected_output": ".22"+ },+ {+ "name": "hash",+ "input": "#",+ "expected_output": ".23"+ },+ {+ "name": "dollar",+ "input": "$",+ "expected_output": ".24"+ },+ {+ "name": "percent",+ "input": "%",+ "expected_output": ".25"+ },+ {+ "name": "ampersand",+ "input": "&",+ "expected_output": ".26"+ },+ {+ "name": "single_quote",+ "input": "'",+ "expected_output": ".27"+ },+ {+ "name": "open_paren",+ "input": "(",+ "expected_output": ".28"+ },+ {+ "name": "close_paren",+ "input": ")",+ "expected_output": ".29"+ },+ {+ "name": "plus",+ "input": "+",+ "expected_output": ".2B"+ },+ {+ "name": "comma",+ "input": ",",+ "expected_output": ".2C"+ },+ {+ "name": "period",+ "input": ".",+ "expected_output": ".2E"+ },+ {+ "name": "slash",+ "input": "/",+ "expected_output": ".2F"+ },+ {+ "name": "colon",+ "input": ":",+ "expected_output": ".3A"+ },+ {+ "name": "semicolon",+ "input": ";",+ "expected_output": ".3B"+ },+ {+ "name": "less_than",+ "input": "<",+ "expected_output": ".3C"+ },+ {+ "name": "equals",+ "input": "=",+ "expected_output": ".3D"+ },+ {+ "name": "greater_than",+ "input": ">",+ "expected_output": ".3E"+ },+ {+ "name": "question",+ "input": "?",+ "expected_output": ".3F"+ },+ {+ "name": "at",+ "input": "@",+ "expected_output": ".40"+ },+ {+ "name": "open_bracket",+ "input": "[",+ "expected_output": ".5B"+ },+ {+ "name": "backslash",+ "input": "\\",+ "expected_output": ".5C"+ },+ {+ "name": "close_bracket",+ "input": "]",+ "expected_output": ".5D"+ },+ {+ "name": "caret",+ "input": "^",+ "expected_output": ".5E"+ },+ {+ "name": "backtick",+ "input": "`",+ "expected_output": ".60"+ },+ {+ "name": "open_brace",+ "input": "{",+ "expected_output": ".7B"+ },+ {+ "name": "pipe",+ "input": "|",+ "expected_output": ".7C"+ },+ {+ "name": "close_brace",+ "input": "}",+ "expected_output": ".7D"+ },++ {+ "name": "simple_url",+ "input": "https://www.zulipexample.com",+ "expected_output": "https.3A.2F.2Fwww.2Ezulipexample.2Ecom"+ }+]diff --git a/zerver/tests/test_url_encoding.py b/zerver/tests/test_url_encoding.pyindex d657a450231..72e9bd4455e 100644--- a/zerver/tests/test_url_encoding.py+++ b/zerver/tests/test_url_encoding.py@@ -1,3 +1,7 @@+import os++import orjson+from zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.url_encoding import (encode_channel,@@ -12,6 +16,16 @@ from zerver.models.streams import get_streamclass URLEncodeTest(ZulipTestCase):+ def test_encode_hash_component(self) -> None:+ fixture_path = os.path.join(+ os.path.dirname(__file__), "fixtures/url_encoding_test_cases.json"+ )+ with open(fixture_path, "rb") as f:+ data = orjson.loads(f.read())+ for test in data:+ with self.subTest(name=test["name"]):+ self.assertEqual(encode_hash_component(test["input"]), test["expected_output"])+def test_encode_channel(self) -> None:# We have more tests for this function in `test_topic_link_utils.py`self.assertEqual(encode_channel(9, "Verona"), "9-Verona")