public void CanParseAccountWithValidJsonPayloadAndHeaders()
        {
            var accountName = "1234567890";
            var validSingleContainerJson = @"[
                                            {
                                                  ""count"": 1,
                                                  ""bytes"": 7,
                                                  ""name"": ""TestContainer""
                                            }]";

            var headers = new HttpHeadersAbstraction
            {
                {"X-Account-Bytes-Used", "12345"},
                {"X-Account-Object-Count", "1"},
                {"X-Account-Container-Count", "1"}
            };

            var converter = new StorageAccountPayloadConverter(new ServiceLocator());
            var account = converter.Convert(accountName, headers, validSingleContainerJson);

            Assert.IsNotNull(account);
            Assert.AreEqual(accountName, account.Name);
            Assert.AreEqual(12345, account.TotalBytesUsed);
            Assert.AreEqual(1, account.TotalObjectCount);
            Assert.AreEqual(1, account.TotalContainerCount);
            Assert.AreEqual(1, account.Containers.ToList().Count());
        }
 public void CanAddHeaders()
 {
     var headers = new HttpHeadersAbstraction();
     headers.Add("Test","Value");
     Assert.IsTrue(headers.Contains("Test"));
     Assert.AreEqual("Value",headers["Test"].First());
 }
        public async Task CanGetStorageContainerWithOkResponse()
        {
            var containerName = "TestContainer";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Container-Bytes-Used", "1234"},
                {"X-Container-Object-Count", "1"}
            };

            var payload = @"[
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 0,
                                    ""name"": ""BLAH"",
                                    ""content_type"": ""application/octet-stream""
                                }]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            var result = await client.GetStorageContainer(containerName);
            
            Assert.IsNotNull(result);
            Assert.AreEqual(containerName, result.Name);
            Assert.AreEqual(1234, result.TotalBytesUsed);
            Assert.AreEqual(1, result.TotalObjectCount);
            Assert.IsNotNull(result.Objects);
            Assert.AreEqual(1, result.Objects.Count());
        }
 public void CanGetValuesHeaders()
 {
     var headers = new HttpHeadersAbstraction();
     var values = new List<string>() {"value1", "value2"};
     headers.Add("Test", values);
     Assert.IsTrue(headers.Contains("Test"));
     Assert.AreEqual(values, headers.GetValues("Test"));
 }
        public void CanClearHeaders()
        {
            var headers = new HttpHeadersAbstraction();
            headers.Add("Test", "Value");
            Assert.IsTrue(headers.Contains("Test"));

            headers.Clear();
            Assert.AreEqual(0,headers.Count());
        }
 public static IHttpResponseAbstraction CreateResponse(HttpStatusCode code, IEnumerable<KeyValuePair<string, string>> headers, Stream content)
 {
     var abstractionHeaders = new HttpHeadersAbstraction();
     foreach (var header in headers)
     {
         abstractionHeaders.Add(header.Key, header.Value);
     }
     return new HttpResponseAbstraction(content, abstractionHeaders, code);
 }
        public void CanAddRangeHeaders()
        {
            var headers = new HttpHeadersAbstraction();

            var rspMsg = new HttpResponseMessage();
            rspMsg.Headers.Add("Test", "Value");

            headers.AddRange(rspMsg.Headers);
            Assert.IsTrue(headers.Contains("Test"));
            Assert.AreEqual("Value", headers["Test"].First());
        }
        public void CanTryGetValuesHeaders()
        {
            var headers = new HttpHeadersAbstraction();
            var values = new List<string>() { "value1", "value2" };
            headers.Add("Test", values);

            IEnumerable<string> resValues = new List<string>();
            Assert.IsTrue(headers.TryGetValue("Test", out resValues));

            Assert.AreEqual(2,resValues.Count());
            Assert.AreEqual(values, resValues);
        }
        public void CannotParseAccountWithMissingBytesUsedHeader()
        {
            var accountName = "1234567890";
            var validSingleContainerJson = @"[
                                            {
                                                  ""count"": 1,
                                                  ""bytes"": 7,
                                                  ""name"": ""TestContainer""
                                            }]";

            var headers = new HttpHeadersAbstraction
            {
                {"X-Account-Object-Count", "1"},
                {"X-Account-Container-Count", "1"}
            };

            var converter = new StorageAccountPayloadConverter(new ServiceLocator());
            converter.Convert(accountName, headers, validSingleContainerJson);
        }
        public void CanParseDynamicManifestFromHeadersWithMetadata()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"},
                {"X-Object-Meta-Test1","Test1"},
                {"X-Object-Manifest","a/b"}
            };

            var converter = new StorageObjectPayloadConverter();
            var obj = converter.Convert(containerName, objectName, headers);

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(DynamicLargeObjectManifest));
            Assert.AreEqual(1234, obj.Length);
            Assert.AreEqual("d41d8cd98f00b204e9800998ecf8427e", obj.ETag);
            Assert.AreEqual("application/octet-stream", obj.ContentType);
            Assert.AreEqual(DateTime.Parse("Wed, 12 Mar 2014 23:42:23 GMT"), obj.LastModified);
            Assert.AreEqual(objectName, obj.Name);
            Assert.AreEqual(containerName, obj.ContainerName);
            Assert.AreEqual(1, obj.Metadata.Count());
            Assert.IsTrue(obj.Metadata.ContainsKey("Test1"));
            Assert.AreEqual("Test1", obj.Metadata["Test1"]);

            var manifest = obj as DynamicLargeObjectManifest;
            Assert.AreEqual("a/b", manifest.SegmentsPath);
        }
        public async Task CanCreateStorageContainerWithNoContentResponse()
        {
            var containerName = "TestContainer";

            var headers = new HttpHeadersAbstraction
            {
                {"X-Container-Bytes-Used", "12345"},
                {"X-Container-Object-Count", "1"}
            };

            var restResp = new HttpResponseAbstraction(new MemoryStream(), headers, HttpStatusCode.NoContent);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var containerReq = new StorageContainer(containerName, new Dictionary<string, string>());

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.CreateStorageContainer(containerReq);

            //Assert.IsNotNull(container);
            //Assert.AreEqual(containerName, container.Name);
            //Assert.AreEqual(12345, container.TotalBytesUsed);
        }
        public async Task CanGetStorageAccountWithOkResponse()
        {
            var accountName = "1234567890";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Account-Bytes-Used", "1234"},
                {"X-Account-Object-Count", "1"},
                {"X-Account-Container-Count", "1"}
            };

            var payload = @"[
                            {
                                    ""count"": 1,
                                    ""bytes"": 7,
                                    ""name"": ""TestContainer""
                            }]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetStorageAccount();

            Assert.IsNotNull(result);
            Assert.AreEqual(accountName, result.Name);
            Assert.AreEqual(1234, result.TotalBytesUsed);
            Assert.AreEqual(1, result.TotalObjectCount);
            Assert.AreEqual(1, result.TotalContainerCount);
            Assert.IsNotNull(result.Containers);
            Assert.AreEqual(1, result.Containers.Count());
        }
        public void CannotParseAccountWithNullPayload()
        {
            var accountName = "1234567890";

            var headers = new HttpHeadersAbstraction
            {
                {"X-Account-Bytes-Used", "12345"},
                {"X-Account-Object-Count", "1"},
                {"X-Account-Container-Count", "1"}
            };

            var converter = new StorageAccountPayloadConverter(new ServiceLocator());
            converter.Convert(accountName, headers, null);
        }
        public async Task ExceptionThrownWhenCreatingaStorageFolderMissingLength()
        {
            var containerName = "TestContainer";
            var folderName = "a/b/b/";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"}
            };

            var restResp = new HttpResponseAbstraction(new MemoryStream(), headers, HttpStatusCode.LengthRequired);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.CreateStorageFolder(containerName, folderName);
        }
        public void CannotParseWithBadBytesHeader()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "This is not a number"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"Etag", "d41d8cd98f00b204e9800998ecf8427e"}
            };

            var converter = new StorageObjectPayloadConverter();
            converter.Convert(containerName, objectName, headers);
        }
        public async Task CanGetStorageAccontWithNoContent()
        {
            var accountName = "1234567890";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Account-Bytes-Used", "1234"},
                {"X-Account-Object-Count", "1"},
                {"X-Account-Container-Count", "1"}
            };

            var restResp = new HttpResponseAbstraction(new MemoryStream(), headers, HttpStatusCode.NoContent);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetStorageAccount();

            Assert.IsNotNull(result);
            Assert.AreEqual(accountName, result.Name);
            Assert.AreEqual(1234, result.TotalBytesUsed);
            Assert.AreEqual(1, result.TotalObjectCount);
            Assert.AreEqual(1, result.TotalContainerCount);
            Assert.IsNotNull(result.Containers);
            Assert.AreEqual(0, result.Containers.Count());
        }
        public async Task CanDownloadStorageObjectWithOkResponse()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"}
            };

            var data = "some data";
            var content = TestHelper.CreateStream(data);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);

            var respContent = new MemoryStream();
            var result = await client.DownloadStorageObject(containerName, objectName, respContent);

            Assert.IsNotNull(result);
            Assert.AreEqual(objectName, result.Name);
            Assert.AreEqual(containerName, result.ContainerName);
            Assert.AreEqual(1234, result.Length);
            Assert.AreEqual("application/octet-stream", result.ContentType);
            Assert.AreEqual("d41d8cd98f00b204e9800998ecf8427e", result.ETag);
            Assert.AreEqual(DateTime.Parse("Wed, 12 Mar 2014 23:42:23 GMT"), result.LastModified);
            Assert.AreEqual(data,TestHelper.GetStringFromStream(respContent));
        }
        public async Task CanGetDynamicStorageManifestWithOkResponseAndPayload()
        {
            var containerName = "TestContainer";
            var manifestName = "a/b/c/manifest";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"},
                {"X-Object-Meta-Test1","Test1"},
                {"X-Object-Manifest","a/b"}
            };

            var restResp = new HttpResponseAbstraction(new MemoryStream(), headers, HttpStatusCode.NoContent);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            var result = await client.GetStorageManifest(containerName, manifestName);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DynamicLargeObjectManifest));
            Assert.AreEqual(manifestName, result.FullName);
            Assert.AreEqual("manifest", result.Name);
            var manifest = result as DynamicLargeObjectManifest;
            Assert.AreEqual("a/b", manifest.SegmentsPath);
        }
        public async Task CanGetStaticStorageManifestWithOkResponseAndPayload()
        {
            var containerName = "TestContainer";
            var manifestName = "a/b/c/manifest";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"},
                {"X-Object-Meta-Test1","Test1"},
                {"X-Static-Large-Object","True"}
            };

            var payload = @"[
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 0,
                                    ""name"": ""a/b/c/BLAH"",
                                    ""content_type"": ""application/octet-stream""
                                }]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            var result = await client.GetStorageManifest(containerName, manifestName);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(StaticLargeObjectManifest));
            Assert.AreEqual(manifestName, result.FullName);
            Assert.AreEqual("manifest", result.Name);

            var manifest = result as StaticLargeObjectManifest;
            Assert.IsNotNull(manifest.Objects);
            Assert.AreEqual(1, manifest.Objects.Count());
            Assert.AreEqual("a/b/c/BLAH", manifest.Objects.First().FullName);
        }
        public async Task CanGetStorageObjectWithHeaders()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"},
                {"X-Object-Meta-Test1","Test1"}
            };

            var restResp = new HttpResponseAbstraction(new MemoryStream(), headers, HttpStatusCode.NoContent);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetStorageObject(containerName, objectName);

            Assert.IsNotNull(result);
            Assert.AreEqual(objectName, result.Name);
            Assert.AreEqual(containerName, result.ContainerName);
            Assert.AreEqual(1234, result.Length);
            Assert.AreEqual("application/octet-stream", result.ContentType);
            Assert.AreEqual("d41d8cd98f00b204e9800998ecf8427e", result.ETag);
            Assert.AreEqual(DateTime.Parse("Wed, 12 Mar 2014 23:42:23 GMT"), result.LastModified);
            Assert.AreEqual(1, result.Metadata.Count());
            Assert.IsTrue(result.Metadata.ContainsKey("Test1"));
            Assert.AreEqual("Test1", result.Metadata["Test1"]);
        }
        public async Task CanGetStorageObjectWithOkResponseThatIsAManifest()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"},
                {"X-Object-Meta-Test1","Test1"},
                {"X-Static-Large-Object","True"}
            };

            var payload = @"[
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 54321,
                                    ""name"": ""a/b/c/BLAH"",
                                    ""content_type"": ""application/octet-stream""
                                }]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetStorageObject(containerName, objectName);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(StaticLargeObjectManifest));
            Assert.AreEqual(objectName, result.Name);
            Assert.AreEqual(containerName, result.ContainerName);
            Assert.AreEqual(1234, result.Length);
            Assert.AreEqual("application/octet-stream", result.ContentType);
            Assert.AreEqual("d41d8cd98f00b204e9800998ecf8427e", result.ETag);
            Assert.AreEqual(DateTime.Parse("Wed, 12 Mar 2014 23:42:23 GMT"), result.LastModified);
        }
        public void CannotParseWithMissingModifiedHeader()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Etag", "d41d8cd98f00b204e9800998ecf8427e"}
            };

            var converter = new StorageObjectPayloadConverter();
            converter.Convert(containerName, objectName, headers);
        }
        public async Task ExceptionThrownWhenDeletingAStorageFolderThatHasChildren()
        {
            var containerName = "TestContainer";
            var folderName = "a/b/c/";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Container-Bytes-Used", "1234"},
                {"X-Container-Object-Count", "1"}
            };

            var payload = @"[
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 0,
                                    ""name"": ""a/b/c/BLAH"",
                                    ""content_type"": ""application/octet-stream""
                                }]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.DeleteStorageFolder(containerName, folderName);
        }
        public void CanParseObjectFromHeadersAndSpecificObjectLength()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"}
            };

            var converter = new StorageObjectPayloadConverter();
            var obj = converter.Convert(containerName, objectName, headers, 100);

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(StorageObject));
            Assert.AreEqual(100, obj.Length);
            Assert.AreEqual("d41d8cd98f00b204e9800998ecf8427e", obj.ETag);
            Assert.AreEqual("application/octet-stream", obj.ContentType);
            Assert.AreEqual(DateTime.Parse("Wed, 12 Mar 2014 23:42:23 GMT"), obj.LastModified);
            Assert.AreEqual(objectName, obj.Name);
            Assert.AreEqual(containerName, obj.ContainerName);
        }
        public async Task CannotGetStaticStorageManifestWhenObjectIsNotManifest()
        {
            var containerName = "TestContainer";
            var manifestName = "a/b/c/manifest";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"ETag", "d41d8cd98f00b204e9800998ecf8427e"},
                {"X-Object-Meta-Test1","Test1"},
                {"X-Static-Large-Object","False"}
            };

            var payload = @"[
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 0,
                                    ""name"": ""a/b/c/BLAH"",
                                    ""content_type"": ""application/octet-stream""
                                }]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            await client.GetStorageManifest(containerName, manifestName);
        }
        public void CannotParseWithMissingETagHeader()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Content-Type", "application/octet-stream"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"}
            };

            var converter = new StorageObjectPayloadConverter();
            converter.Convert(containerName, objectName, headers);
        }
        public async Task CanGetStorageFolderWithNoContentResponse()
        {
            var containerName = "TestContainer";
            var folderName = "a/b/c/";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Container-Bytes-Used", "1234"},
                {"X-Container-Object-Count", "1"}
            };

            var restResp = new HttpResponseAbstraction(new MemoryStream(), headers, HttpStatusCode.NoContent);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            var result = await client.GetStorageFolder(containerName, folderName);

            Assert.IsNotNull(result);
            Assert.AreEqual("a/b/c/", result.FullName);
            Assert.AreEqual("c", result.Name);
            Assert.IsNotNull(result.Objects);
            Assert.AreEqual(0, result.Objects.Count());
            Assert.IsNotNull(result.Folders);
            Assert.AreEqual(0, result.Folders.Count());
        }
        public void CannotParseWithMissingContentTypeHeader()
        {
            var containerName = "TestContainer";
            var objectName = "TestObject";

            var headers = new HttpHeadersAbstraction()
            {
                {"Content-Length", "1234"},
                {"Last-Modified", "Wed, 12 Mar 2014 23:42:23 GMT"},
                {"Etag", "d41d8cd98f00b204e9800998ecf8427e"}
            };

            var converter = new StorageObjectPayloadConverter();
            converter.Convert(containerName, objectName, headers);
        }
        public async Task CanGetStorageFolderWithOkResponseAndSubFolders()
        {
            var containerName = "TestContainer";
            var folderName = "a/b/c/";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Container-Bytes-Used", "1234"},
                {"X-Container-Object-Count", "1"}
            };

            var payload = @"[
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 0,
                                    ""name"": ""a/b/c/"",
                                    ""content_type"": ""application/octet-stream""
                                },
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 0,
                                    ""name"": ""a/b/c/BLAH"",
                                    ""content_type"": ""application/octet-stream""
                                },
                                {
                                        ""subdir"": ""a/b/c/d/""
                                },
                                {
                                        ""subdir"": ""a/b/c/x/""
                                }
                            ]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            var resp = await client.GetStorageFolder(containerName, folderName);

            Assert.AreEqual("c", resp.Name);
            Assert.AreEqual("a/b/c/", resp.FullName);
            Assert.AreEqual(1, resp.Objects.Count);
            Assert.AreEqual(2, resp.Folders.Count);

            var obj = resp.Objects.First();
            Assert.AreEqual("a/b/c/BLAH", obj.FullName);

            var dNode = resp.Folders.First(f => f.FullName == "a/b/c/d/");
            var xNode = resp.Folders.First(f => f.FullName == "a/b/c/x/");

            Assert.AreEqual("d", dNode.Name);
            Assert.AreEqual(0, dNode.Folders.Count);
            Assert.AreEqual(0, dNode.Objects.Count);

            Assert.AreEqual("x", xNode.Name);
            Assert.AreEqual(0, xNode.Folders.Count);
            Assert.AreEqual(0, xNode.Objects.Count);
        }
        public async Task ExceptionthrownWhenGettingAStorageFolderThatDoesNotExistAndCannotBeInferred()
        {
            var containerName = "TestContainer";
            var folderName = "a/b/c/";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Container-Bytes-Used", "1234"},
                {"X-Container-Object-Count", "1"}
            };

            var payload = @"[]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            var resp = await client.GetStorageFolder(containerName, folderName);
        }
        public async Task <IHttpResponseAbstraction> SendAsync()
        {
            var requestMessage = new HttpRequestMessage {
                Method = this.Method, RequestUri = this.Uri
            };

            if (this.Method == HttpMethod.Post || this.Method == HttpMethod.Put)
            {
                if (this.Content != null)
                {
                    requestMessage.Content = new StreamContent(this.Content);
                    if (this.ContentType != string.Empty)
                    {
                        requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(this.ContentType);
                    }
                }
            }

            requestMessage.Headers.Clear();
            foreach (var header in this.Headers)
            {
                requestMessage.Headers.Add(header.Key, header.Value);
            }

            var startTime = DateTime.Now;

            try
            {
                var result = await this._client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, this._cancellationToken);

                var headers = new HttpHeadersAbstraction(result.Headers);

                Stream content = null;
                if (result.Content != null)
                {
                    headers.AddRange(result.Content.Headers);
                    content = this.WaitForResult(result.Content.ReadAsStreamAsync(), new TimeSpan(0, 0, 0, 0, int.MaxValue));
                }

                var retval = new HttpResponseAbstraction(content, headers, result.StatusCode);

                //TODO: Add logging code

                return(retval);
            }
            catch (Exception ex)
            {
                //TODO: Add logging code

                var tcex = ex as TaskCanceledException;
                if (tcex == null)
                {
                    throw;
                }

                if (this._cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException("The operation was canceled by user request.", tcex, this._cancellationToken);
                }

                if (DateTime.Now - startTime > this.Timeout)
                {
                    throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "The task failed to complete in the given timeout period ({0}).", this.Timeout));
                }

                throw;
            }
        }