From d5cdb37a8f06128e472f1ef13ec4d7823f956e7d Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Sun, 2 Feb 2025 02:22:59 +0200 Subject: [PATCH] fix: route escaping round three --- deno/utils/internals.ts | 3 ++- tests/v10/routes-escaping.ts | 24 +++++++++++++++++++----- utils/internals.ts | 3 ++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/deno/utils/internals.ts b/deno/utils/internals.ts index 2aa42506..3b341e75 100644 --- a/deno/utils/internals.ts +++ b/deno/utils/internals.ts @@ -40,7 +40,8 @@ export type DistributiveOmit> = type Omit_ = Omit>; -const pattern = /^[\d%A-Za-z-]+$/g; +// eslint-disable-next-line unicorn/better-regex +const pattern = /^[\d%A-Za-z-_]+$/g; export const urlSafeCharacters = { test(input: string) { diff --git a/tests/v10/routes-escaping.ts b/tests/v10/routes-escaping.ts index a38a216b..683696f8 100644 --- a/tests/v10/routes-escaping.ts +++ b/tests/v10/routes-escaping.ts @@ -1,12 +1,26 @@ import { strictEqual } from 'node:assert'; import { Routes } from '../../v10'; -const expected = '/channels/1320466398597615666/messages/1320622300642545705/reactions/%F0%9F%95%99/@me'; +const channelId = '1320466398597615666'; +const messageId = '1320622300642545705'; -strictEqual(Routes.channelMessageOwnReaction('1320466398597615666', '1320622300642545705', '%F0%9F%95%99'), expected); -strictEqual(Routes.channelMessageOwnReaction('1320466398597615666', '1320622300642545705', '%F0%9F%95%99'), expected); -strictEqual(Routes.channelMessageOwnReaction('1320466398597615666', '1320622300642545705', '%F0%9F%95%99'), expected); +const expected = `/channels/${channelId}/messages/${messageId}/reactions/%F0%9F%95%99/@me`; +// Run multiple times to ensure that the validation isn't intermittent +strictEqual(Routes.channelMessageOwnReaction(channelId, messageId, '%F0%9F%95%99'), expected); +strictEqual(Routes.channelMessageOwnReaction(channelId, messageId, '%F0%9F%95%99'), expected); +strictEqual(Routes.channelMessageOwnReaction(channelId, messageId, '%F0%9F%95%99'), expected); + +// make sure that the emoji is properly encoded const emoji = '🕙'; -strictEqual(Routes.channelMessageOwnReaction('1320466398597615666', '1320622300642545705', emoji), expected); +strictEqual(Routes.channelMessageOwnReaction(channelId, messageId, emoji), expected); + +// test custom emojis too +const animated = '1_:1234567890'; +const encodedExpected = '1_%3A1234567890'; + +const expected2 = `/channels/${channelId}/messages/${messageId}/reactions/${encodedExpected}/@me`; + +strictEqual(Routes.channelMessageOwnReaction(channelId, messageId, animated), expected2); +strictEqual(Routes.channelMessageOwnReaction(channelId, messageId, encodedExpected), expected2); diff --git a/utils/internals.ts b/utils/internals.ts index 2aa42506..3b341e75 100644 --- a/utils/internals.ts +++ b/utils/internals.ts @@ -40,7 +40,8 @@ export type DistributiveOmit> = type Omit_ = Omit>; -const pattern = /^[\d%A-Za-z-]+$/g; +// eslint-disable-next-line unicorn/better-regex +const pattern = /^[\d%A-Za-z-_]+$/g; export const urlSafeCharacters = { test(input: string) {