public JsonFormatterExtensionsFixtures()
        {
            var serializerFactory =
               new DefaultSerializerFactory(new ISerializer[] { new DefaultJsonSerializer() });

            this.formatter = A.Fake<IResponseFormatter>();
            A.CallTo(() => this.formatter.SerializerFactory).Returns(serializerFactory);
            this.model = new Person { FirstName = "Andy", LastName = "Pike" };
            this.response = this.formatter.AsJson(model);
        }
Ejemplo n.º 2
0
        public JsonFormatterExtensionsFixtures()
        {
            var serializerFactory =
                new DefaultSerializerFactory(new ISerializer[] { new DefaultJsonSerializer() });

            this.formatter = A.Fake <IResponseFormatter>();
            A.CallTo(() => this.formatter.SerializerFactory).Returns(serializerFactory);
            this.model = new Person {
                FirstName = "Andy", LastName = "Pike"
            };
            this.response = this.formatter.AsJson(model);
        }
        public void when_create_serializer_for_unknown_data_type_it_should_return_null()
        {
            var sut = new DefaultSerializerFactory();

            sut.Create <int[]>().Should().BeNull();
            sut.Create <byte>().Should().BeNull();
            sut.Create <char>().Should().BeNull();
            sut.Create <object>().Should().BeNull();
            sut.Create <DefaultSerializerFactory>().Should().BeNull();
            sut.Create <ISerializerFactory>().Should().BeNull();
            sut.Create <DateTimeOffset>().Should().BeNull();
        }
        public void when_create_serializer_for_known_data_type_it_should_return_expected_serializer()
        {
            var sut = new DefaultSerializerFactory();

            sut.Create <byte[]>().Should().Be(Serializers.ByteArray);
            sut.Create <string>().Should().Be(Serializers.Utf8);
            sut.Create <int>().Should().Be(Serializers.Int32);
            sut.Create <long>().Should().Be(Serializers.Int64);
            sut.Create <float>().Should().Be(Serializers.Single);
            sut.Create <double>().Should().Be(Serializers.Double);
            sut.Create <Null>().Should().Be(Serializers.Null);
        }
Ejemplo n.º 5
0
        public void Should_not_include_default_serializers_when_counting_matches()
        {
            var serializers = new ISerializer[]
            {
                new TestableSerializer("application/json"),
                new DefaultJsonSerializer(GetTestingEnvironment())
            };

            this.serializerFactory = new DefaultSerializerFactory(serializers);

            // When, Then
            Assert.DoesNotThrow(() => this.serializerFactory.GetSerializer("application/json"));
        }
Ejemplo n.º 6
0
        public void Should_throw_if_multiple_serializers_matched_media_range()
        {
            var serializers = new ISerializer[]
            {
                new TestableSerializer("application/json"),
                new TestableSerializer("application/json")
            };

            this.serializerFactory = new DefaultSerializerFactory(serializers);

            // When, Then
            Assert.Throws <InvalidOperationException>(() => this.serializerFactory.GetSerializer("application/json"));
        }
Ejemplo n.º 7
0
        public void Should_not_throw_exception_if_a_serializer_throws()
        {
            // Given
            var serializers = new ISerializer[]
            {
                new ExceptionThrowingSerializer()
            };

            this.serializerFactory = new DefaultSerializerFactory(serializers);

            // When, Then
            Assert.DoesNotThrow(() => this.serializerFactory.GetSerializer("application/json"));
        }
        public XmlFormatterExtensionsFixtures()
        {
            this.rootPathProvider = A.Fake<IRootPathProvider>();

            var serializerFactory =
                new DefaultSerializerFactory(new ISerializer[] { new DefaultXmlSerializer() });

            this.responseFormatter =
                new DefaultResponseFormatter(this.rootPathProvider, new NancyContext(), serializerFactory);

            this.model = new Person { FirstName = "Andy", LastName = "Pike" };
            this.response = this.responseFormatter.AsXml(model);
        }
        public XmlFormatterExtensionsFixtures()
        {
            this.rootPathProvider = A.Fake<IRootPathProvider>();
            var environment = new DefaultNancyEnvironment();
            environment.AddValue(XmlConfiguration.Default);

            var serializerFactory =
                new DefaultSerializerFactory(new ISerializer[] { new DefaultXmlSerializer(environment) });

            this.responseFormatter =
                new DefaultResponseFormatter(this.rootPathProvider, new NancyContext(), serializerFactory, environment);

            this.model = new Person { FirstName = "Andy", LastName = "Pike" };
            this.response = this.responseFormatter.AsXml(model);
        }
