public void CaptureThrowsWhenNoCapturingStream()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content), new MessageBodyCaptureDescriptor("url", MessageBodyCaptureMode.Claimed)))
     {
         Invoking(() => stream.Capture()).Should().Throw <InvalidOperationException>()
         .WithMessage("TrackingStream cannot be captured unless it has been setup with another capturing stream to replicate its payload to.");
     }
 }
 public void InnerStreamIsWrappedByReplicatingStreamIfTracked()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         stream.SetupCapture(new MessageBodyCaptureDescriptor("some-data", MessageBodyCaptureMode.Claimed), new MemoryStream());
         Reflector.GetProperty(stream, "InnerStream").Should().BeOfType <ReplicatingReadStream>();
     }
 }
 public void CaptureThrowsWhenNoCaptureDescriptor()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         Invoking(() => stream.Capture()).Should().Throw <InvalidOperationException>()
         .WithMessage("TrackingStream cannot be captured because its Descriptor is null and has not been initialized for tracking.");
     }
 }
 public void InnerStreamIsWrappedByMarkableStream()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         stream.AsMarkable();
         Reflector.GetProperty(stream, "InnerStream").Should().BeOfType <MarkableForwardOnlyEventingReadStream>();
     }
 }
 public void CaptureThrowsWhenCaptureModeIsNotClaimed()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         stream.SetupCapture(new MessageBodyCaptureDescriptor("some-data", MessageBodyCaptureMode.Unclaimed));
         Invoking(() => stream.Capture()).Should().Throw <InvalidOperationException>()
         .WithMessage("TrackingStream cannot be captured because its Descriptor's CaptureMode has not been set to Claimed but to Unclaimed.");
     }
 }
 public void SetupCaptureAsUnclaimedThrowsWhenCaptureModeIsOther()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         Invoking(() => stream.SetupCapture(new MessageBodyCaptureDescriptor("some-data", MessageBodyCaptureMode.Claimed)))
         .Should().Throw <InvalidOperationException>()
         .WithMessage("TrackingStream's capture cannot be setup with a CaptureMode of Claimed; other CaptureMode than Unclaimed requires a capturing stream.");
     }
 }
 public void SetupCaptureAsClaimedThrowsWithoutCapturingStream()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         Invoking(() => stream.SetupCapture(new MessageBodyCaptureDescriptor("some-data", MessageBodyCaptureMode.Claimed), null))
         .Should().Throw <ArgumentNullException>()
         .Which.ParamName.Should().Be("capturingStream");
     }
 }
 public void CaptureSetupResetMarkablePosition()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         var ms = stream.AsMarkable();
         ms.Drain();
         stream.Position.Should().Be(_content.Length);
         stream.SetupCapture(new MessageBodyCaptureDescriptor("some-data", MessageBodyCaptureMode.Unclaimed));
         stream.Position.Should().Be(0);
     }
 }
 public void CaptureDrainsInnerStream()
 {
     using (var innerStream = new MemoryStream(_content))
         using (var stream = new TrackingStream(innerStream))
         {
             stream.SetupCapture(new MessageBodyCaptureDescriptor("some-data", MessageBodyCaptureMode.Claimed), new MemoryStream());
             innerStream.Position.Should().Be(0);
             Invoking(() => stream.Capture()).Should().NotThrow();
             innerStream.Position.Should().Be(innerStream.Length);
         }
 }
        public void PayloadIsBeingRedeemed()
        {
            Invoking(() => new TrackingStream(new MemoryStream(), new MessageBodyCaptureDescriptor("url", MessageBodyCaptureMode.Unclaimed)))
            .Should().Throw <ArgumentException>()
            .WithMessage(
                "A TrackingStream, whose payload is being redeemed, cannot be instantiated with a CaptureDescriptor having a CaptureMode of Unclaimed; the only compliant CaptureMode is Claimed.*");

            using (var stream = new TrackingStream(new MemoryStream(_content), new MessageBodyCaptureDescriptor("url", MessageBodyCaptureMode.Claimed)))
            {
                stream.IsRedeemed.Should().BeTrue();
            }
        }
        public void SetupCaptureThrowsIfCaptureDescriptorHasAlreadyBeenSetup()
        {
            using (var stream = new TrackingStream(new MemoryStream(_content), new MessageBodyCaptureDescriptor("some-data", MessageBodyCaptureMode.Claimed)))
            {
                Invoking(() => stream.SetupCapture(new MessageBodyCaptureDescriptor("other-data", MessageBodyCaptureMode.Unclaimed)))
                .Should().Throw <InvalidOperationException>()
                .WithMessage("TrackingStream's capture has already been setup and cannot be overwritten.");

                Invoking(() => stream.SetupCapture(new MessageBodyCaptureDescriptor("other-data", MessageBodyCaptureMode.Claimed), new MemoryStream()))
                .Should().Throw <InvalidOperationException>()
                .WithMessage("TrackingStream's capture has already been setup and cannot be overwritten.");
            }
        }
 public void PayloadIsNotBeingRedeemed()
 {
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         stream.IsRedeemed.Should().BeFalse();
         stream.SetupCapture(new MessageBodyCaptureDescriptor("some-data", MessageBodyCaptureMode.Unclaimed));
         stream.IsRedeemed.Should().BeFalse();
     }
     using (var stream = new TrackingStream(new MemoryStream(_content)))
     {
         stream.IsRedeemed.Should().BeFalse();
         stream.SetupCapture(new MessageBodyCaptureDescriptor("url", MessageBodyCaptureMode.Claimed), new MemoryStream());
         stream.IsRedeemed.Should().BeFalse();
     }
 }
        public void EventsAreNotFiredThroughMarkableInnerStream()
        {
            using (var stream = new TrackingStream(new MemoryStream(_content)))
            {
                var edgeEventsCount = 0;
                stream.AfterLastReadEvent   += (_, _) => ++ edgeEventsCount;
                stream.BeforeFirstReadEvent += (_, _) => ++ edgeEventsCount;
                var eventsCount = 0;
                stream.ReadEvent += (_, _) => ++ eventsCount;

                var markableForwardOnlyEventingReadStream = stream.AsMarkable();
                edgeEventsCount.Should().Be(0);
                eventsCount.Should().Be(0);

                markableForwardOnlyEventingReadStream.Drain();
                edgeEventsCount.Should().Be(0);
                eventsCount.Should().Be(0);
            }
        }