Exemple #1
0
        public void Form_request_and_context_properties_should_be_ignored_in_body_only_mode_when_there_is_a_body()
        {
            // Given
            var typeConverters    = new ITypeConverter[] { new CollectionConverter(), new FallbackConverter(), };
            var bodyDeserializers = new IBodyDeserializer[] { new XmlBodyDeserializer() };
            var binder            = GetBinder(typeConverters, bodyDeserializers);
            var body = XmlBodyDeserializerFixture.ToXmlString(new TestModel {
                IntProperty = 2, StringProperty = "From body"
            });

            var context = CreateContextWithHeaderAndBody("Content-Type", new[] { "application/xml" }, body);

            context.Request.Form["StringProperty"]      = "From form";
            context.Request.Query["IntProperty"]        = "1";
            context.Parameters["AnotherStringProprety"] = "From context";

            // When
            var result = (TestModel)binder.Bind(context, typeof(TestModel), null, new BindingConfig {
                BodyOnly = true
            });

            // Then
            result.StringProperty.ShouldEqual("From body");
            result.AnotherStringProprety.ShouldBeNull(); // not in body, so default value
            result.IntProperty.ShouldEqual(2);
        }
Exemple #2
0
        public void Form_request_and_context_properties_should_take_precedence_over_body_properties()
        {
            // Given
            var typeConverters    = new ITypeConverter[] { new CollectionConverter(), new FallbackConverter(), };
            var bodyDeserializers = new IBodyDeserializer[] { new XmlBodyDeserializer() };
            var binder            = this.GetBinder(typeConverters, bodyDeserializers);
            var body = XmlBodyDeserializerFixture.ToXmlString(new TestModel()
            {
                IntProperty = 0, StringProperty = "From body"
            });

            var context = CreateContextWithHeaderAndBody("Content-Type", new[] { "application/xml" }, body);

            context.Request.Form["StringProperty"]      = "From form";
            context.Request.Query["IntProperty"]        = "1";
            context.Parameters["AnotherStringProprety"] = "From context";

            // When
            var result = (TestModel)binder.Bind(context, typeof(TestModel), null, new BindingConfig());

            // Then
            result.StringProperty.ShouldEqual("From form");
            result.AnotherStringProprety.ShouldEqual("From context");
            result.IntProperty.ShouldEqual(1);
        }
        /// <summary>
        /// Gets the deserialized representation of the response body using the specified body deserializer.
        /// </summary>
        /// <typeparam name="TModel">The type that the response body should be deserialized to.</typeparam>
        /// <param name="bodyWrapper">An instance of the <see cref="BrowserResponseBodyWrapper"/> that the extension should be invoked on.</param>
        /// <param name="bodyDeserializer">An instance of the <see cref="IBodyDeserializer"/> that should be used to deserialize the response body.</param>
        /// <value>A <typeparamref name="TModel"/> instance representation of the HTTP response body.</value>
        public static TModel Deserialize<TModel>(this BrowserResponseBodyWrapper bodyWrapper, IBodyDeserializer bodyDeserializer)
        {
            var bindingContext = new BindingContext { DestinationType = typeof(TModel) };

            return (TModel)bodyDeserializer.Deserialize(bodyWrapper.ContentType, bodyWrapper.AsStream(), bindingContext);
        }
        public void Form_request_and_context_properties_should_take_precedence_over_body_properties()
        {
            // Given
            var typeConverters = new ITypeConverter[] { new CollectionConverter(), new FallbackConverter(), };
            var bodyDeserializers = new IBodyDeserializer[] { new XmlBodyDeserializer() };
            var binder = this.GetBinder(typeConverters, bodyDeserializers);
            var body = XmlBodyDeserializerFixture.ToXmlString(new TestModel { IntProperty = 0, StringProperty = "From body" });

            var context = CreateContextWithHeaderAndBody("Content-Type", new[] { "application/xml" }, body);

            context.Request.Form["StringProperty"] = "From form";
            context.Request.Query["IntProperty"] = "1";
            context.Parameters["AnotherStringProprety"] = "From context";

            // When
            var result = (TestModel)binder.Bind(context, typeof(TestModel), null, BindingConfig.Default);

            // Then
            result.StringProperty.ShouldEqual("From form");
            result.AnotherStringProprety.ShouldEqual("From context");
            result.IntProperty.ShouldEqual(1);
        }
        public void Form_request_and_context_properties_should_be_ignored_in_body_only_mode_when_there_is_a_body()
        {
            // Given
            var typeConverters = new ITypeConverter[] { new CollectionConverter(), new FallbackConverter(), };
            var bodyDeserializers = new IBodyDeserializer[] { new XmlBodyDeserializer() };
            var binder = GetBinder(typeConverters, bodyDeserializers);
            var body = XmlBodyDeserializerFixture.ToXmlString(new TestModel { IntProperty = 2, StringProperty = "From body" });

            var context = CreateContextWithHeaderAndBody("Content-Type", new[] { "application/xml" }, body);

            context.Request.Form["StringProperty"] = "From form";
            context.Request.Query["IntProperty"] = "1";
            context.Parameters["AnotherStringProprety"] = "From context";

            // When
            var result = (TestModel)binder.Bind(context, typeof(TestModel), null, new BindingConfig { BodyOnly = true });

            // Then
            result.StringProperty.ShouldEqual("From body");
            result.AnotherStringProprety.ShouldBeNull(); // not in body, so default value
            result.IntProperty.ShouldEqual(2);
        }