From bc518f0c4fa0a36c86cbf45a317abfa6518c6d60 Mon Sep 17 00:00:00 2001 From: Chandu Date: Wed, 26 Mar 2014 17:43:19 -0400 Subject: [PATCH 1/4] Added System.Net.Http library and removed the WebRequest dependency. --- .../Cucumber.SimpleDb.Test.csproj | 46 +++++++++-- .../Fakes/FakeMessageHandler.cs | 29 +++++++ .../Transport/AwsRestServiceTest.cs | 24 +++++- .../Transport/CaptureUriRequestProvider.cs | 45 ----------- source/Cucumber.SimpleDb.Test/app.config | 11 +++ source/Cucumber.SimpleDb.Test/packages.config | 3 + .../Cucumber.SimpleDb.csproj | 45 ++++++++++- .../Extensions/HttpMethodExtensions.cs | 21 +++++ source/Cucumber.SimpleDb/SimpleDbContext.cs | 2 +- .../Transport/AwsRestService.cs | 81 +++++++++++-------- .../Transport/IWebRequestProvider.cs | 11 --- .../Transport/WebRequestProvider.cs | 14 ---- source/Cucumber.SimpleDb/app.config | 11 +++ source/Cucumber.SimpleDb/packages.config | 6 ++ 14 files changed, 231 insertions(+), 118 deletions(-) create mode 100644 source/Cucumber.SimpleDb.Test/Fakes/FakeMessageHandler.cs delete mode 100644 source/Cucumber.SimpleDb.Test/Transport/CaptureUriRequestProvider.cs create mode 100644 source/Cucumber.SimpleDb.Test/app.config create mode 100644 source/Cucumber.SimpleDb/Extensions/HttpMethodExtensions.cs delete mode 100644 source/Cucumber.SimpleDb/Transport/IWebRequestProvider.cs delete mode 100644 source/Cucumber.SimpleDb/Transport/WebRequestProvider.cs create mode 100644 source/Cucumber.SimpleDb/app.config create mode 100644 source/Cucumber.SimpleDb/packages.config diff --git a/source/Cucumber.SimpleDb.Test/Cucumber.SimpleDb.Test.csproj b/source/Cucumber.SimpleDb.Test/Cucumber.SimpleDb.Test.csproj index 26496d9..5d73b49 100644 --- a/source/Cucumber.SimpleDb.Test/Cucumber.SimpleDb.Test.csproj +++ b/source/Cucumber.SimpleDb.Test/Cucumber.SimpleDb.Test.csproj @@ -33,6 +33,29 @@ + + ..\packages\Microsoft.Bcl.1.1.3\lib\net40\System.IO.dll + + + False + ..\packages\Microsoft.Net.Http.2.2.18\lib\net40\System.Net.Http.dll + + + ..\packages\Microsoft.Net.Http.2.2.18\lib\net40\System.Net.Http.Extensions.dll + + + ..\packages\Microsoft.Net.Http.2.2.18\lib\net40\System.Net.Http.Primitives.dll + + + False + ..\packages\Microsoft.Net.Http.2.2.18\lib\net40\System.Net.Http.WebRequest.dll + + + ..\packages\Microsoft.Bcl.1.1.3\lib\net40\System.Runtime.dll + + + ..\packages\Microsoft.Bcl.1.1.3\lib\net40\System.Threading.Tasks.dll + @@ -43,6 +66,7 @@ + @@ -56,7 +80,6 @@ - @@ -74,14 +97,21 @@ + - - - - - - - + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + + \ No newline at end of file diff --git a/source/Cucumber.SimpleDb.Test/Fakes/FakeMessageHandler.cs b/source/Cucumber.SimpleDb.Test/Fakes/FakeMessageHandler.cs new file mode 100644 index 0000000..4a037f7 --- /dev/null +++ b/source/Cucumber.SimpleDb.Test/Fakes/FakeMessageHandler.cs @@ -0,0 +1,29 @@ +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace Cucumber.SimpleDb.Test.Fakes +{ + //http://stackoverflow.com/questions/10693955/stubbing-or-mocking-asp-net-web-api-httpclient + internal class FakeHttpMessageHandler : HttpMessageHandler + { + private readonly HttpResponseMessage _responseMessage; + private readonly Action _requestFilter; + + + public FakeHttpMessageHandler(HttpResponseMessage responseMessage, Action requestFilter) + { + _responseMessage = responseMessage; + _requestFilter = requestFilter; + } + + public HttpRequestMessage RequestMessage { get; private set; } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + _requestFilter(request); + return Task.Factory.StartNew(() => _responseMessage, cancellationToken); + } + } +} \ No newline at end of file diff --git a/source/Cucumber.SimpleDb.Test/Transport/AwsRestServiceTest.cs b/source/Cucumber.SimpleDb.Test/Transport/AwsRestServiceTest.cs index dbc1889..cbcc0ad 100644 --- a/source/Cucumber.SimpleDb.Test/Transport/AwsRestServiceTest.cs +++ b/source/Cucumber.SimpleDb.Test/Transport/AwsRestServiceTest.cs @@ -1,24 +1,40 @@ -using NUnit.Framework; -using System; +using System.Net; +using System.Net.Http; +using Cucumber.SimpleDb.Test.Fakes; +using NUnit.Framework; using System.Collections.Specialized; using Cucumber.SimpleDb.Transport; namespace Cucumber.SimpleDb.Test { + [TestFixture ()] public class AwsRestServiceTest { + private HttpRequestMessage _requestMessage; + private HttpClient _httpClient; + [SetUp] + public void Setup() + { + var responseMessage = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent("") + }; + _httpClient = new HttpClient(new FakeHttpMessageHandler(responseMessage, req => _requestMessage = req)); + + } + [Test ()] public void ValidateRequestSignature () { string generatedUri = null; - var service = new AwsRestService ("myPublicKey", "myPrivateKey", new CaptureUriRequestProvider(uri => generatedUri = uri)); + var service = new AwsRestService("myPublicKey", "myPrivateKey", _httpClient); service.ExecuteRequest (new NameValueCollection { { "Item.0.ItemName", "TestItem1" }, { "Item.0.Attribute.0.Name", "TestAtt1" }, { "Item.0.Attribute.0.Value", "123" } }); - //TODO: validate HMAC + Assert.NotNull(_requestMessage); } } } diff --git a/source/Cucumber.SimpleDb.Test/Transport/CaptureUriRequestProvider.cs b/source/Cucumber.SimpleDb.Test/Transport/CaptureUriRequestProvider.cs deleted file mode 100644 index 8f7f578..0000000 --- a/source/Cucumber.SimpleDb.Test/Transport/CaptureUriRequestProvider.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Net; -using System.IO; -using System.Text; -using Cucumber.SimpleDb.Transport; - -namespace Cucumber.SimpleDb.Test -{ - public class CaptureUriRequestProvider : IWebRequestProvider - { - private readonly Action _captureUri; - - public CaptureUriRequestProvider (Action captureUri) - { - _captureUri = captureUri; - } - - public System.Net.WebRequest Create (string uri) - { - _captureUri (uri); - return new DummyWebRequest(); - } - - private class DummyWebRequest : WebRequest - { - public override WebResponse GetResponse () - { - return new DummyWebResponse (); - } - - private class DummyWebResponse : WebResponse - { - public override Stream GetResponseStream () - { - return new MemoryStream (Encoding.UTF8.GetBytes ("")); - } - - public override void Close () - { - } - } - } - } -} - diff --git a/source/Cucumber.SimpleDb.Test/app.config b/source/Cucumber.SimpleDb.Test/app.config new file mode 100644 index 0000000..49d6235 --- /dev/null +++ b/source/Cucumber.SimpleDb.Test/app.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/source/Cucumber.SimpleDb.Test/packages.config b/source/Cucumber.SimpleDb.Test/packages.config index d6fcf5d..cbc3eb6 100644 --- a/source/Cucumber.SimpleDb.Test/packages.config +++ b/source/Cucumber.SimpleDb.Test/packages.config @@ -1,5 +1,8 @@  + + + \ No newline at end of file diff --git a/source/Cucumber.SimpleDb/Cucumber.SimpleDb.csproj b/source/Cucumber.SimpleDb/Cucumber.SimpleDb.csproj index 86e00af..8e5093f 100644 --- a/source/Cucumber.SimpleDb/Cucumber.SimpleDb.csproj +++ b/source/Cucumber.SimpleDb/Cucumber.SimpleDb.csproj @@ -11,6 +11,8 @@ Cucumber.SimpleDb Cucumber.SimpleDb 512 + ..\ + true True @@ -33,6 +35,29 @@ + + ..\packages\Microsoft.Bcl.1.1.3\lib\net40\System.IO.dll + + + False + ..\packages\Microsoft.Net.Http.2.2.18\lib\net40\System.Net.Http.dll + + + ..\packages\Microsoft.Net.Http.2.2.18\lib\net40\System.Net.Http.Extensions.dll + + + ..\packages\Microsoft.Net.Http.2.2.18\lib\net40\System.Net.Http.Primitives.dll + + + False + ..\packages\Microsoft.Net.Http.2.2.18\lib\net40\System.Net.Http.WebRequest.dll + + + ..\packages\Microsoft.Bcl.1.1.3\lib\net40\System.Runtime.dll + + + ..\packages\Microsoft.Bcl.1.1.3\lib\net40\System.Threading.Tasks.dll + @@ -41,6 +66,7 @@ + @@ -96,16 +122,29 @@ - - - + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + +