public void CanCustomizeResponseBuilder()
        {
            // ARRANGE
            var customResponseBody = "Custom Response";
            var recordedRequest    = new RecordedRequest
            {
                Url    = "http://fakeSite.fake",
                Method = "GET"
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession)
            {
                RecordedResultResponseBuilder =
                    (recordedReq, interceptedReq) => interceptedReq.HttpWebResponseCreator.Create(customResponseBody)
            };

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            // ACT
            var response = request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            using (var sr = new StreamReader(response.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(customResponseBody);
        }
        public void BuilderSetsInvalidOperationException()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url               = "http://fakeSite.fake",
                Method            = "GET",
                ResponseException = new RecordedResponseException
                {
                    Message = "Test Exception Message",
                    Type    = typeof(InvalidOperationException)
                }
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            // ACT
            var exception = Record.Exception(() => request.GetResponse());

            // ASSERT
            exception.ShouldNotBeNull();
            exception.ShouldBeType <InvalidOperationException>();
            exception.Message.ShouldEqual(recordedRequest.ResponseException.Message);
        }
        public void CanCustomizeNotFoundBehavior()
        {
            // ARRANGE
            var customNotFoundResponseBody = "Not Found";

            var recordingSession = new RecordingSession[0];

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession)
            {
                RequestNotFoundResponseBuilder = req =>
                                                 req.HttpWebResponseCreator.Create(customNotFoundResponseBody)
            };

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri("http://fakeSite.fake"));

            // ACT
            var response = request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            using (var sr = new StreamReader(response.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(customNotFoundResponseBody);
        }
        public void BuilderSetsResponseBody()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url          = "http://fakeSite.fake",
                Method       = "GET",
                ResponseBody = "Response Body"
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            // ACT
            var response = request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            using (var sr = new StreamReader(response.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest.ResponseBody.SerializedStream);
        }
        public void BuilderSetsResponseStatusCode()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url                = "http://fakeSite.fake",
                Method             = "GET",
                ResponseStatusCode = HttpStatusCode.Forbidden
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            // ACT
            var response = (HttpWebResponse)request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();
            response.StatusCode.ShouldEqual(recordedRequest.ResponseStatusCode);
        }
        public void BuilderSetsResponseHeaders()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url             = "http://fakeSite.fake",
                Method          = "GET",
                ResponseHeaders = new RecordedHeaders {
                    { "Header1", new [] { "Header1Value" } }
                }
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            // ACT
            var response = (HttpWebResponse)request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            recordedRequest.ResponseHeaders.ShouldEqual((RecordedHeaders)response.Headers);
        }
        public void MatchesOnZippedPayload()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url            = "http://fakeSite.fake",
                Method         = "POST",
                RequestPayload = new RecordedStream
                {
                    SerializedStream    = "Request 1",
                    IsGzippedCompressed = true
                },
                RequestHeaders = new RecordedHeaders
                {
                    { "Content-Type", new [] { "text/plain" } }
                },
                ResponseBody = "Response 1"
            };

            var recordingSession = new RecordingSession
            {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            request.Method      = "POST";
            request.ContentType = "text/plain";

            using (var input = new MemoryStream(Encoding.UTF8.GetBytes(recordedRequest.RequestPayload.SerializedStream)))
                using (var compressed = new MemoryStream())
                    using (var zip = new GZipStream(compressed, CompressionMode.Compress, leaveOpen: true))
                    {
                        input.CopyTo(zip);
                        zip.Close();
                        compressed.Seek(0, SeekOrigin.Begin);
                        compressed.CopyTo(request.GetRequestStream());
                    }

            // ACT
            var response = request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            using (var sr = new StreamReader(response.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest.ResponseBody.SerializedStream);
        }
        public void CanSetRecordedRequestsToOnlyMatchOnce()
        {
            // ARRANGE
            var recordedRequest1 = new RecordedRequest
            {
                Url          = "http://fakeSite.fake/1",
                Method       = "GET",
                ResponseBody = "Response 1"
            };

            var recordedRequest2 = new RecordedRequest
            {
                Url          = "http://fakeSite.fake/2",
                Method       = recordedRequest1.Method,
                ResponseBody = "Response 2"
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(
                new RecordingSession {
                RecordedRequests = { recordedRequest1, recordedRequest1, recordedRequest2 }
            })
            {
                AllowReplayingRecordedRequestsMultipleTimes = false
            };

            var creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            // ACT
            var response1a = (HttpWebResponse)creator.Create(new Uri(recordedRequest1.Url)).GetResponse();
            var response1b = (HttpWebResponse)creator.Create(new Uri(recordedRequest1.Url)).GetResponse();
            var response1c = (HttpWebResponse)creator.Create(new Uri(recordedRequest1.Url)).GetResponse();
            var response2a = (HttpWebResponse)creator.Create(new Uri(recordedRequest2.Url)).GetResponse();
            var response2b = (HttpWebResponse)creator.Create(new Uri(recordedRequest2.Url)).GetResponse();

            // ASSERT
            response1a.ShouldNotBeNull();
            response1b.ShouldNotBeNull();
            response1c.ShouldNotBeNull();
            response2a.ShouldNotBeNull();
            response2b.ShouldNotBeNull();

            using (var sr = new StreamReader(response1a.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest1.ResponseBody.SerializedStream);

            using (var sr = new StreamReader(response1b.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest1.ResponseBody.SerializedStream);

            using (var sr = new StreamReader(response2a.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest2.ResponseBody.SerializedStream);

            response1c.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
            response2b.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
        public void CanPlaybackFromMultipleRecordingSessions()
        {
            // ARRANGE
            var recordedRequest1 = new RecordedRequest
            {
                Url          = "http://fakeSite.fake/1",
                Method       = "GET",
                ResponseBody = "Response 1"
            };

            var recordedRequest2 = new RecordedRequest
            {
                Url          = "http://fakeSite.fake/2",
                Method       = recordedRequest1.Method,
                ResponseBody = "Response 2"
            };

            var recordingSession1 = new RecordingSession
            {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest1
                }
            };

            var recordingSession2 = new RecordingSession
            {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest2
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession1, recordingSession2);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request1 = creator.Create(new Uri(recordedRequest1.Url));
            var request2 = creator.Create(new Uri(recordedRequest2.Url));

            // ACT
            var response1 = request1.GetResponse();
            var response2 = request2.GetResponse();

            // ASSERT
            response1.ShouldNotBeNull();
            response2.ShouldNotBeNull();

            using (var sr = new StreamReader(response1.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest1.ResponseBody.SerializedStream);

            using (var sr = new StreamReader(response2.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest2.ResponseBody.SerializedStream);
        }
        public void BuilderSetsWebExceptionWithResponse()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url               = "http://fakeSite.fake",
                Method            = "GET",
                ResponseException = new RecordedResponseException
                {
                    Message            = "Test Exception Message",
                    Type               = typeof(WebException),
                    WebExceptionStatus = WebExceptionStatus.ConnectionClosed
                },
                ResponseBody    = "Fake Error Response",
                ResponseHeaders = new RecordedHeaders {
                    { "header1", new [] { "value1" } }
                },
                ResponseStatusCode = HttpStatusCode.InternalServerError
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            // ACT
            var exception            = Record.Exception(() => request.GetResponse());
            var webException         = exception as WebException;
            var webExceptionResponse = webException.Response as HttpWebResponse;

            // ASSERT
            webException.ShouldNotBeNull();
            webException.Message.ShouldEqual(recordedRequest.ResponseException.Message);
            webException.Status.ShouldEqual(recordedRequest.ResponseException.WebExceptionStatus.Value);

            webExceptionResponse.ShouldNotBeNull();
            Assert.Equal(recordedRequest.ResponseHeaders, (RecordedHeaders)webExceptionResponse.Headers);
            webExceptionResponse.StatusCode.ShouldEqual(recordedRequest.ResponseStatusCode);
            webExceptionResponse.ContentLength.ShouldBeGreaterThan(0);

            using (var sr = new StreamReader(webExceptionResponse.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest.ResponseBody.SerializedStream);
        }
        public void CanDynamicallyChangeRequestsMidPlayback()
        {
            // ARRANGE
            var requestUrl    = "http://www.github.com";
            var fakeResponse1 = "response1";
            var fakeResponse2 = "response2";

            var builder = new RecordingSessionInterceptorRequestBuilder();

            builder.RecordedRequests.Add(new RecordedRequest
            {
                Url          = requestUrl,
                Method       = "GET",
                ResponseBody = fakeResponse1
            });

            using (new HttpWebRequestWrapperSession(new HttpWebRequestWrapperInterceptorCreator(
                                                        builder)))
            {
                // ACT
                var response1 = WebRequest.Create(requestUrl).GetResponse();

                builder.RecordedRequests.Clear();
                builder.RecordedRequests.Add(new RecordedRequest
                {
                    Url          = requestUrl,
                    Method       = "GET",
                    ResponseBody = fakeResponse2
                });

                var response2 = WebRequest.Create(requestUrl).GetResponse();

                builder.RecordedRequests.Clear();

                var response3 = (HttpWebResponse)WebRequest.Create(requestUrl).GetResponse();

                // ASSERT
                response1.ShouldNotBeNull();
                response2.ShouldNotBeNull();
                response3.ShouldNotBeNull();

                using (var sr = new StreamReader(response1.GetResponseStream()))
                    sr.ReadToEnd().ShouldEqual(fakeResponse1);

                using (var sr = new StreamReader(response2.GetResponseStream()))
                    sr.ReadToEnd().ShouldEqual(fakeResponse2);

                response3.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
            }
        }
        public void BuilderAlwaysSetsWebExceptionResponseWhenStatusIsProtocolError()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url               = "http://fakeSite.fake",
                Method            = "GET",
                ResponseException = new RecordedResponseException
                {
                    Message            = "Test Exception Message",
                    Type               = typeof(WebException),
                    WebExceptionStatus = WebExceptionStatus.ProtocolError
                },
                ResponseHeaders = new RecordedHeaders
                {
                    { "header1", new[] { "value1" } }
                },
                ResponseStatusCode = HttpStatusCode.Unauthorized
                                     //intentionally leave ResponseBody null
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            // ACT
            var exception            = Record.Exception(() => request.GetResponse());
            var webException         = exception as WebException;
            var webExceptionResponse = webException.Response as HttpWebResponse;

            // ASSERT
            webException.ShouldNotBeNull();
            webException.Message.ShouldEqual(recordedRequest.ResponseException.Message);
            webException.Status.ShouldEqual(recordedRequest.ResponseException.WebExceptionStatus.Value);

            webExceptionResponse.ShouldNotBeNull();
            Assert.Equal(recordedRequest.ResponseHeaders, (RecordedHeaders)webExceptionResponse.Headers);
            webExceptionResponse.StatusCode.ShouldEqual(recordedRequest.ResponseStatusCode);
            // no response content in recordedResponse, so content length should be 0
            webExceptionResponse.ContentLength.ShouldEqual(0);
        }
        public void DefaultNotFoundBehaviorReturns404()
        {
            // ARRANGE
            var recordingSession = new RecordingSession[0];

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri("http://fakeSite.fake"));

            // ACT
            var response = (HttpWebResponse)request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();
            response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
        public void CanSetOnMatchEventHandler()
        {
            var matchCallCount = 0;

            var requestUrl      = new Uri("http://fakeSite.fake/");
            var recordedRequest = new RecordedRequest
            {
                Url                = "http://fakeSite.fake/",
                Method             = "GET",
                ResponseStatusCode = HttpStatusCode.Found
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession)
            {
                OnMatch = (recordedReq, interceptedReq, httpWebResponse, exception) =>
                {
                    recordedReq.ShouldEqual(recordedRequest);
                    interceptedReq.HttpWebRequest.RequestUri.ShouldEqual(new Uri(recordedRequest.Url));
                    httpWebResponse.StatusCode.ShouldEqual(recordedRequest.ResponseStatusCode);

                    matchCallCount++;
                }
            };

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(requestUrl);

            // ACT
            var response = request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            matchCallCount.ShouldEqual(1);
        }
        public void CanPlaybackDeflatedResponse()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url          = "http://fakeSite.fake",
                Method       = "GET",
                ResponseBody = new RecordedStream
                {
                    SerializedStream    = "Response 1",
                    IsDeflateCompressed = true
                },
                ResponseHeaders = new RecordedHeaders
                {
                    { "Content-Encoding", new [] { "deflate" } }
                }
            };

            var recordingSession = new RecordingSession
            {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = (HttpWebRequest)creator.Create(new Uri(recordedRequest.Url));

            request.AutomaticDecompression = DecompressionMethods.Deflate;

            // ACT
            var response = request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            using (var sr = new StreamReader(response.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest.ResponseBody.SerializedStream);
        }
        public void CanChangeDefaultNotFoundBehaviorToPassThrough()
        {
            // ARRANGE
            var recordingSession = new RecordingSession[0];

            var requestBuilder =
                new RecordingSessionInterceptorRequestBuilder(
                    RequestNotFoundBehavior.PassThrough,
                    recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri("http://github.com"));

            // ACT
            var response = (HttpWebResponse)request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            using (var sr = new StreamReader(response.GetResponseStream()))
                sr.ReadToEnd().ShouldContain("<html");
        }
        public void BuilderSetsWebException()
        {
            // ARRANGE
            var recordedRequest = new RecordedRequest
            {
                Url               = "http://fakeSite.fake",
                Method            = "GET",
                ResponseException = new RecordedResponseException
                {
                    Message            = "Test Exception Message",
                    Type               = typeof(WebException),
                    WebExceptionStatus = WebExceptionStatus.ConnectionClosed
                }
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(new Uri(recordedRequest.Url));

            // ACT
            var exception    = Record.Exception(() => request.GetResponse());
            var webException = exception as WebException;

            // ASSERT
            webException.ShouldNotBeNull();
            webException.Message.ShouldEqual(recordedRequest.ResponseException.Message);
            webException.Status.ShouldEqual(recordedRequest.ResponseException.WebExceptionStatus.Value);
            webException.Response.ShouldBeNull();
        }
        public void CanCustomizeMatchingAlgorithm()
        {
            // ARRANGE
            var requestUrl      = new Uri("http://fakeSite.fake/2");
            var recordedRequest = new RecordedRequest
            {
                // intentionally use a different url from requestUrl
                Url          = "http://fakeSite.fake/3",
                Method       = "GET",
                ResponseBody = "Custom Matching Algorithm"
            };

            var recordingSession = new RecordingSession {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession)
            {
                MatchingAlgorithm = (interceptedReq, recordedReq) => interceptedReq.HttpWebRequest.RequestUri == requestUrl
            };

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request = creator.Create(requestUrl);

            // ACT
            var response = request.GetResponse();

            // ASSERT
            response.ShouldNotBeNull();

            using (var sr = new StreamReader(response.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest.ResponseBody.SerializedStream);
        }
        public void MatchesOnUniqueRequestHeaders()
        {
            // ARRANGE
            var recordedRequest1 = new RecordedRequest
            {
                Url            = "http://fakeSite.fake",
                Method         = "GET",
                RequestHeaders = new RecordedHeaders {
                    { "Request1", new [] { "Request1Value" } }
                },
                ResponseBody = "Response 1"
            };

            var recordedRequest2 = new RecordedRequest
            {
                Url            = recordedRequest1.Url,
                Method         = recordedRequest1.Method,
                RequestHeaders = new RecordedHeaders {
                    { "Request2", new [] { "Request2Value" } }
                },
                ResponseBody = "Response 2"
            };

            var recordingSession = new RecordingSession
            {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest1, recordedRequest2
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request1 = creator.Create(new Uri(recordedRequest1.Url));

            request1.Headers = new WebHeaderCollection {
                recordedRequest1.RequestHeaders
            };

            var request2 = creator.Create(new Uri(recordedRequest2.Url));

            request2.Headers = new WebHeaderCollection {
                recordedRequest2.RequestHeaders
            };


            // ACT
            var response1 = request1.GetResponse();
            var response2 = request2.GetResponse();

            // ASSERT
            response1.ShouldNotBeNull();
            response2.ShouldNotBeNull();

            using (var sr = new StreamReader(response1.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest1.ResponseBody.SerializedStream);

            using (var sr = new StreamReader(response2.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest2.ResponseBody.SerializedStream);
        }
        public async Task CanInterceptMultipleSequentialPosts()
        {
            // ARRANGE
            var numberOfSequentialRequests = 20;

            var recordedRequest = new RecordedRequest
            {
                Method         = "POST",
                Url            = "http://fakeSite.fake/",
                RequestPayload = new RecordedStream
                {
                    SerializedStream = "Test Request"
                },
                ResponseStatusCode = HttpStatusCode.OK,
                ResponseBody       = new RecordedStream
                {
                    SerializedStream = "Test Response",
                    // important - force gzip so a compression stream gets plumbed
                    // through the http client as that changes behavior
                    IsGzippedCompressed = true
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(
                new RecordingSession
            {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest
                }
            })
            {
                MatchingAlgorithm = (intercepted, recorded) =>
                                    string.Equals(
                    intercepted.HttpWebRequest.RequestUri.ToString(),
                    recorded.Url,
                    StringComparison.OrdinalIgnoreCase)
            };

            // ACT
            using (new HttpWebRequestWrapperSession(new HttpWebRequestWrapperInterceptorCreator(requestBuilder)))
            {
                for (var i = 0; i < numberOfSequentialRequests; i++)
                {
                    var httpClient = new HttpClient(new WebRequestHandler());

                    var message = new HttpRequestMessage(HttpMethod.Post, recordedRequest.Url)
                    {
                        Content = new StringContent(recordedRequest.RequestPayload.ToString())
                    };

                    var response = await httpClient.SendAsync(message);

                    // decompress stream
                    var responseStream = await response.Content.ReadAsStreamAsync();

                    using (var zip = new GZipStream(responseStream, CompressionMode.Decompress, leaveOpen: true))
                        using (var sr = new StreamReader(zip))
                            //using (var sr = new StreamReader(responseStream))
                            sr.ReadToEnd().ShouldEqual(recordedRequest.ResponseBody.ToString());

                    Console.WriteLine("Completed " + i);
                }
            }

            // ASSERT

            // if we didn't timeout, then we're good
        }
        public void MatchesOnUniquePayload()
        {
            // ARRANGE
            var recordedRequest1 = new RecordedRequest
            {
                Url            = "http://fakeSite.fake",
                Method         = "POST",
                RequestPayload = "Request 1",
                RequestHeaders = new RecordedHeaders
                {
                    { "Content-Type", new [] { "text/plain" } }
                },
                ResponseBody = "Response 1"
            };

            var recordedRequest2 = new RecordedRequest
            {
                Url            = recordedRequest1.Url,
                Method         = recordedRequest1.Method,
                RequestPayload = "Request 2",
                RequestHeaders = recordedRequest1.RequestHeaders,
                ResponseBody   = "Response 2"
            };

            var recordingSession = new RecordingSession
            {
                RecordedRequests = new List <RecordedRequest> {
                    recordedRequest1, recordedRequest2
                }
            };

            var requestBuilder = new RecordingSessionInterceptorRequestBuilder(recordingSession);

            IWebRequestCreate creator = new HttpWebRequestWrapperInterceptorCreator(requestBuilder);

            var request1 = creator.Create(new Uri(recordedRequest1.Url));

            request1.Method      = "POST";
            request1.ContentType = "text/plain";

            recordedRequest1.RequestPayload.ToStream().CopyTo(request1.GetRequestStream());

            var request2 = creator.Create(new Uri(recordedRequest2.Url));

            request2.Method      = "POST";
            request2.ContentType = "text/plain";

            recordedRequest2.RequestPayload.ToStream().CopyTo(request2.GetRequestStream());

            // ACT
            var response1 = request1.GetResponse();
            var response2 = request2.GetResponse();

            // ASSERT
            response1.ShouldNotBeNull();
            response2.ShouldNotBeNull();

            using (var sr = new StreamReader(response1.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest1.ResponseBody.SerializedStream);

            using (var sr = new StreamReader(response2.GetResponseStream()))
                sr.ReadToEnd().ShouldEqual(recordedRequest2.ResponseBody.SerializedStream);
        }