Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Morpheus.Tests/SubscriptionInputParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,54 @@ public void ParseRssSources_AcceptsSpaceSeparatedUrls()
Assert.All(parsed, source => Assert.Null(source.DisplayName));
}

[Theory]
[InlineData("https://example.com/feed.xml?tags=release,stable", null)]
[InlineData("https://example.com/feed.xml?tags=release,stable Example feed", "Example feed")]
public void ParseRssSources_PreservesQueryCommasForSingleFeed(string input, string? expectedDisplayName)
{
SubscriptionInputParser.RssSource parsed = Assert.Single(SubscriptionInputParser.ParseRssSources(input));

Assert.Equal("https://example.com/feed.xml?tags=release,stable", parsed.Url);
Assert.Equal(expectedDisplayName, parsed.DisplayName);
}

[Theory]
[InlineData("https://example.com/feed.xml?tags=release,stable")]
[InlineData("https://example.com/feed.xml?tags=release,stable Example feed")]
public void ParseRssSources_PreservesQueryCommasInMultilineInput(string firstSource)
{
IReadOnlyList<SubscriptionInputParser.RssSource> parsed = SubscriptionInputParser.ParseRssSources(
$"{firstSource}\nhttps://example.org/atom.xml");

Assert.Equal(
["https://example.com/feed.xml?tags=release,stable", "https://example.org/atom.xml"],
parsed.Select(source => source.Url));
}

[Fact]
public void ParseRssSources_PreservesQueryCommasInSpaceSeparatedUrls()
{
IReadOnlyList<SubscriptionInputParser.RssSource> parsed = SubscriptionInputParser.ParseRssSources(
"https://example.com/feed.xml?tags=release,stable https://example.org/atom.xml");

Assert.Equal(
["https://example.com/feed.xml?tags=release,stable", "https://example.org/atom.xml"],
parsed.Select(source => source.Url));
}

[Theory]
[InlineData("https://example.com/feed.xml,https://example.org/atom.xml")]
[InlineData("https://example.com/feed.xml , https://example.org/atom.xml")]
[InlineData("https://example.com/feed.xml;https://example.org/atom.xml")]
public void ParseRssSources_AcceptsPunctuationSeparatedUrls(string input)
{
IReadOnlyList<SubscriptionInputParser.RssSource> parsed = SubscriptionInputParser.ParseRssSources(input);

Assert.Equal(
["https://example.com/feed.xml", "https://example.org/atom.xml"],
parsed.Select(source => source.Url));
}

[Fact]
public void ParseRssSources_AcceptsOneFeedPerLineWithOptionalNames()
{
Expand Down
14 changes: 12 additions & 2 deletions Utilities/SubscriptionInputParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static IReadOnlyList<RssSource> ParseRssSources(string input)

if (lines.Length == 1)
{
string[] tokens = SplitTokens(lines[0]).ToArray();
string[] tokens = SplitRssTokens(lines[0]).ToArray();
string[] urls = tokens.Where(IsHttpUrl).ToArray();
if (urls.Length > 1)
return urls
Expand All @@ -56,7 +56,10 @@ public static IReadOnlyList<RssSource> ParseRssSources(string input)
if (separatorIndex < 0)
{
int commaIndex = line.IndexOf(',');
if (commaIndex > 0 && IsHttpUrl(line[..commaIndex].Trim()))
int queryIndex = line.IndexOf('?');
if (commaIndex > 0 &&
(queryIndex < 0 || commaIndex < queryIndex) &&
IsHttpUrl(line[..commaIndex].Trim()))
separatorIndex = commaIndex;
}

Expand Down Expand Up @@ -100,6 +103,10 @@ internal static string RemoveRssFragment(string value)
private static IEnumerable<string> SplitTokens(string input) => input
.Split([' ', '\t', '\r', '\n', ',', ';'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

private static IEnumerable<string> SplitRssTokens(string input) => RssUrlSeparatorRegex()
.Split(input)
.Where(token => !string.IsNullOrWhiteSpace(token));

private static IReadOnlyList<string> Deduplicate(IEnumerable<string> values) => values
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
Expand All @@ -112,4 +119,7 @@ private static bool IsHttpUrl(string value) =>

[GeneratedRegex("^<#(\\d+)>$")]
private static partial Regex ChannelMentionRegex();

[GeneratedRegex(@"(?:\s*[,;]\s*|\s+)(?=https?://)", RegexOptions.IgnoreCase)]
private static partial Regex RssUrlSeparatorRegex();
}