scribe/spec/classes/article_id_parser_spec.cr
Edward Loveall e86108e18f
Rearrange article id parsing to be more reliable
The article ID parser looks for a string at the end of a URL path with
a bunch of hex digits. But it also has to handle user, tag, and search
URLs.

* /@ba5eba11
* /tag/0ddba11
* /search?q=ba5eba11

Some URLs are encoded as params. The parser used to look at the result
of the path first, then the params. But paths that ended in
`global-identity-2` messed that up because `2` is a hex digit at the
end of the path. This changes the logic to parse params first and paths
second which gets around this.
2023-03-25 16:32:37 -04:00

112 lines
3.1 KiB
Crystal

require "../spec_helper"
def resource_request(resource : String)
headers = HTTP::Headers{"Host" => "example.com"}
HTTP::Request.new("GET", resource, headers)
end
describe ArticleIdParser do
it "parses the post id for urls like /@user/:post_slug" do
request = resource_request("/@user/my-post-111111abcdef")
result = ArticleIdParser.parse(request)
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")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Just.new("222222abcdef"))
end
it "parses the post id for urls like /p/:post_slug" do
request = resource_request("/p/my-post-333333abcdef")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Just.new("333333abcdef"))
end
it "parses the post id for urls like /posts/:post_slug" do
request = resource_request("/posts/my-post-444444abcdef")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Just.new("444444abcdef"))
end
it "parses the post id for urls like /p/:post_id" do
request = resource_request("/p/555555abcdef")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Just.new("555555abcdef"))
end
it "parses the post id for urls like /:post_slug" do
request = resource_request("/my-post-666666abcdef")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Just.new("666666abcdef"))
end
it "parses the post id for urls like /https:/medium.com/@user/:post_slug" do
request = resource_request("/https:/medium.com/@user/777777abcdef")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Just.new("777777abcdef"))
end
it "parses the post id for global identity redirects" do
request = resource_request("/m/global-identity?redirectUrl=https%3A%2F%2Fexample.com%2Fmy-post-888888abcdef")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Just.new("888888abcdef"))
end
it "parses the post id for global identity 2 redirects" do
request = resource_request("/m/global-identity-2?redirectUrl=https%3A%2F%2Fexample.com%2Fmy-post-999999abcdef")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Just.new("999999abcdef"))
end
it "returns Nothing if path is a username" do
request = resource_request("/@ba5eba11")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Nothing(String).new)
end
it "returns Nothing if path is a tag" do
request = resource_request("/tag/0ddba11")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Nothing(String).new)
end
it "returns Nothing if path is search result" do
request = resource_request("/search?q=ba5eba11")
result = ArticleIdParser.parse(request)
result.should eq(Monads::Nothing(String).new)
end
end