public static TestableHttpWebResponse GetHttpWebResponse(HttpResponseSettings httpResponseSettings, Uri uri, string expectedContentType)
        {
            SerializationInfo si = new SerializationInfo(typeof(HttpWebResponse), new System.Runtime.Serialization.FormatterConverter());
            StreamingContext sc = new StreamingContext();
            WebHeaderCollection headers = new WebHeaderCollection();

            foreach (var kvp in httpResponseSettings.HeaderValues)
                headers.Add(kvp.Key, kvp.Value);

            si.AddValue("m_HttpResponseHeaders", headers);
            si.AddValue("m_Uri", uri);
            si.AddValue("m_Certificate", null);
            si.AddValue("m_Version", HttpVersion.Version11);
            si.AddValue("m_StatusCode", httpResponseSettings.StatusCode);
            si.AddValue("m_ContentLength", 0);
            si.AddValue("m_Verb", "GET");
            si.AddValue("m_StatusDescription", httpResponseSettings.StatusDescription);
            si.AddValue("m_MediaType", expectedContentType);

            var webResponse = new TestableHttpWebResponse(si, sc, httpResponseSettings.ResponseStream);
 
            if (httpResponseSettings.ExpectException)
                throw new WebException("This request failed", new Exception(httpResponseSettings.StatusDescription), WebExceptionStatus.ProtocolError, webResponse);
            else
                return webResponse;
        }
        public void Execute_ExpectingEmptyPayload_ConsumesResponseStream()
        {
            var expectedUri = "test://queue.abc/whatever/";
            var expectedRawRequest = new TestableWebRequest(new Uri(expectedUri));
            var expectedRawResponse = new HttpResponseSettings(HttpStatusCode.OK, "Success", "Response content", false);
            expectedRawRequest.EnqueueResponse(expectedRawResponse);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRawRequest);
            var request = new RequestWithEmptyPayload(new SettingsFake(), expectedUri, "GET");

            var response = request.Execute();

            // expect the response stream to be closed now
            long x;
            Assert.Throws<ObjectDisposedException>(() => x = expectedRawResponse.ResponseStream.Position);
        }
 public TestableWebRequest EnqueueResponse(HttpResponseSettings response)
 {
     _expectedResponses.Enqueue(response);
     return this;
 }