public void Add_WithTightThread_IsReasonablyWriteSafe()
        {
            const int ItemCount = 1000;

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            var t = new Thread(() =>
            {
                Parallel.For(0, ItemCount, (i) =>
                {
                    var response = new BasicHttpResponse()
                    {
                        StatusCode = i
                    };

                    factory.Add(response);
                });
            });

            t.Start();

            Assert.IsTrue(t.Join(5000));

            if (t.IsAlive)
            {
                t.Abort();
            }

            Assert.AreEqual(ItemCount, factory.Count);
        }
        public void Indexor_WhenFactoryIsDisposed_ThrowsExpcetion()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Dispose();

            Assert.Throws <ObjectDisposedException>(() => factory[0].GetBody());
        }
        public void CreateResponse_WithoutResponsesWithOrderedThenDefaultBehavior_ReturnsDefault()
        {
            var factory  = new StumpResponseFactory(ResponseFactoryBehavior.OrderedThenFailure);
            var response = factory.CreateResponse(new MockHttpRequest());

            Assert.NotNull(response);
            Assert.AreEqual(factory.FailureResponse.StatusCode, response.StatusCode);
        }
        public void RemoveAt_WhenCalledWithBadPosition_ThrowsException()
        {
            const int BadPosition = 0;

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            Assert.Throws <ArgumentOutOfRangeException>(() => factory.RemoveAt(BadPosition));
        }
        public void Add_WhenFactoryIsDisposed_ThrowsExpcetion()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Dispose();

            Assert.Throws <ObjectDisposedException>(() => factory.Add(new BasicHttpResponse()));
        }
        public void Add_WithValidResponse_AddsToCollection()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Add(new BasicHttpResponse());

            Assert.AreEqual(1, factory.Count);
        }
        public void HasResponse_GivenABehavior_WithResponsesAndFailureResponse_ReturnsTrue(ResponseFactoryBehavior behavior)
        {
            var factory = new StumpResponseFactory(behavior);

            factory.Add(new MockHttpResponse());

            Assert.IsTrue(factory.HasResponse);
        }
        public void CreateResponse_WhenFactoryIsDisposed_ThrowsExpcetion()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Dispose();

            Assert.Throws <ObjectDisposedException>(() => factory.CreateResponse(new MockHttpRequest()));
        }
        public void RemoveAt_WhenFactoryIsDisposed_ThrowsExpcetion()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Dispose();

            Assert.Throws <ObjectDisposedException>(() => factory.RemoveAt(0));
        }
        public void Constructor_WithEmptyEnumerableResponses_AddsResponses()
        {
            var responses = new List <IStumpsHttpResponse>();

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.Random, responses);

            Assert.AreEqual(ResponseFactoryBehavior.Random, factory.Behavior);
            Assert.AreEqual(responses.Count, factory.Count);
        }
        public void HasResponse_GivenABehavior_WhenFailureResponseIsNull_ReturnsCorrectResult(ResponseFactoryBehavior behavior, bool expectedResponse)
        {
            var factory = new StumpResponseFactory(behavior);

            factory.Add(new MockHttpResponse());
            factory.FailureResponse = null;

            Assert.AreEqual(expectedResponse, factory.HasResponse);
        }
 public void Add_WithNullResponse_ThrowsException()
 {
     Assert.That(
         () =>
     {
         var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);
         factory.Add(null);
     },
         Throws.Exception.TypeOf <ArgumentNullException>().With.Property("ParamName").EqualTo("response"));
 }
        public void Clear_WhenCalled_RemoveAllItemsInCollection()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Add(new BasicHttpResponse());

            factory.Clear();

            Assert.AreEqual(0, factory.Count);
        }
 public void Constructor_WithNullEnumerableResponses_ThrowsException()
 {
     Assert.That(
         () =>
     {
         IEnumerable <IStumpsHttpResponse> nullResponse = null;
         var factory = new StumpResponseFactory(ResponseFactoryBehavior.Random, nullResponse);
     },
         Throws.Exception.TypeOf <ArgumentNullException>().With.Property("ParamName").EqualTo("responses"));
 }
Exemplo n.º 15
0
        /// <summary>
        ///     Asserts that the <see cref="StumpResponseFactory"/> will respond with a <see cref="BasicHttpResponse"/>.
        /// </summary>
        /// <param name="responseFactory">The <see cref="StumpResponseFactory"/> to which the new <see cref="BasicHttpResponse"/> is added.</param>
        /// <returns>A <see cref="BasicHttpResponse"/> created for the <paramref name="responseFactory"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="responseFactory"/> is <c>null</c>.</exception>
        public static BasicHttpResponse Responds(this StumpResponseFactory responseFactory)
        {
            responseFactory = responseFactory ?? throw new ArgumentNullException(nameof(responseFactory));

            var response = new BasicHttpResponse();

            responseFactory.Add(response);

            return(response);
        }
 public void Constructor_WithNullDefaultResponse_ThrowsException()
 {
     Assert.That(
         () =>
     {
         IStumpsHttpResponse nullResponse = null;
         var factory = new StumpResponseFactory(nullResponse);
     },
         Throws.Exception.TypeOf <ArgumentNullException>().With.Property("ParamName").EqualTo("failureResponse"));
 }
