diff --git a/Burr.Tests/Burr.Tests.csproj b/Burr.Tests/Burr.Tests.csproj index 001a7fd9..b5762003 100644 --- a/Burr.Tests/Burr.Tests.csproj +++ b/Burr.Tests/Burr.Tests.csproj @@ -56,6 +56,7 @@ + diff --git a/Burr.Tests/Http/ConnectionTests.cs b/Burr.Tests/Http/ConnectionTests.cs new file mode 100644 index 00000000..2e91a992 --- /dev/null +++ b/Burr.Tests/Http/ConnectionTests.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Burr.Http; +using Xunit; +using Xunit.Extensions; +using FluentAssertions; +using Moq; + +namespace Burr.Tests.Http +{ + public class ConnectionTests + { + const string ExampleUrl = "http://example.com"; + static Uri ExampleUri = new Uri(ExampleUrl); + + public class TheConstructor + { + [Fact] + public void ThrowsForBadArguments() + { + Assert.Throws(() => new Connection(null)); + } + + [Fact] + public void DefaultsToStandardImplementations() + { + var c = new Connection(ExampleUri); + + c.Builder.Should().BeOfType(); + } + + [Fact] + public void CanUseCustomBuilder() + { + var builder = new Mock(); + builder.Setup(x => x.Run(It.IsAny())).Returns(Mock.Of()); + var c = new Connection(ExampleUri); + + c.Builder = builder.Object; + + c.App.Should().NotBeNull(); + builder.Verify(x => x.Run(It.IsAny())); + } + + [Fact] + public void CanUseCustomMiddlewareStack() + { + var app = new Mock(); + var c = new Connection(ExampleUri); + + c.MiddlewareStack = builder => builder.Run(app.Object); + + c.App.Should().NotBeNull(); + c.App.Should().Be(app.Object); + } + } + + public class TheGetAsyncMethod + { + [Fact] + public async Task RunsConfigureAppWithAppropriateEnv() + { + var app = new Mock(); + app.Setup(x => x.Call(It.IsAny>())).Returns(Task.FromResult(app.Object)); + var c = new Connection(ExampleUri); + c.MiddlewareStack = builder => builder.Run(app.Object); + + var res = await c.GetAsync("/endpoint"); + + app.Verify(p => p.Call(It.Is>(x => + x.Request.BaseAddress == ExampleUri && + x.Request.Method == "GET" && + x.Request.Endpoint == "/endpoint")), Times.Once()); + } + + [Fact] + public async Task CanMakeMutipleRequestsWithSameConnection() + { + var app = new Mock(); + app.Setup(x => x.Call(It.IsAny>())).Returns(Task.FromResult(app.Object)); + var c = new Connection(ExampleUri); + c.MiddlewareStack = builder => builder.Run(app.Object); + + var res = await c.GetAsync("/endpoint"); + res = await c.GetAsync("/endpoint"); + res = await c.GetAsync("/endpoint"); + + app.Verify(p => p.Call(It.Is>(x => + x.Request.BaseAddress == ExampleUri && + x.Request.Method == "GET" && + x.Request.Endpoint == "/endpoint")), Times.Exactly(3)); + } + } + } +} diff --git a/Burr.Tests/Http/ResponseHandlerTests.cs b/Burr.Tests/Http/ResponseHandlerTests.cs index dcd43db9..54eecf80 100644 --- a/Burr.Tests/Http/ResponseHandlerTests.cs +++ b/Burr.Tests/Http/ResponseHandlerTests.cs @@ -23,6 +23,7 @@ namespace Burr.Tests.Http protected override void Before(Env env) { + base.Before(env); BeforeWasCalled = true; } diff --git a/Burr/Http/Connection.cs b/Burr/Http/Connection.cs index 06957c31..da992bc0 100644 --- a/Burr/Http/Connection.cs +++ b/Burr/Http/Connection.cs @@ -1,14 +1,19 @@ using System; using System.Threading.Tasks; +using Burr.Helpers; namespace Burr.Http { public class Connection : IConnection { + static readonly Func defaultStack = builder => { return builder.Run(new HttpClientAdapter()); }; + Uri baseAddress; public Connection(Uri baseAddress) { + Ensure.ArgumentNotNull(baseAddress, "baseAddress"); + this.baseAddress = baseAddress; } @@ -57,7 +62,6 @@ namespace Burr.Http } } - static readonly Func defaultStack = builder => { return builder.Run(new HttpClientAdapter()); }; Func middlewareStack; public Func MiddlewareStack {