diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..76bdacc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +root=true +[*] +indent_size = 4 +indent_style = tab \ No newline at end of file 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..5150d96 --- /dev/null +++ b/source/Cucumber.SimpleDb.Test/Fakes/FakeMessageHandler.cs @@ -0,0 +1,28 @@ +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..6fb127b 100644 --- a/source/Cucumber.SimpleDb.Test/Transport/AwsRestServiceTest.cs +++ b/source/Cucumber.SimpleDb.Test/Transport/AwsRestServiceTest.cs @@ -1,25 +1,88 @@ -using NUnit.Framework; -using System; +using System.Linq; +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 { - [Test ()] - public void ValidateRequestSignature () + 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 ExecuteRequest_ShouldSerializeArgumentsToRequest () { - string generatedUri = null; - var service = new AwsRestService ("myPublicKey", "myPrivateKey", new CaptureUriRequestProvider(uri => generatedUri = uri)); - service.ExecuteRequest (new NameValueCollection { + var service = new AwsRestService("myPublicKey", "myPrivateKey", _httpClient); + var arguments = new NameValueCollection { { "Item.0.ItemName", "TestItem1" }, { "Item.0.Attribute.0.Name", "TestAtt1" }, { "Item.0.Attribute.0.Value", "123" } - }); - //TODO: validate HMAC + }; + service.ExecuteRequest(arguments); + Assert.NotNull(_requestMessage); + + var requestData = _requestMessage.RequestUri.ToNameValueCollection(); + Assert.NotNull(requestData); + Assert.IsTrue(requestData.Count > 0); + foreach(string key in arguments) + { + Assert.AreEqual(arguments[key], requestData[key]); + } + } + + [Test] + public void ExecuteRequest_ShouldAddStandardArgumentsToRequest() + { + var service = new AwsRestService("myPublicKey", "myPrivateKey", _httpClient); + service.ExecuteRequest(new NameValueCollection()); + Assert.NotNull(_requestMessage); + + var requestData = _requestMessage.RequestUri.ToNameValueCollection(); + Assert.NotNull(requestData); + Assert.IsTrue(requestData.Count > 0); + new[] + { + "AWSAccessKeyId", + "SignatureVersion", + "SignatureMethod", + "Timestamp", + "Version" + }.ToList() + .ForEach(key => Assert.IsNotNullOrEmpty(requestData[key])); + Assert.AreEqual("myPublicKey", requestData["AWSAccessKeyId"]); + } + + [Test] + public void ExecuteRequest_ShouldAddSignatureToRequest() + { + var service = new AwsRestService("myPublicKey", "myPrivateKey", _httpClient); + service.ExecuteRequest(new NameValueCollection()); + Assert.NotNull(_requestMessage); + + var requestData = _requestMessage.RequestUri.ToNameValueCollection(); + Assert.NotNull(requestData); + Assert.IsTrue(requestData.Count > 0); + Assert.IsNotNullOrEmpty(requestData["Signature"]); + + //TODO: (CV) Should assert the actual signature. Need to move the signature generation logic out of the AwsRequestService class + } } } 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..1a5cd8c 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,8 @@ + + @@ -96,16 +123,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}. + + + + + + + +