Exemplo n.º 17
0
        public void ResposeFactory_GetSet_ReturnsResponseFactory()
        {
            var responseFactory = new StumpResponseFactory();

            var stump = new Stump("ABC")
            {
                Responses = responseFactory
            };

            Assert.AreEqual(responseFactory, stump.Responses);
        }
        public void Constructor_WithBehavior_InitializesCorrectDefaults()
        {
            const int NotImplementedStatusCode = 501;

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.Random);

            Assert.AreEqual(ResponseFactoryBehavior.Random, factory.Behavior);
            Assert.AreEqual(0, factory.Count);
            Assert.NotNull(factory.FailureResponse);
            Assert.AreEqual(NotImplementedStatusCode, factory.FailureResponse.StatusCode);
        }
        public void Constructor_WithFailureResponse_SetsResponseAndBehavior()
        {
            const int NotFoundStatusCode = 404;

            var factory = new StumpResponseFactory(HttpErrorResponses.HttpNotFound);

            Assert.AreEqual(ResponseFactoryBehavior.OrderedThenFailure, factory.Behavior);
            Assert.AreEqual(0, factory.Count);
            Assert.NotNull(factory.FailureResponse);
            Assert.AreEqual(NotFoundStatusCode, factory.FailureResponse.StatusCode);
        }
        public void RemoveAt_WithValidPosition_RemovesResponse()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            PopulateFactoryWithMultipleResponses(factory, 3);

            factory.RemoveAt(1);

            Assert.AreEqual(2, factory.Count);
            Assert.AreEqual(1, factory[0].StatusCode);
            Assert.AreEqual(3, factory[1].StatusCode);
        }
        public void CreateResponse_WithOrderedInfiniteBehavior_ReturnsExpectedValues()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            PopulateFactoryWithMultipleResponses(factory, 5);

            var responses = GetMultipleResponsesFromFactory(factory, 10);

            var expectedStatusCodeSequence = new int[] { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };

            Assert.IsTrue(AreStatusCodesEqual(responses, expectedStatusCodeSequence));
        }
        private List <IStumpsHttpResponse> GetMultipleResponsesFromFactory(StumpResponseFactory factory, int count)
        {
            var request   = new MockHttpRequest();
            var responses = new List <IStumpsHttpResponse>();

            for (var i = 0; i < count; i++)
            {
                responses.Add(factory.CreateResponse(request));
            }

            return(responses);
        }
        private void PopulateFactoryWithMultipleResponses(StumpResponseFactory factory, int count)
        {
            for (var i = 1; i <= count; i++)
            {
                var response = new BasicHttpResponse()
                {
                    StatusCode = i
                };

                factory.Add(response);
            }
        }
        public void ResetToBeginning_WhenCalled_ResetsOrderPosition()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            PopulateFactoryWithMultipleResponses(factory, 3);

            Assert.AreEqual(1, factory.CreateResponse(new MockHttpRequest()).StatusCode);
            Assert.AreEqual(2, factory.CreateResponse(new MockHttpRequest()).StatusCode);

            factory.ResetToBeginning();

            Assert.AreEqual(1, factory.CreateResponse(new MockHttpRequest()).StatusCode);
        }
        public void Constructor_WithMultipleEnumerableResponses_AddsResponses()
        {
            var responses = new List <IStumpsHttpResponse>
            {
                new BasicHttpResponse(),
                new BasicHttpResponse(),
                new BasicHttpResponse()
            };

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.Random, responses);

            Assert.AreEqual(ResponseFactoryBehavior.Random, factory.Behavior);
            Assert.AreEqual(responses.Count, factory.Count);
        }
        public void CreateResponse_WithRandomBehavior_ReturnsExpectedValues()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.Random);

            PopulateFactoryWithMultipleResponses(factory, 5);

            var responses = GetMultipleResponsesFromFactory(factory, 10);

            var unexpectedStatusCodeSequence = new int[] { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };

            Assert.IsFalse(AreStatusCodesEqual(responses, unexpectedStatusCodeSequence));

            responses.ForEach((r) => Assert.AreNotEqual(factory.FailureResponse.StatusCode, r.StatusCode));
        }
        public void Clear_WithTightThread_IsReasonablyWriteSafe()
        {
            var t = new Thread(() =>
            {
                var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);
                Assert.DoesNotThrow(() => Parallel.For(0, 1000, (i) => factory.Clear()));
            });

            t.Start();
            Assert.IsTrue(t.Join(1000));

            if (t.IsAlive)
            {
                t.Abort();
            }
        }
        public void Clear_WhenCalled_ResetsOrderPosition()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            PopulateFactoryWithMultipleResponses(factory, 3);

            Assert.AreEqual(1, factory.CreateResponse(new MockHttpRequest()).StatusCode);
            Assert.AreEqual(2, factory.CreateResponse(new MockHttpRequest()).StatusCode);

            factory.Clear();

            factory.Add(new BasicHttpResponse
            {
                StatusCode = 4
            });

            Assert.AreEqual(4, factory.CreateResponse(new MockHttpRequest()).StatusCode);
        }
        public void Indexor_WithValidIndex_ReturnsCorrectValue()
        {
            var responses = new List <IStumpsHttpResponse>
            {
                new BasicHttpResponse(),

                new BasicHttpResponse()
                {
                    StatusCode = 1
                },

                new BasicHttpResponse()
            };

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite, responses);

            Assert.AreSame(responses[1], factory[1]);
            Assert.AreNotSame(responses[0], factory[1]);
        }
        public void CreateResponse_WithOrderedInfiniteBehaviorInMultithreadedEnvironment_ReturnsExpectedValues()
        {
            var counts = new int[21];

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            PopulateFactoryWithMultipleResponses(factory, 20);

            var mockRequest = new MockHttpRequest();

            Parallel.For(0, 1000, (i) =>
            {
                var response = factory.CreateResponse(mockRequest);
                Interlocked.Increment(ref counts[response.StatusCode]);
            });

            for (var i = 1; i <= 20; i++)
            {
                Assert.AreEqual(50, counts[i]);
            }
        }