Ejemplo n.º 10
0
        public void Should_not_throw_exception_if_a_serializer_throws()
        {
            // Given
            var serializers = new ISerializer[]
            {
                new ExceptionThrowingSerializer()
            };

            this.serializerFactory = new DefaultSerializerFactory(serializers);

            // When
            var result = this.serializerFactory.GetSerializer("application/json");

            // Then
            Assert.Null(result);
        }
Ejemplo n.º 11
0
        public void Should_return_null_if_no_serializer_matched_media_range()
        {
            // Given
            var serializers = new ISerializer[]
            {
                new TestableSerializer("custom/foo"),
                new TestableSerializer("custom/bar"),
                new TestableSerializer("custom/baz"),
            };

            this.serializerFactory = new DefaultSerializerFactory(serializers);

            // When
            var result = this.serializerFactory.GetSerializer("application/json");

            // Then
            result.ShouldBeNull();
        }
        public XmlFormatterExtensionsFixtures()
        {
            this.rootPathProvider = A.Fake <IRootPathProvider>();
            var environment = new DefaultNancyEnvironment();

            environment.AddValue(XmlConfiguration.Default);

            var serializerFactory =
                new DefaultSerializerFactory(new ISerializer[] { new DefaultXmlSerializer(environment) });

            this.responseFormatter =
                new DefaultResponseFormatter(this.rootPathProvider, new NancyContext(), serializerFactory, environment);

            this.model = new Person {
                FirstName = "Andy", LastName = "Pike"
            };
            this.response = this.responseFormatter.AsXml(model);
        }
Ejemplo n.º 13
0
        public void Should_return_default_serializer_if_no_other_match_could_be_made()
        {
            // Given
            var expectedSerializer =
                new DefaultJsonSerializer(GetTestingEnvironment());

            var serializers = new ISerializer[]
            {
                expectedSerializer,
                new TestableSerializer("application/xml"),
                new TestableSerializer("application/xml"),
            };

            this.serializerFactory = new DefaultSerializerFactory(serializers);

            // When
            var result = this.serializerFactory.GetSerializer("application/json");

            // Then
            result.ShouldBeSameAs(expectedSerializer);
        }
Ejemplo n.º 14
0
        public void Should_prioritize_non_default_serializer_match()
        {
            const string expectedMediaRange = "application/json";

            var expectedSerializer =
                new TestableSerializer(expectedMediaRange);

            var serializers = new ISerializer[]
            {
                new DefaultJsonSerializer(GetTestingEnvironment()),
                expectedSerializer
            };

            this.serializerFactory = new DefaultSerializerFactory(serializers);

            // When
            var result = this.serializerFactory.GetSerializer(expectedMediaRange);

            // Then
            result.ShouldBeSameAs(expectedSerializer);
        }
Ejemplo n.º 15
0
        public void Should_return_serializer_for_media_range(string expectedMediaRange)
        {
            // Given
            var expectedSerializer =
                new TestableSerializer(expectedMediaRange);

            var serializers = new ISerializer[]
            {
                expectedSerializer,
                new TestableSerializer("custom/foo"),
                new TestableSerializer("custom/bar"),
                new TestableSerializer("custom/baz"),
            };

            this.serializerFactory = new DefaultSerializerFactory(serializers);

            // When
            var result = this.serializerFactory.GetSerializer(expectedMediaRange);

            // Then
            result.ShouldBeSameAs(expectedSerializer);
        }