public void WriteFlushesToStreamButDoesntCloseItIfHadToCreateItsOwnWriter()
        {
            SparkCodec sparkCodec = new SparkCodecBuilder().Build();
            var httpEntity = MockRepository.GenerateStub<IHttpEntity>();
            var streamToWrite = MockRepository.GenerateStub<Stream>();
            streamToWrite.Stub(x => x.CanWrite).Return(true);
            httpEntity.Stub(x => x.Stream).Return(streamToWrite);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            streamToWrite.AssertWasCalled(x => x.Flush());
            streamToWrite.AssertWasNotCalled(x => x.Close());
        }
        public void WriteFlushesToStreamButDoesntCloseItIfHadToCreateItsOwnWriter()
        {
            SparkCodec sparkCodec    = new SparkCodecBuilder().Build();
            var        httpEntity    = MockRepository.GenerateStub <IHttpEntity>();
            var        streamToWrite = MockRepository.GenerateStub <Stream>();

            streamToWrite.Stub(x => x.CanWrite).Return(true);
            httpEntity.Stub(x => x.Stream).Return(streamToWrite);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            streamToWrite.AssertWasCalled(x => x.Flush());
            streamToWrite.AssertWasNotCalled(x => x.Close());
        }
        public void WriteToPassesViewDataToRender()
        {
            var renderer   = MockRepository.GenerateStub <ISparkRenderer>();
            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var viewData = new object();

            sparkCodec.WriteTo(viewData, MockRepository.GenerateStub <IHttpEntitySupportingTextWriter>(), new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                                         Arg <object> .Is.Equal(viewData),
                                         Arg <TextWriter> .Is.Anything,
                                         Arg <Dictionary <string, string> > .Is.Anything
                                         ));
        }
        public void WriteCreatesDeterministicWriterWithCorrectEncoding()
        {
            const string funnyCharacters = "úĀąü";
            ISparkRenderer renderer = new StubSparkRendererThatWritesStuff(funnyCharacters);
            SparkCodec sparkCodec = new SparkCodecBuilder().With(renderer).Build();
            var httpEntity = MockRepository.GenerateStub<IHttpEntity>();
            var stream = new MemoryStream();
            Assert.That(stream.CanRead, Is.True);
            httpEntity.Stub(x => x.Stream).Return(stream);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream, new UTF8Encoding());
            Assert.That(reader.ReadToEnd(), Is.EqualTo(funnyCharacters));
        }
        public void WriteWritesToStreamIfHttpEntityDoesNotSupportTextWriter()
        {
            const string expectedContents = "abcdefg";
            var          renderer         = new StubSparkRendererThatWritesStuff(expectedContents);
            SparkCodec   sparkCodec       = new SparkCodecBuilder().With(renderer).Build();

            var httpEntity = MockRepository.GenerateStub <IHttpEntity>();
            var stream     = new MemoryStream();

            httpEntity.Stub(x => x.Stream).Return(stream);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream);

            Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedContents));
        }
        public void WritePassesConfigObjectAsDictionaryToRender()
        {
            var renderer   = MockRepository.GenerateStub <ISparkRenderer>();
            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var configData = new{ foo = "bar", bat = 123 };
            IDictionary <string, string> expectedConfiguration = configData.ToCaseInvariantDictionary();

            sparkCodec.Configuration = configData;

            sparkCodec.WriteTo(new object(), MockRepository.GenerateStub <IHttpEntitySupportingTextWriter>(), new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                                         Arg <object> .Is.Anything,
                                         Arg <TextWriter> .Is.Anything,
                                         Arg <Dictionary <string, string> > .Is.Equal(expectedConfiguration)
                                         ));
        }
        public void WriteCreatesDeterministicWriterWithCorrectEncoding()
        {
            const string   funnyCharacters = "úĀąü";
            ISparkRenderer renderer        = new StubSparkRendererThatWritesStuff(funnyCharacters);
            SparkCodec     sparkCodec      = new SparkCodecBuilder().With(renderer).Build();
            var            httpEntity      = MockRepository.GenerateStub <IHttpEntity>();
            var            stream          = new MemoryStream();

            Assert.That(stream.CanRead, Is.True);
            httpEntity.Stub(x => x.Stream).Return(stream);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream, new UTF8Encoding());

            Assert.That(reader.ReadToEnd(), Is.EqualTo(funnyCharacters));
        }
        public void WriteToUsesExistingTextWriterIfHttpEntitySupportsTextWriter()
        {
            var renderer           = MockRepository.GenerateStub <ISparkRenderer>();
            var textWriter         = new StringWriter();
            var supportsTextWriter = MockRepository.GenerateStub <IHttpEntitySupportingTextWriter>();

            supportsTextWriter.Stub(x => x.TextWriter).Return(textWriter);

            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            sparkCodec.Configuration = new object();

            sparkCodec.WriteTo(new object(), supportsTextWriter, new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                                         Arg <object> .Is.Anything,
                                         Arg <TextWriter> .Is.Equal(textWriter),
                                         Arg <Dictionary <string, string> > .Is.Anything
                                         ));
        }
        public void WritePassesConfigObjectAsDictionaryToRender()
        {
            var renderer = MockRepository.GenerateStub<ISparkRenderer>();
            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var configData = new{foo="bar",bat=123};
            IDictionary<string, string> expectedConfiguration = configData.ToCaseInvariantDictionary();
            sparkCodec.Configuration = configData;

            sparkCodec.WriteTo(new object(), MockRepository.GenerateStub<IHttpEntitySupportingTextWriter>(), new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                Arg<object>.Is.Anything,
                Arg<TextWriter>.Is.Anything,
                Arg<Dictionary<string, string>>.Is.Equal(expectedConfiguration)
                ));
        }
        public void WriteWritesToStreamIfHttpEntityDoesNotSupportTextWriter()
        {
            const string expectedContents = "abcdefg";
            var renderer = new StubSparkRendererThatWritesStuff(expectedContents);
            SparkCodec sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var httpEntity = MockRepository.GenerateStub<IHttpEntity>();
            var stream = new MemoryStream();
            httpEntity.Stub(x => x.Stream).Return(stream);

            sparkCodec.WriteTo(null, httpEntity, new[]{"a", "b"});

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream);
            Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedContents));
        }
        public void WriteToUsesExistingTextWriterIfHttpEntitySupportsTextWriter()
        {
            var renderer = MockRepository.GenerateStub<ISparkRenderer>();
            var textWriter = new StringWriter();
            var supportsTextWriter = MockRepository.GenerateStub<IHttpEntitySupportingTextWriter>();
            supportsTextWriter.Stub(x => x.TextWriter).Return(textWriter);

            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();
            sparkCodec.Configuration = new object();

            sparkCodec.WriteTo(new object(), supportsTextWriter, new[] {"A", "B"});

            renderer.AssertWasCalled(x => x.Render(
                Arg<object>.Is.Anything,
                Arg<TextWriter>.Is.Equal(textWriter),
                Arg<Dictionary<string,string>>.Is.Anything
                ));
        }
        public void WriteToPassesViewDataToRender()
        {
            var renderer = MockRepository.GenerateStub<ISparkRenderer>();
            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var viewData = new object();

            sparkCodec.WriteTo(viewData, MockRepository.GenerateStub<IHttpEntitySupportingTextWriter>(), new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                Arg<object>.Is.Equal(viewData),
                Arg<TextWriter>.Is.Anything,
                Arg<Dictionary<string, string>>.Is.Anything
                ));
        }