Using .Net WebRequest Factory

As Richard Willis says in http://blog.salamandersoft.co.uk/index.php/2009/10/how-to-mock-httpwebrequest-when-unit-testing/ , I'm trying to trigger a web request by connecting behavior.

For this (I ask me if I messed up something) I implemented IWebRequestCreate and extended WebRequest and WebResponse. (more in the link codes)

But now in my code I had a test that registers ( WebRequest.RegisterPrefix ) prefix:

 [Test] public void Test() { var some = File.ReadAllBytes(@"TestData\WebService\admrond_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 suggestions? Is an interesting post about the implementation of my class interesting?


EDIT: how @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 Url is something like: mockPrefix://NoMatterUrl

+7
source share
3 answers

Since the error is "Invalid URI: hostname could not be parsed." you probably screw your Uri "mockPrefix: // NoMatterUrl"

I had this problem because I forgot to add an "/" between the uri domain and the request parameters.

Can you specify exactly what your "NoMatterUri" looks like?

+3
source

You need to register your prefix with a colon (':'); how in:

 WebRequest.RegisterPrefix("mockPrefix:", new WebRequestCreateMock()); 
+1
source

I found that the final "/" must be included in the prefix. For example, " 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 {} } 
0
source

All Articles