Skip to content

Commit

Permalink
Improve parsing of question mark after forward slash
Browse files Browse the repository at this point in the history
  • Loading branch information
shaan1337 committed Jul 6, 2020
1 parent f4ffa9c commit bfad201
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ public static EventStoreClientSettings Parse(string connectionString) {

var slashIndex = connectionString.IndexOf(Slash, currentIndex, StringComparison.Ordinal);
if (slashIndex == -1)
throw new ConnectionStringParseException("The connection string must contain a / (forward slash) after specifying the hosts");
throw new ConnectionStringParseException("The connection string must contain a forward slash (/) after specifying the hosts");

var hosts = ParseHosts(connectionString.Substring(currentIndex, slashIndex - currentIndex));
currentIndex = slashIndex + Slash.Length;

var questionMarkIndex = connectionString.IndexOf(QuestionMark, currentIndex);
var questionMarkIndex = connectionString.IndexOf(QuestionMark, currentIndex, StringComparison.Ordinal);
if ((questionMarkIndex == -1 && slashIndex != connectionString.Length - Slash.Length)
|| (questionMarkIndex != -1 && questionMarkIndex != currentIndex))
throw new ConnectionStringParseException($"The connection string may contain only a question mark (?) after specifying the hosts and a forward slash (/) but the following character was found: '{connectionString[currentIndex]}'");

var options = new Dictionary<string, string>();
if (questionMarkIndex != -1) {
currentIndex = questionMarkIndex + QuestionMark.Length;
Expand Down
16 changes: 16 additions & 0 deletions test/EventStore.Client.Tests/ConnectionStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ public void connection_string_with_invalid_host_should_throw() {
});
}

[Fact]
public void connection_string_with_non_question_mark_character_after_forward_slash_should_throw() {
Assert.Throws<ConnectionStringParseException>(() => {
EventStoreClientSettings.Create("esdb://user:pass@127.0.0.1/maxDiscoverAttempts=10");
});

Assert.Throws<ConnectionStringParseException>(() => {
EventStoreClientSettings.Create("esdb://user:pass@127.0.0.1/hello?maxDiscoverAttempts=10");
});
}

[Fact]
public void connection_string_with_no_key_value_pairs_specified_should_not_throw() {
EventStoreClientSettings.Create("esdb://user:pass@127.0.0.1/");
}

[Fact]
public void connection_string_with_invalid_key_value_pair_should_throw() {
Assert.Throws<InvalidKeyValuePairException>(() => {
Expand Down

0 comments on commit bfad201

Please sign in to comment.