Using .Net WebRequest Factory
As Richard Willis suggests in http://blog.salamandersoft.co.uk/index.php/2009/10/how-to-mock-httpwebrequest-when-unit-testing/ i'm trying to call a web request moking the behavior.
For that (I asking me if I'm messing something here) I implemented an IWebRequestCreate and extended a WebRequest and a WebResponse. (more details in link codes)
But now in my code I had a test that register ( WebRequest.RegisterPrefix
) a prefix:
[Test]
public void Test() {
var some = File.ReadAllBytes(@"TestDataWebServiceadmrond_13jan2011_14jan2011.xml");
WebRequest.RegisterPrefix("mockPrefix", new WebRequestCreateMock());
WebRequestFake request = WebRequestCreateMock.CreateRequestFake(some);
_remoteRepository.PopulateWithMeterData(_meter);
... (error in line before)
Then, I got this error: Invalid URI: The hostname could not be parsed.
But why? In my PopulateWithMeterData(Meter meter)
I have this call:
WebRequest request = WebRequest.Create(urlListMeteringData);
WebResponse ws = request.GetResponse();
Some suggestion? Is interesting post my class implementations?
EDIT: as @Matthew ask:
public class WebRequestCreateMock : IWebRequestCreate {
static WebRequest _nextRequest;
static readonly object LockObject = new object();
static public WebRequest NextRequest {
get { return _nextRequest; }
set {
lock (LockObject) {
_nextRequest = value;
}
}
}
public WebRequest Create(Uri uri) {
return _nextRequest;
}
public static WebRequestFake CreateRequestFake(byte[] xmlStream) {
WebRequestFake webRequestFake = new WebRequestFake(xmlStream);
NextRequest = webRequestFake;
return webRequestFake;
}
}
public class WebRequestFake : 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; }
public WebRequestFake(byte[] response) {
responseStream = new MemoryStream(response);
}
public override Stream GetRequestStream() {
return requestStream;
}
public override WebResponse GetResponse() {
return new WebReponseFake(responseStream);
}
}
public class WebReponseFake : WebResponse {
private readonly Stream _responseStream;
public WebReponseFake(Stream responseStream) {
_responseStream = responseStream;
}
public override Stream GetResponseStream() {
return _responseStream;
}
}
And the Url is something like: mockPrefix://NoMatterUrl
Since the error is "Invalid URI: The hostname could not be parsed." you are probably screwing up your Uri "mockPrefix://NoMatterUrl"
I had this problem once because I forgot to add a "/" between the domain uri and the request parameters.
Can you post exactly what your "NoMatterUri" looks like?
You need to register your prefix with a colon (':'); as in:
WebRequest.RegisterPrefix("mockPrefix:", new WebRequestCreateMock());
I have found that it's necessary to include a trailing "/" in the prefix. For instance, " test://localhost/
":
[TestClass]
public class WebRequestTests
{
[TestMethod]
public void TestWebRequestCreate()
{
const string uriString = "test://localhost/foo/bar.baz?a=b&c=d";
var webRequest = new MockWebRequestCreateAssertUrl(uriString);
Assert.IsTrue(WebRequest.RegisterPrefix("test://localhost/", webRequest),
"Failed to register prefix");
Assert.IsNotNull(WebRequest.Create(uriString));
}
public class MockWebRequestCreateAssertUrl : IWebRequestCreate
{
private readonly Uri _expectedUri;
public MockWebRequestCreateAssertUrl(string uriString)
{
_expectedUri = new Uri(uriString);
}
public WebRequest Create(Uri uri)
{
Assert.AreEqual(_expectedUri, uri, "uri parameter is wrong");
return new MockWebRequestAssertUrl();
}
}
public class MockWebRequestAssertUrl : WebRequest {}
}
链接地址: http://www.djcxy.com/p/50834.html
上一篇: 如何使直接用户与谷歌分析