From d79b526d70a98a350bf57f9dd7747803ac656cb0 Mon Sep 17 00:00:00 2001 From: Haacked Date: Fri, 4 Oct 2013 09:47:28 -0700 Subject: [PATCH] Ensure user agent not empty string Also added more ctor tests --- Octokit.Tests/Http/ConnectionTests.cs | 99 +++++++++++++++++++++------ Octokit/Http/Connection.cs | 2 +- 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/Octokit.Tests/Http/ConnectionTests.cs b/Octokit.Tests/Http/ConnectionTests.cs index 0edbee4e..e451c27c 100644 --- a/Octokit.Tests/Http/ConnectionTests.cs +++ b/Octokit.Tests/Http/ConnectionTests.cs @@ -9,7 +9,6 @@ using NSubstitute; using Octokit.Http; using Octokit.Tests.Helpers; using Xunit; -using Xunit.Extensions; namespace Octokit.Tests.Http { @@ -18,25 +17,6 @@ namespace Octokit.Tests.Http const string ExampleUrl = "http://example.com"; static readonly Uri ExampleUri = new Uri(ExampleUrl); - public class TheConstructor - { - [Fact] - public void EnsuresAbsoluteBaseAddress() - { - Assert.Throws(() => new Connection("Test Runner", new Uri("/foo", UriKind.Relative))); - Assert.Throws(() => new Connection("Test Runner", new Uri("/foo", UriKind.RelativeOrAbsolute))); - } - - [Fact] - public void CreatesConnectionWithBaseAddress() - { - var connection = new Connection("Test Runner User Agent", new Uri("https://github.com/")); - - Assert.Equal(new Uri("https://github.com/"), connection.BaseAddress); - Assert.Equal("Test Runner User Agent", connection.UserAgent); - } - } - public class TheGetAsyncMethod { [Fact] @@ -271,5 +251,84 @@ namespace Octokit.Tests.Http req.Endpoint == new Uri("/endpoint", UriKind.Relative))); } } + + public class TheConstructor + { + [Fact] + public void EnsuresAbsoluteBaseAddress() + { + Assert.Throws(() => new Connection("Test Runner", new Uri("/foo", UriKind.Relative))); + Assert.Throws(() => new Connection("Test Runner", new Uri("/foo", UriKind.RelativeOrAbsolute))); + } + + [Fact] + public void EnsuresNonNullArguments() + { + // 1 arg + Assert.Throws(() => new Connection(null)); + Assert.Throws(() => new Connection("")); + + + // 2 args + Assert.Throws(() => new Connection(null, new Uri("https://example.com"))); + Assert.Throws(() => new Connection("", new Uri("https://example.com"))); + Assert.Throws(() => new Connection("foo", (Uri)null)); + + // 3 args + Assert.Throws(() => new Connection("", + new Uri("https://example.com"), + Substitute.For())); + Assert.Throws(() => new Connection(null, + new Uri("https://example.com"), + Substitute.For())); + Assert.Throws(() => new Connection("foo", + null, + Substitute.For())); + Assert.Throws(() => new Connection("foo", + new Uri("https://example.com"), + null)); + + // 5 Args + Assert.Throws(() => new Connection("" + , new Uri("https://example.com"), + Substitute.For(), + Substitute.For(), + Substitute.For())); + Assert.Throws(() => new Connection(null + , new Uri("https://example.com"), + Substitute.For(), + Substitute.For(), + Substitute.For())); + Assert.Throws(() => new Connection("foo", + new Uri("https://example.com"), + Substitute.For(), + Substitute.For(), + null)); + Assert.Throws(() => new Connection("foo", + new Uri("https://example.com"), + Substitute.For(), + null, + Substitute.For())); + Assert.Throws(() => new Connection("foo", + new Uri("https://example.com"), + null, + Substitute.For(), + Substitute.For())); + Assert.Throws(() => new Connection("foo", + null, + Substitute.For(), + Substitute.For(), + Substitute.For())); + } + + [Fact] + public void CreatesConnectionWithBaseAddress() + { + var connection = new Connection("Test Runner User Agent", new Uri("https://github.com/")); + + Assert.Equal(new Uri("https://github.com/"), connection.BaseAddress); + Assert.Equal("Test Runner User Agent", connection.UserAgent); + } + } } } diff --git a/Octokit/Http/Connection.cs b/Octokit/Http/Connection.cs index e32933df..e76c9fe5 100644 --- a/Octokit/Http/Connection.cs +++ b/Octokit/Http/Connection.cs @@ -44,7 +44,7 @@ namespace Octokit.Http IHttpClient httpClient, IJsonSerializer serializer) { - Ensure.ArgumentNotNull(userAgent, "userAgent"); + Ensure.ArgumentNotNullOrEmptyString(userAgent, "userAgent"); Ensure.ArgumentNotNull(baseAddress, "baseAddress"); Ensure.ArgumentNotNull(credentialStore, "credentialStore"); Ensure.ArgumentNotNull(httpClient, "httpClient");