コード例 #1
0
        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);
        }
コード例 #2
0
        public void Indexor_WhenFactoryIsDisposed_ThrowsExpcetion()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Dispose();

            Assert.Throws <ObjectDisposedException>(() => factory[0].GetBody());
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void RemoveAt_WhenCalledWithBadPosition_ThrowsException()
        {
            const int BadPosition = 0;

            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            Assert.Throws <ArgumentOutOfRangeException>(() => factory.RemoveAt(BadPosition));
        }
コード例 #5
0
        public void Add_WhenFactoryIsDisposed_ThrowsExpcetion()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Dispose();

            Assert.Throws <ObjectDisposedException>(() => factory.Add(new BasicHttpResponse()));
        }
コード例 #6
0
        public void Add_WithValidResponse_AddsToCollection()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Add(new BasicHttpResponse());

            Assert.AreEqual(1, factory.Count);
        }
コード例 #7
0
        public void HasResponse_GivenABehavior_WithResponsesAndFailureResponse_ReturnsTrue(ResponseFactoryBehavior behavior)
        {
            var factory = new StumpResponseFactory(behavior);

            factory.Add(new MockHttpResponse());

            Assert.IsTrue(factory.HasResponse);
        }
コード例 #8
0
        public void CreateResponse_WhenFactoryIsDisposed_ThrowsExpcetion()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Dispose();

            Assert.Throws <ObjectDisposedException>(() => factory.CreateResponse(new MockHttpRequest()));
        }
コード例 #9
0
        public void RemoveAt_WhenFactoryIsDisposed_ThrowsExpcetion()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Dispose();

            Assert.Throws <ObjectDisposedException>(() => factory.RemoveAt(0));
        }
コード例 #10
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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
 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"));
 }
コード例 #13
0
        public void Clear_WhenCalled_RemoveAllItemsInCollection()
        {
            var factory = new StumpResponseFactory(ResponseFactoryBehavior.OrderedInfinite);

            factory.Add(new BasicHttpResponse());

            factory.Clear();

            Assert.AreEqual(0, factory.Count);
        }
コード例 #14
0
 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"));
 }
コード例 #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);
        }
コード例 #16
0
 public void Constructor_WithNullDefaultResponse_ThrowsException()
 {
     Assert.That(
         () =>
     {
         IStumpsHttpResponse nullResponse = null;
         var factory = new StumpResponseFactory(nullResponse);
     },
         Throws.Exception.TypeOf <ArgumentNullException>().With.Property("ParamName").EqualTo("failureResponse"));
 }
コード例 #17
0
ファイル: StumpTests.cs プロジェクト: mtharrington/stumps
        public void ResposeFactory_GetSet_ReturnsResponseFactory()
        {
            var responseFactory = new StumpResponseFactory();

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

            Assert.AreEqual(responseFactory, stump.Responses);
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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));
        }
コード例 #22
0
        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);
        }
コード例 #23
0
        private void PopulateFactoryWithMultipleResponses(StumpResponseFactory factory, int count)
        {
            for (var i = 1; i <= count; i++)
            {
                var response = new BasicHttpResponse()
                {
                    StatusCode = i
                };

                factory.Add(response);
            }
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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);
        }
コード例 #26
0
        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));
        }
コード例 #27
0
        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();
            }
        }
コード例 #28
0
        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);
        }
コード例 #29
0
        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]);
        }
コード例 #30
0
        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]);
            }
        }