public void CanRetrieveSimpleResourceDetails()
        {
            using (var host = new InMemoryHost(new SampleApi.Configuration()))
            {
                var request = new InMemoryRequest
                {
                    Uri = new Uri("http://localhost/api-docs/simple"),
                    HttpMethod = "GET",
                    Entity = {ContentType = MediaType.Json}
                };

                request.Entity.Headers["Accept"] = "application/json";

                var response = host.ProcessRequest(request);
                var statusCode = response.StatusCode;
                Assert.AreEqual(200, statusCode);

                Assert.IsTrue(response.Entity.ContentLength>0);

                response.Entity.Stream.Seek(0, SeekOrigin.Begin);

                var serializer = new DataContractJsonSerializer(typeof(ResourceDetails));
                var resourceDetails = (ResourceDetails) serializer.ReadObject(response.Entity.Stream);

                Assert.IsNotNull(resourceDetails);
            }
        }
Exemple #2
0
        public void Should_Get_Specific_Task()
        {
            using (var host = new InMemoryHost(new Configuration()))
            {
                var req = new InMemoryRequest
                    {
                        Uri = new Uri("http://localhost/tasks/1"),
                        HttpMethod = "GET"
                    };

                req.Entity.Headers["Accept"] = "application/json";

                var response = host.ProcessRequest(req);

                // deserialize the content from the response
                Resources.Task returnedObject = null;
                if (response.Entity.ContentLength > 0)
                {
                    // you must rewind the stream, as OpenRasta
                    // won't do this for you
                    response.Entity.Stream.Seek(0, SeekOrigin.Begin);

                    var serializer = new DataContractJsonSerializer(typeof(Resources.Task));
                    returnedObject = (Resources.Task)serializer.ReadObject(response.Entity.Stream);
                }

                returnedObject.Id.ShouldEqual(1);
            }
        }
Exemple #3
0
 public static HttpEntity GetHttpEntity(InMemoryRequest postRequest, string data, MediaType MediaType)
 {
     var bytes = Encoding.UTF8.GetBytes(data);
     var httpEntity = new HttpEntity(postRequest.Entity.Headers, new MemoryStream(bytes)) { ContentLength = bytes.Length };
     httpEntity.ContentType = MediaType;
     return httpEntity;
 }
 public InMemoryCommunicationContext()
 {
     ApplicationBaseUri = new Uri("http://local");
     Request = new InMemoryRequest();
     Response = new InMemoryResponse();
     ServerErrors = new List<Error>();
     PipelineData = new PipelineData();
 }
 public InMemoryCommunicationContext()
 {
     ApplicationBaseUri = new Uri("http://local");
     Request = new InMemoryRequest();
     Response = new InMemoryResponse();
     ServerErrors = new List<Error>();
     Environment = new Environment();
 }
        public void OrderList_Get_ShouldReturnStatusCodeOK()
        {
            var inMemoryRequest = new InMemoryRequest {Uri = new Uri("http://localhost/orders"), HttpMethod = "GET"};

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode);
        }
        public void Get_WhenHasNotPostedOrder_ShouldReturnStatusCodeNotFound()
        {
            var inMemoryRequest = new InMemoryRequest { Uri = new Uri("http://localhost/order/1"), HttpMethod = "GET" };

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            Assert.AreEqual((int)HttpStatusCode.NotFound, response.StatusCode);
        }
 public InMemoryCommunicationContext()
 {
     ApplicationBaseUri = new Uri("http://localhost/");
     Request            = new InMemoryRequest();
     Response           = new InMemoryResponse();
     ServerErrors       = new List <Error>();
     PipelineData       = new PipelineData();
 }
