From 5f9014125dd326b95b767ffa5f6e4f0738169ea8 Mon Sep 17 00:00:00 2001 From: Menno van Lavieren Date: Mon, 26 Jun 2017 11:43:55 +0200 Subject: [PATCH] Finished the Connectionstring Parser with extensive unit tests --- Tapeti.Tests/ConnectionStringParser.cs | 200 +++++++++++++++++++++++ Tapeti.Tests/Tapeti.Tests.csproj | 13 ++ Tapeti.Tests/packages.config | 1 + Tapeti/Helpers/ConnectionstringParser.cs | 35 ++-- Test/Test.csproj | 9 + Test/packages.config | 1 + 6 files changed, 245 insertions(+), 14 deletions(-) create mode 100644 Tapeti.Tests/ConnectionStringParser.cs diff --git a/Tapeti.Tests/ConnectionStringParser.cs b/Tapeti.Tests/ConnectionStringParser.cs new file mode 100644 index 0000000..d56e95f --- /dev/null +++ b/Tapeti.Tests/ConnectionStringParser.cs @@ -0,0 +1,200 @@ +using System; +using Tapeti; +using Tapeti.Helpers; +using Xunit; + +namespace Tapet.Tests +{ + // ReSharper disable InconsistentNaming + public class ConnectionStringParserTest + { + [Fact] + public void EmptyConnectionString() + { + AssertConnectionString("", new TapetiConnectionParams()); + } + + [Fact] + public void SimpleHostname() + { + AssertConnectionString("HostName=rabbit.com", new TapetiConnectionParams + { + HostName = "rabbit.com" + }); + } + + + [Fact] + public void LongHostname() + { + AssertConnectionString("HostName=rabbit.1234.internal.prod.Uname-IT.com", new TapetiConnectionParams + { + HostName = "rabbit.1234.internal.prod.Uname-IT.com" + }); + } + + [Fact] + public void HostnameAndPort() + { + AssertConnectionString("HostName=rabbit.com;Port=60001", new TapetiConnectionParams + { + HostName = "rabbit.com", + Port = 60001 + }); + } + + [Fact] + public void HostnameAndVirtualHost() + { + AssertConnectionString("HostName=rabbit.com;VirtualHost=/staging", new TapetiConnectionParams + { + HostName = "rabbit.com", + VirtualHost = "/staging" + }); + } + + [Fact] + public void HostnameAndPrefetchCount() + { + AssertConnectionString("HostName=rabbit.com;PrefetchCount=5", new TapetiConnectionParams + { + HostName = "rabbit.com", + PrefetchCount = 5 + }); + } + + [Fact] + public void HostnameAndUsername() + { + AssertConnectionString("HostName=rabbit.com;Username=myApplication", new TapetiConnectionParams + { + HostName = "rabbit.com", + Username = "myApplication" + }); + } + + [Fact] + public void HostnameAndUsernameAndPassword() + { + AssertConnectionString("HostName=rabbit.com;Username=myApplication;Password=abcd1234", new TapetiConnectionParams + { + HostName = "rabbit.com", + Username = "myApplication", + Password = "abcd1234" + }); + } + + [Fact] + public void HostnameAndUsernameAndFunnyPassword() + { + AssertConnectionString("HostName=rabbit.com;Username=myApplication;Password=a#$%h^5Gs!`~EE357(-+)=_\\/&*|6$<>{}[]MyFaFoRiTePeTnAmE", new TapetiConnectionParams + { + HostName = "rabbit.com", + Username = "myApplication", + Password = "a#$%h^5Gs!`~EE357(-+)=_\\/&*|6$<>{}[]MyFaFoRiTePeTnAmE" + }); + } + + [Fact] + public void ExtraSemicolon() + { + AssertConnectionString("HostName=rabbit.com;Username=myApplication;Password=abcd1234;", new TapetiConnectionParams + { + HostName = "rabbit.com", + Username = "myApplication", + Password = "abcd1234" + }); + } + + [Fact] + public void ExtraMultipleSemicolon() + { + AssertConnectionString("HostName=rabbit.com;;;;;;;;;;;;;;Username=myApplication;Password=abcd1234;;;", new TapetiConnectionParams + { + HostName = "rabbit.com", + Username = "myApplication", + Password = "abcd1234" + }); + } + + [Fact] + public void OnlySemicolons() + { + AssertConnectionString(";;;", new TapetiConnectionParams + { + }); + } + + [Fact] + public void DoubleQuotes() + { + AssertConnectionString("HostName=rabbit.com;Username=\"myApplication\"", new TapetiConnectionParams + { + HostName = "rabbit.com", + Username = "myApplication", + }); + } + + [Fact] + public void DoubleQuotesInDoubleQuotes() + { + AssertConnectionString("HostName=rabbit.com;Username=\"\"\"myApplication\"\"\";Password=abcd1234", new TapetiConnectionParams + { + HostName = "rabbit.com", + Username = "\"myApplication\"", + Password = "abcd1234" + }); + } + + [Fact] + public void CharactersInDoubleQuotes() + { + AssertConnectionString("HostName=rabbit.com;Username=\"Application?culture=\"\"nl-NL\"\";nr=5;\";Password=abcd1234", new TapetiConnectionParams + { + HostName = "rabbit.com", + Username = "Application?culture=\"nl-NL\";nr=5;", + Password = "abcd1234" + }); + } + + [Fact] + public void DifferentOrder() + { + AssertConnectionString("Username=myApplication;PrefetchCount=10;Password=1234;Port=80;VirtualHost=/test;HostName=messagebus.universe.local", new TapetiConnectionParams + { + HostName = "messagebus.universe.local", + Port = 80, + VirtualHost= "/test", + Username = "myApplication", + Password = "1234", + PrefetchCount= 10 + }); + } + + [Fact] + public void DifferentOrderAndQuotes() + { + AssertConnectionString("Port=80;Username=myApplication;PrefetchCount=\"10\";Password=\"ab\"\"cd\";VirtualHost=\"/test\";HostName=\"messagebus.universe.local\"", new TapetiConnectionParams + { + HostName = "messagebus.universe.local", + Port = 80, + VirtualHost = "/test", + Username = "myApplication", + Password = "ab\"cd", + PrefetchCount = 10 + }); + } + + private void AssertConnectionString(string connectionstring, TapetiConnectionParams expected) + { + var parsed = ConnectionStringParser.Parse(connectionstring); + + Assert.Equal(expected.HostName, parsed.HostName); + Assert.Equal(expected.Port, parsed.Port); + Assert.Equal(expected.VirtualHost, parsed.VirtualHost); + Assert.Equal(expected.Username, parsed.Username); + Assert.Equal(expected.Password, parsed.Password); + Assert.Equal(expected.PrefetchCount, parsed.PrefetchCount); + } + } +} diff --git a/Tapeti.Tests/Tapeti.Tests.csproj b/Tapeti.Tests/Tapeti.Tests.csproj index 4cbecb5..e6d35b5 100644 --- a/Tapeti.Tests/Tapeti.Tests.csproj +++ b/Tapeti.Tests/Tapeti.Tests.csproj @@ -1,5 +1,6 @@  + Debug @@ -12,6 +13,8 @@ v4.6.1 512 + + true @@ -57,6 +60,7 @@ + @@ -69,7 +73,16 @@ Tapeti + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + +