Is it possible to mock out a .NET HttpWebResponse?

后端 未结 7 1041
再見小時候
再見小時候 2020-11-28 22:29

i\'ve got an integration test that grabs some json result from a 3rd party server. It\'s really simple and works great.

I was hoping to stop actually hitting this se

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 22:51

    I Found a great solution in this blog post:

    It´s very easy to use, you just need to do this:

    string response = "my response string here";
    WebRequest.RegisterPrefix("test", new TestWebRequestCreate());
    TestWebRequest request = TestWebRequestCreate.CreateTestRequest(response);
    

    And copy those files to your project:

        class TestWebRequestCreate : IWebRequestCreate
    {
        static WebRequest nextRequest;
        static object lockObject = new object();
    
        static public WebRequest NextRequest
        {
            get { return nextRequest ;}
            set
            {
                lock (lockObject)
                {
                    nextRequest = value;
                }
            }
        }
    
        /// See .
        public WebRequest Create(Uri uri)
        {
            return nextRequest;
        }
    
        /// Utility method for creating a TestWebRequest and setting
        /// it to be the next WebRequest to use.
        /// The response the TestWebRequest will return.
        public static TestWebRequest CreateTestRequest(string response)
        {
            TestWebRequest request = new TestWebRequest(response);
            NextRequest = request;
            return request;
        }
    }
    
    class TestWebRequest : WebRequest
    {
        MemoryStream requestStream = new MemoryStream();
        MemoryStream responseStream;
    
        public override string Method { get; set; }
        public override string ContentType { get; set; }
        public override long ContentLength { get; set; }
    
        /// Initializes a new instance of 
        /// with the response to return.
        public TestWebRequest(string response)
        {
            responseStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(response));
        }
    
        /// Returns the request contents as a string.
        public string ContentAsString()
        {
            return System.Text.Encoding.UTF8.GetString(requestStream.ToArray());
        }
    
        /// See .
        public override Stream GetRequestStream()
        {
            return requestStream;
        }
    
        /// See .
        public override WebResponse GetResponse()
        {
            return new TestWebReponse(responseStream);
        }
    }
    
    class TestWebReponse : WebResponse
    {
        Stream responseStream;
    
        /// Initializes a new instance of 
        /// with the response stream to return.
        public TestWebReponse(Stream responseStream)
        {
            this.responseStream = responseStream;
        }
    
        /// See .
        public override Stream GetResponseStream()
        {
            return responseStream;
        }
    }
    

提交回复
热议问题