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(@"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 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
NET 6, the WebRequest, WebClient, and ServicePoint classes are deprecated. The classes are still available, but they're not recommended for new development. To reduce the number of analyzer warnings, only construction methods are decorated with the ObsoleteAttribute attribute.
In a nutshell, WebRequest—in its HTTP-specific implementation, HttpWebRequest—represents the original way to consume HTTP requests in . NET Framework. WebClient provides a simple but limited wrapper around HttpWebRequest.
WebRequest is the abstract base class for . NET's request/response model for accessing data from the Internet.
GetResponseStream Method (System.Net) Gets the stream that is used to read the body of the response from the server.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With