Exemple #9
0
 public InMemoryCommunicationContext()
 {
     ApplicationBaseUri = new Uri("http://local");
     Request            = new InMemoryRequest();
     Response           = new InMemoryResponse();
     ServerErrors       = new List <Error>();
     Environment        = new Environment();
 }
        private IResponse PostOrder()
        {
            var inMemoryRequest = new InMemoryRequest {Uri = new Uri("http://localhost/order"), HttpMethod = "POST"};
            var jsonFromObject = Utils.GetJsonFromObject(new {customer = "eirik"});
            inMemoryRequest.Entity = Utils.GetHttpEntity(inMemoryRequest, jsonFromObject, MediaType.Json);

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);
            return response;
        }
        public void Delete_WhenHasPostedOrder_ShouldReturnStatusCodeNoContent()
        {
            var orderUri = PostOrder().Headers["location"];
            var inMemoryRequest = new InMemoryRequest { Uri = new Uri(orderUri), HttpMethod = "DELETE" };

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            Assert.AreEqual((int)HttpStatusCode.NoContent, response.StatusCode);
        }
        public void Get_WhenHasPostedOrderWithCustomerEirik_ShoulReturnOrderWithCustomerEirik()
        {
            var orderUri = PostOrder().Headers["location"];
            var inMemoryRequest = new InMemoryRequest { Uri = new Uri(orderUri), HttpMethod = "GET" };

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            var json = Utils.GetResultFromJson(response);
            Assert.AreEqual("eirik", json.customer.Value);
        }
        public void Get_WhenHasPostedOrder_ShouldReturnOneOrderPreview()
        {
            PostOrder();
            var inMemoryRequest = new InMemoryRequest { Uri = new Uri("http://localhost/orders"), HttpMethod = "GET" };

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            var resultFromJson = Utils.GetResultFromJson(response);
            Assert.AreEqual(1, resultFromJson.Count);
        }
        public void Get_WhenHasPostedOrder_ShouldReturnOrderPreviewWithUri()
        {
            var postOrderResponse = PostOrder();
            var inMemoryRequest = new InMemoryRequest { Uri = new Uri("http://localhost/orders"), HttpMethod = "GET" };

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            var resultFromJson = Utils.GetResultFromJson(response);
            Assert.AreEqual(postOrderResponse.Headers["location"], resultFromJson[0].uri.Value);
        }
 IResponse RedirectOn303SeeOther(IResponse response)
 {
     string acceptHeader = response.Headers["Accept"];
     var redirectedRequest = new InMemoryRequest
     {
         Uri = new Uri(response.Headers["Location"], UriKind.Absolute),
         HttpMethod = "GET"
     }.Accept(acceptHeader);
     response = _host.ProcessRequest(redirectedRequest);
     return response;
 }
        public void Missing_registration_throws_exception()
        {
            var container = new Container();
            _host = new InMemoryHost(new Configuration(container));

            IRequest request = new InMemoryRequest
                                   {
                                       HttpMethod = "GET",
                                       Uri = new Uri("http://localhost/Config/tacos")
                                   };

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(500, response.StatusCode);
        }
        public void Get_component_list_returns_list()
        {
            var entity = _repository.Create();
            entity.Contents = new ConfigRoot
                                  {
                                      ComponentName = "tacos",
                                      Data = new ConfigNode {Name = "host", Value = "tortilla"}
                                  };
            _repository.Add(entity);
            _repository.Save();

            IRequest request = new InMemoryRequest
                                   {
                                       HttpMethod = "GET",
                                       Uri = new Uri("http://localhost/Config")
                                   };

            var response = _host.ProcessRequest(request);
            var data = GetStringListFromResponseStream(response.Entity.Stream);

            Assert.AreEqual(200, response.StatusCode);
            Assert.AreEqual("tacos", data.ToArray()[0]);
        }
        public void Post_returns_400_if_component_name_is_null()
        {
            var configRoot = new ConfigRoot
            {
                ComponentName = null,
                Data = new ConfigNode
                {
                    Name = string.Empty,
                    Value = string.Empty
                }
            };

            IRequest request = new InMemoryRequest
            {
                HttpMethod = "POST",
                Uri = new Uri("http://localhost/Config")
            };
            request.Headers["Content-Type"] = "application/json";

            var length = WriteConfigRootToStream(request.Entity.Stream, configRoot);
            request.Headers["Content-Length"] = length.ToString(CultureInfo.InvariantCulture);

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(400, response.StatusCode);
        }
        public void Put_with_invalid_content_type_returns_415()
        {
            var entity = _repository.Create();
            entity.Contents = new ConfigRoot
            {
                ComponentName = "taboUli",
                Data = new ConfigNode { Name = "tabouli", Value = "salad" }
            };
            _repository.Add(entity);
            _repository.Save();

            var configRoot = new ConfigRoot
            {
                ComponentName = "tabouLi",
                Data = new ConfigNode
                {
                    Name = "tabouli",
                    Value = "tomatoes",
                    Children =
                        new List<ConfigNode> { new ConfigNode { Name = "Side", Value = "Rice" } }
                }
            };

            IRequest request = new InMemoryRequest
            {
                HttpMethod = "PUT",
                Uri = new Uri("http://localhost/Config/tabouli")
            };
            request.Headers["Content-Type"] = "text/plain";

            var length = WriteConfigRootToStream(request.Entity.Stream, configRoot);
            request.Headers["Content-Length"] = length.ToString(CultureInfo.InvariantCulture);

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(415, response.StatusCode);
        }
        public void Put_on_empty_repo_returns_404()
        {
            var configRoot = new ConfigRoot
            {
                ComponentName = "tabouli",
                Data = new ConfigNode
                {
                    Name = "tabouli",
                    Value = "tomatoes",
                    Children =
                        new List<ConfigNode> { new ConfigNode { Name = "Side", Value = "Rice" } }
                }
            };

            IRequest request = new InMemoryRequest
            {
                HttpMethod = "PUT",
                Uri = new Uri("http://localhost/Config/tacos")
            };

            request.Headers["Content-Type"] = "application/json";

            var length = WriteConfigRootToStream(request.Entity.Stream, configRoot);
            request.Headers["Content-Length"] = length.ToString(CultureInfo.InvariantCulture);

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(404, response.StatusCode);
        }
        public void Ensure_that_error_handling_works_for_put()
        {
            // Override the config to give it a broken logger
            /*var mockLogger = new Mock<ILog>();
            mockLogger.Setup(s => s.Debug(It.Is<string>(m => m.Contains("Entering ConfigHandler"))))
                .Throws(new ApplicationException("ha ha!"));*/

            _mockFileSystem = (new MockFileSystemProvider()).MockFileSystem;
            _repository = new DurableMemoryRepository<ConfigRoot>(DataPath, _mockFileSystem.Object);

            var container = new Container();
            container.Configure(x =>
            {
                x.For<IRepository<IJsonEntity<ConfigRoot>>>().Singleton().Use(_repository);
                //x.For<ILog>().Singleton().Use(mockLogger.Object);
            });

            _host = new InMemoryHost(new Configuration(container));

            var configRoot = new ConfigRoot
            {
                ComponentName = "tabOuli",
                Data = new ConfigNode
                {
                    Name = "tabouli",
                    Value = "tomatoes",
                    Children =
                        new List<ConfigNode> { new ConfigNode { Name = "Side", Value = "Rice" } }
                }
            };

            IRequest request = new InMemoryRequest
            {
                HttpMethod = "PUT",
                Uri = new Uri("http://localhost/Config/tacos")
            };

            request.Headers["Content-Type"] = "application/json";
            var length = WriteConfigRootToStream(request.Entity.Stream, configRoot);
            request.Headers["Content-Length"] = length.ToString(CultureInfo.InvariantCulture);

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(500, response.StatusCode);
        }
        public void Put_returns_400_if_component_name_contains_invalid_characters()
        {
            var entity = _repository.Create();
            entity.Contents = new ConfigRoot
            {
                ComponentName = "taboUli",
                Data = new ConfigNode { Name = "tabouli", Value = "salad" }
            };
            _repository.Add(entity);
            _repository.Save();

            var configRoot = new ConfigRoot
            {
                ComponentName = "jsnsadnc sd 8d8*** sad8s7sagd ^Q^ G5asds",
                Data = new ConfigNode
                {
                    Name = string.Empty,
                    Value = string.Empty
                }
            };

            IRequest request = new InMemoryRequest
            {
                HttpMethod = "PUT",
                Uri = new Uri("http://localhost/Config/tabouli")
            };
            request.Headers["Content-Type"] = "application/json";

            var length = WriteConfigRootToStream(request.Entity.Stream, configRoot);
            request.Headers["Content-Length"] = length.ToString(CultureInfo.InvariantCulture);

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(400, response.StatusCode);
        }
        public void Delete_on_empty_repo_returns_404()
        {
            IRequest request = new InMemoryRequest
            {
                HttpMethod = "DELETE",
                Uri = new Uri("http://localhost/Config/tabouli")
            };

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(404, response.StatusCode);
        }
        public void Get_WhenHasPuttedOrder_ShouldReturnUpdatedOrder()
        {
            var orderUri = PostOrder().Headers["location"];
            PutOrder(orderUri, new {customer = "torstein"});
            var inMemoryRequest = new InMemoryRequest { Uri = new Uri(orderUri), HttpMethod = "GET" };

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            var json = Utils.GetResultFromJson(response);
            Assert.AreEqual("torstein", json.customer.Value);
        }
        public void Get_WhenHasPostedOrder_ShoulReturnStatusCodeOK()
        {
            var orderUri = PostOrder().Headers["location"];
            var inMemoryRequest = new InMemoryRequest {Uri = new Uri(orderUri), HttpMethod = "GET"};

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode);
        }
        public void Delete_removes_resource_from_repo()
        {
            var entity = _repository.Create();
            entity.Contents = new ConfigRoot
            {
                ComponentName = "taboUli",
                Data = new ConfigNode { Name = "taboUli", Value = "salad" }
            };
            _repository.Add(entity);
            _repository.Save();

            IRequest request = new InMemoryRequest
            {
                HttpMethod = "DELETE",
                Uri = new Uri("http://localhost/Config/tabouli")
            };
            var response = _host.ProcessRequest(request);

            Assert.AreEqual(204, response.StatusCode);
        }
        public void If_an_attempt_is_made_to_change_a_name_using_put_make_sure_it_doesnt_overwrite_something_else()
        {
            var entity = _repository.Create();
            entity.Contents = new ConfigRoot
                                  {
                                      ComponentName = "taBouli",
                                      Data = new ConfigNode {Name = "tabouli", Value = "salad"}
                                  };
            _repository.Add(entity);

            var anotherEntity = _repository.Create();
            anotherEntity.Contents = new ConfigRoot
                                         {
                                             ComponentName = "Grapes",
                                             Data = new ConfigNode {Name = "grapes", Value = "wrath"}
                                         };
            _repository.Add(anotherEntity);

            _repository.Save();

            var configRoot = new ConfigRoot
                                 {
                                     ComponentName = "grapeS",
                                     Data = new ConfigNode
                                                {
                                                    Name = "taboulI",
                                                    Value = "tomatoes",
                                                    Children =
                                                        new List<ConfigNode>
                                                            {new ConfigNode {Name = "Side", Value = "Rice"}}
                                                }
                                 };

            IRequest request = new InMemoryRequest
                                   {
                                       HttpMethod = "PUT",
                                       Uri = new Uri("http://localhost/Config/tabouli")
                                   };
            request.Headers["Content-Type"] = "application/json";

            var length = WriteConfigRootToStream(request.Entity.Stream, configRoot);
            request.Headers["Content-Length"] = length.ToString(CultureInfo.InvariantCulture);

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(409, response.StatusCode);
        }
        private IResponse PutOrder(string location, dynamic updatedOrder)
        {
            var inMemoryRequest = new InMemoryRequest { Uri = new Uri(location), HttpMethod = "PUT" };
            var jsonFromObject = Utils.GetJsonFromObject(updatedOrder);
            inMemoryRequest.Entity = Utils.GetHttpEntity(inMemoryRequest, jsonFromObject, MediaType.Json);

            var response = _inMemoryHost.ProcessRequest(inMemoryRequest);

            return response;
        }
        public void Post_returns_415_for_invalid_content_type()
        {
            IRequest request = new InMemoryRequest
            {
                HttpMethod = "POST",
                Uri = new Uri("http://localhost/Config")
            };
            request.Headers["Content-Type"] = "text/plain";

            using (var sw = new StreamWriter(request.Entity.Stream))
            {
                sw.Write("The cat ran down the road");
                sw.Flush();
            }
            request.Entity.Stream.Position = 0;

            request.Headers["Content-Length"] = request.Entity.Stream.Length.ToString(CultureInfo.InvariantCulture);

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(415, response.StatusCode);
        }
 public void BeforeEachTest()
 {
     _mockAuthenticator = new Mock<IBasicAuthenticator>();
     _request = new InMemoryRequest();
     _basicScheme = new BasicAuthenticationScheme(_mockAuthenticator.Object);
 }
        public void Put_updates_config_root_in_repository()
        {
            var entity = _repository.Create();
            entity.Contents = new ConfigRoot
                                  {
                                      ComponentName = "taboUli",
                                      Data = new ConfigNode {Name = "tabouli", Value = "salad"}
                                  };
            _repository.Add(entity);
            _repository.Save();

            var configRoot = new ConfigRoot
                                 {
                                     ComponentName = "tabouLi",
                                     Data = new ConfigNode
                                                {
                                                    Name = "tabouli",
                                                    Value = "tomatoes",
                                                    Children =
                                                        new List<ConfigNode>
                                                            {new ConfigNode {Name = "Side", Value = "Rice"}}
                                                }
                                 };

            IRequest request = new InMemoryRequest
                                   {
                                       HttpMethod = "PUT",
                                       Uri = new Uri("http://localhost/Config/tabouli")
                                   };
            request.Headers["Content-Type"] = "application/json";

            var length = WriteConfigRootToStream(request.Entity.Stream, configRoot);
            request.Headers["Content-Length"] = length.ToString(CultureInfo.InvariantCulture);

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(200, response.StatusCode);

            var configObj = GetConfigRootFromResponseStream(response.Entity.Stream);

            Assert.IsNotNull(configObj);
            Assert.AreEqual("tabouli", configObj.ComponentName.ToLower());
            Assert.AreEqual("tomatoes", configObj.Data.Value);
            Assert.AreEqual("Rice", configObj.Data.Children[0].Value);
        }
        public void Get_returns_valid_config_root()
        {
            var entity = _repository.Create();
            entity.Contents = new ConfigRoot
                                  {
                                      ComponentName = "tacos",
                                      Data = new ConfigNode {Name = "host", Value = "tortilla"}
                                  };
            _repository.Add(entity);
            _repository.Save();

            IRequest request = new InMemoryRequest
                                   {
                                       HttpMethod = "GET",
                                       Uri = new Uri("http://localhost/Config/tacos")
                                   };

            var response = _host.ProcessRequest(request);

            Assert.AreEqual(200, response.StatusCode);

            var configObj = GetConfigRootFromResponseStream(response.Entity.Stream);

            Assert.IsNotNull(configObj);
            Assert.AreEqual("tortilla", configObj.Data.Value);
        }