Mocking WebRequest in .Net
July 3, 2012 at 11:28 PM
—
alex
In .net there is a build-in ability to mock up any http uri. See code after the break.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>A web request creator for unit testing.</summary>
class TestWebRequestCreate : IWebRequestCreate
{
static WebRequest nextRequest;
static object lockObject = new object();
static public WebRequest NextRequest
{
get { return nextRequest ;}
set
{
lock (lockObject)
{
nextRequest = value;
}
}
}
/// <summary>See <see cref="IWebRequestCreate.Create"/>.</summary>
public WebRequest Create(Uri uri)
{
return nextRequest;
}
/// <summary>Utility method for creating a TestWebRequest and setting
/// it to be the next WebRequest to use.</summary>
/// <param name="response">The response the TestWebRequest will return.</param>
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; }
/// <summary>Initializes a new instance of <see cref="TestWebRequest"/>
/// with the response to return.</summary>
public TestWebRequest(string response)
{
responseStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(response));
}
/// <summary>Returns the request contents as a string.</summary>
public string ContentAsString()
{
return System.Text.Encoding.UTF8.GetString(requestStream.ToArray());
}
/// <summary>See <see cref="WebRequest.GetRequestStream"/>.</summary>
public override Stream GetRequestStream()
{
return requestStream;
}
/// <summary>See <see cref="WebRequest.GetResponse"/>.</summary>
public override WebResponse GetResponse()
{
return new TestWebReponse(responseStream);
}
}
class TestWebReponse : WebResponse
{
Stream responseStream;
/// <summary>Initializes a new instance of <see cref="TestWebReponse"/>
/// with the response stream to return.</summary>
public TestWebReponse(Stream responseStream)
{
this.responseStream = responseStream;
}
/// <summary>See <see cref="WebResponse.GetResponseStream"/>.</summary>
public override Stream GetResponseStream()
{
return responseStream;
}
}
class Program
{
static void Main(string[] args)
{
var creator = new TestWebRequestCreate();
TestWebRequestCreate.CreateTestRequest("hello!");
WebRequest.RegisterPrefix("http://cooluri.com", creator);
var request = WebRequest.Create("http://cooluri.com");
var response = request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
string json = streamReader.ReadToEnd().ToLowerInvariant();
}
}
}
}
59b67029-a267-4646-b6cb-7218f0d1cf05|1|5.0
Posted in: Development | R&D
Tags: