예제 #1
0
        /// <inheritdoc />
        public async Task <RecordedStream> ReadStreamForward(string stream, int fromVersion, CancellationToken token = default(CancellationToken))
        {
            Guard.StreamName(stream, nameof(stream));

            _settings.Logger.Trace("Reading stream '{0}' forward from version {1}...", stream, fromVersion);

            using (var connection = new SqlConnection(_settings.ConnectionString))
                using (var cmd = connection.CreateCommandForReadStreamForward(stream, fromVersion))
                {
                    await connection.OpenAsync(token).NotOnCapturedContext();

                    using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SingleResult, token).NotOnCapturedContext())
                    {
                        if (!reader.HasRows)
                        {
                            _settings.Logger.Trace("Read 0 messages for '{0}' stream from version {1}.", stream, fromVersion);
                            return(RecordedStream.Empty());
                        }

                        var messages = new List <RecordedMessage>(20); // 20? How many will be enough?
                        while (await reader.ReadAsync(token).NotOnCapturedContext())
                        {
                            messages.Add(reader.GetRecordedMessage());
                        }
                        _settings.Logger.Trace("Read {0} messages for '{1}' stream from version {2}.", messages.Count, stream, fromVersion);
                        return(new RecordedStream(messages.ToArray()));
                    }
                }
        }
        public void DeflatedStreamIsStoredUncompressed()
        {
            // ARRANGE
            var content = "Hello World";

            var compressed = new MemoryStream();

            using (var deflate = new DeflateStream(compressed, CompressionMode.Compress, leaveOpen: true))
            {
                new MemoryStream(Encoding.UTF8.GetBytes(content)).CopyTo(deflate);
            }

            var recordedStream = new RecordedStream(
                compressed.ToArray(),
                HttpWebResponseCreator.Create(
                    new Uri("http://fakeSite.fake"),
                    "POST",
                    HttpStatusCode.OK,
                    compressed,
                    new WebHeaderCollection
            {
                { HttpRequestHeader.ContentEncoding, "deflate" }
            }));

            // ACT
            var toString = recordedStream.ToString();

            // ASSERT
            recordedStream.IsEncoded.ShouldBeFalse();
            recordedStream.IsDeflateCompressed.ShouldBeTrue();

            toString.ShouldEqual(content);
        }
        public void ToStringReturnsStringContentWhenStreamIsNotEncoded()
        {
            // ARRANGE
            var content = "Hello World";

            var recordedStream = new RecordedStream(
                Encoding.UTF8.GetBytes(content),
                new HttpWebRequestWrapper(new Uri("http://fakeSite.fake")));

            // ACT
            var toString = recordedStream.ToString();

            // ASSERT
            recordedStream.IsEncoded.ShouldBeFalse();
            toString.ShouldEqual(content);
        }
        public void ToStringReturnsStringContentWhenStreamIsEncoded()
        {
            // ARRANGE
            var content = "Hello World";

            var recordedStream = new RecordedStream(
                Encoding.UTF8.GetBytes(content),
                new HttpWebRequestWrapper(new Uri("http://fakeSite.fake"))
            {
                // set content type to force string content to be
                // encoded
                ContentType = "image/png"
            });

            // ACT
            var toString = recordedStream.ToString();

            // ASSERT
            recordedStream.IsEncoded.ShouldBeTrue();
            toString.ShouldEqual(content);
        }