Esempio n. 1
0
        public void it_should_serialize_result_to_header()
        {
            object[] arguments = { 1, 2 };
            int      expected  = 3;

            var result = Composer.ComposeResponse(CreateRequestMapping("Multiply", arguments), expected, arguments);

            result.Should().BeOfType <StringResponseInfo>();
            Converter.Verify(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()), Times.Never);
            result.Headers.Should().ContainKey("Pragma").WhichValue.Should().Be(expected.ToString());
        }
Esempio n. 2
0
        public void it_should_serialize_result_to_body()
        {
            object[] arguments = { 1, 2 };
            int      expected  = 3;

            Converter.Setup(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()));

            var result = Composer.ComposeResponse(CreateRequestMapping("Add", arguments), expected, arguments);

            result.Should().BeOfType <ObjectResponseInfo <int> >();
            Converter.Verify(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()), Times.Once);
        }
Esempio n. 3
0
        public void it_should_handle_Read_request_correctly()
        {
            object[] arguments = { 1 };
            Person   expected  = null;

            Converter.Setup(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()));

            var result = Composer.ComposeResponse(CreateRequestMapping("Get", arguments), expected, arguments);

            result.Should().BeOfType <StringResponseInfo>();
            result.Status.Should().Be(HttpStatusCode.NotFound);
            Converter.Verify(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()), Times.Never);
        }
Esempio n. 4
0
        public void it_should_handle_Delete_request_correctly()
        {
            object[] arguments = { 1 };
            bool?    expected  = true;

            Converter.Setup(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()));

            var result = Composer.ComposeResponse(CreateRequestMapping("Delete", arguments), expected, arguments);

            result.Should().BeOfType <StringResponseInfo>();
            result.Status.Should().Be(HttpStatusCode.NoContent);
            Converter.Verify(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()), Times.Never);
        }
Esempio n. 5
0
        public void it_should_merge_headers_if_the_output_is_already_a_response()
        {
            var expected   = "http://temp.uri/";
            var output     = new StringResponseInfo(String.Empty, new RequestInfo(Verb.GET, (HttpUrl)UrlParser.Parse("/"), new MemoryStream(), new BasicClaimBasedIdentity()));
            var controller = new Mock <IController>(MockBehavior.Strict);

            controller.SetupGet(instance => instance.Response).Returns(output);
            var mapping = new Mock <IRequestMapping>(MockBehavior.Strict);

            mapping.SetupGet(instance => instance.Target).Returns(controller.Object);
            output.Headers.Location = expected;

            var result = Composer.ComposeResponse(mapping.Object, output);

            result.Headers["Location"].Value.Should().Be(expected);
        }
Esempio n. 6
0
        public void it_should_return_404_not_found_when_reading_inexisting_resource()
        {
            object[] arguments = { 2 };
            var      expected  = new Person()
            {
                Key = (int)arguments[0]
            };

            Converter.Setup(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()));

            var result = Composer.ComposeResponse(CreateRequestMapping("Get", arguments), expected, arguments);

            result.Should().BeOfType <ObjectResponseInfo <Person> >();
            result.Status.Should().Be(HttpStatusCode.OK);
            Converter.Verify(instance => instance.ConvertFrom(expected, It.IsAny <IResponseInfo>()), Times.Once);
        }
Esempio n. 7
0
        public void it_should_serialize_result_to_both_header_and_body()
        {
            int expected  = 3;
            var arguments = new object[] { 1, 2 };

            Converter.Setup(instance => instance.ConvertFrom(It.IsAny <int>(), It.IsAny <IResponseInfo>()));

            var result = Composer.ComposeResponse(CreateRequestMapping("Whatever", arguments), expected, arguments);

            result.Should().BeOfType <MultiObjectResponseInfo>();
            Converter.Verify(instance => instance.ConvertFrom(It.IsAny <int>(), It.IsAny <IResponseInfo>()), Times.Exactly(arguments.Length));
            result.Headers.Should().ContainKey("Location").WhichValue.Should().Be(arguments.Last().ToString());
            var multiObjectResult = (MultiObjectResponseInfo)result;

            multiObjectResult.Values.Should().HaveCount(2);
            multiObjectResult.Values.First().Should().Be(arguments.First());
            multiObjectResult.Values.Last().Should().Be(expected);
        }
Esempio n. 8
0
        public void it_should_handle_List_request_correctly()
        {
            object[] arguments = { 1, 1, 2 };
            var      expected  = new[] { new Person()
                                         {
                                             Key = 1
                                         } };

            Converter.Setup(instance => instance.ConvertFrom(It.Is <IEnumerable>(collection => collection.Cast <Person>().Contains(expected[0])), It.IsAny <IResponseInfo>()));

            var result = Composer.ComposeResponse(CreateRequestMapping("List", arguments), expected, arguments);

            result.Should().NotBeNull();
            result.GetType().IsGenericType.Should().BeTrue();
            result.GetType().GetGenericTypeDefinition().Should().Be(typeof(ObjectResponseInfo <>));
            typeof(IEnumerable <Person>).IsAssignableFrom(result.GetType().GetGenericArguments()[0]).Should().BeTrue();
            result.Status.Should().Be(HttpStatusCode.PartialContent);
            Converter.Verify(instance => instance.ConvertFrom(It.Is <IEnumerable>(collection => collection.Cast <Person>().Contains(expected[0])), It.IsAny <IResponseInfo>()), Times.Once);
        }
Esempio n. 9
0
        public void it_should_handle_Create_request_correctly()
        {
            var person = new Person()
            {
                Key = 1
            };
            string callUri;
            var    controllerDescription = new ControllerInfo <CrudController>(
                null,
                (HttpUrl)UrlParser.Parse("api/test"),
                Controller.GetType().GetMethod("Get").ToOperationInfo("api/test", Verb.GET, out callUri, person.Key),
                Controller.GetType().GetMethod("Create").ToOperationInfo("api/test", Verb.POST, out callUri, person));

            ControllerDescriptionBuilder.As <IControllerDescriptionBuilder>().Setup(instance => instance.BuildDescriptor()).Returns(controllerDescription);

            var result = Composer.ComposeResponse(CreateRequestMapping("Create", Verb.POST, person), person.Key, person);

            result.Should().BeOfType <StringResponseInfo>();
            result.Status.Should().Be(HttpStatusCode.Created);
            result.Headers.Should().ContainKey("Location").WhichValue.Should().Be("/api/test/" + person.Key);
            Converter.Verify(instance => instance.ConvertFrom(person.Key, It.IsAny <IResponseInfo>()), Times.Never);
        }