From fb51270f873df204c1ed30d1045533d81c03083f Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Sun, 13 Feb 2022 21:07:50 -0500 Subject: [PATCH] Fix article ID parsing bug Since the article ID regular expression wasn't anchored to the end of the URL, it would grab characters after a / or - that were hex characters. For example /@user/bacon-123abc would just grab `bac`. Not great. This anchors the ID at the end of the string so that it will be more likely to catch IDs. --- CHANGELOG | 1 + spec/classes/article_id_parser_spec.cr | 8 ++++++++ src/classes/article_id_parser.cr | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 035d9ce..2334586 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ * Better article ID parsing * Link to full Medium URL on error page +* Fix article ID parsing bug 2022-02-12 diff --git a/spec/classes/article_id_parser_spec.cr b/spec/classes/article_id_parser_spec.cr index ec91493..2baef20 100644 --- a/spec/classes/article_id_parser_spec.cr +++ b/spec/classes/article_id_parser_spec.cr @@ -14,6 +14,14 @@ describe ArticleIdParser do result.should eq(Monads::Just.new("111111abcdef")) end + it "parses the post id for urls with hex characters after a /" do + request = resource_request("/@user/bacon-abcdef123456") + + result = ArticleIdParser.parse(request) + + result.should eq(Monads::Just.new("abcdef123456")) + end + it "parses the post id for urls like /user/:post_slug" do request = resource_request("/user/my-post-222222abcdef") diff --git a/src/classes/article_id_parser.cr b/src/classes/article_id_parser.cr index 524be6b..8693ea6 100644 --- a/src/classes/article_id_parser.cr +++ b/src/classes/article_id_parser.cr @@ -1,7 +1,7 @@ class ArticleIdParser include Monads - ID_REGEX = /[\/\-]([0-9a-f]+)/i + ID_REGEX = /[\/\-]([0-9a-f]+)$/i def self.parse(request : HTTP::Request) new.parse(request)