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); } }
/// <summary> /// Builds an HttpWebResponse using a deprecated constructor and the next queued ResponseSettings for this request /// </summary> /// <remarks>This is based on the sample code here: http://stackoverflow.com/questions/87200/mocking-webresponses-from-a-webrequest </remarks> public override WebResponse GetResponse() { var responseSettings = _expectedResponses.Dequeue(); if (responseSettings is HttpResponseSettings) { var httpResponseSettings = (HttpResponseSettings)responseSettings; var webResponse = TestableHttpWebResponse.GetHttpWebResponse(httpResponseSettings, _uri, DefaultExpectedContentType); if (httpResponseSettings.ExpectException) { throw new WebException("This request failed", new Exception(httpResponseSettings.StatusDescription), WebExceptionStatus.ProtocolError, webResponse); } else { return(webResponse); } } else if (responseSettings is ExceptionResponseSettings) { throw ((ExceptionResponseSettings)responseSettings).ExceptionToThrow; } else { throw new ArgumentException(String.Format("No logic to handle a ResponseSettings object of type '{0}'", responseSettings.GetType().Name)); } }
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; }
/// <summary> /// Builds an HttpWebResponse using a deprecated constructor and the next queued ResponseSettings for this request /// </summary> /// <remarks>This is based on the sample code here: http://stackoverflow.com/questions/87200/mocking-webresponses-from-a-webrequest </remarks> public override WebResponse GetResponse() { var responseSettings = _expectedResponses.Dequeue(); if (responseSettings is HttpResponseSettings) { var httpResponseSettings = (HttpResponseSettings)responseSettings; SerializationInfo si = new SerializationInfo(typeof(HttpWebResponse), new System.Runtime.Serialization.FormatterConverter()); StreamingContext sc = new StreamingContext(); WebHeaderCollection headers = new WebHeaderCollection(); 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", null); 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; } else if (responseSettings is ExceptionResponseSettings) { throw ((ExceptionResponseSettings)responseSettings).ExceptionToThrow; } else { throw new ArgumentException(String.Format("No logic to handle a ResponseSettings object of type '{0}'", responseSettings.GetType().Name)); } }