Reputation: 1120
this should be a simple one (i'm new to Shimming..)
using(ShimsContext.Create())
{
ShimHttpWebRequest.Constructor = @this =>
{
var shim = new ShimHttpWebRequest(@this);
shim.GetResponse = () =>
{
return new ShimHttpWebResponse();
};
};
ShimWebRequest.CreateString = (url) =>
{
return new ShimHttpWebRequest();
};
var http = WebRequest.Create("http://moomoo.moomoo") as HttpWebRequest;
var r = http.GetResponse() as HttpWebResponse;
}
So without shims, this test would fail as the there is no such url, it would fail to resolve. With the shims it works fine. Thing is, if if then create the class i want to test and invoke a method which creates an HttpWebRequest in the same way, it appears that the shim magic doesn't work, it really attempts to resolve the url. I've done a similar test with SmtpClient previously and this works, so I can't really see why my method's creation of these objects should behave any different.
Any ideas/experience on this?
UPDATE 1
Code in my class:
public void METHODNAME()
{
try
{
// Request the login page
Uri url = new Uri(BaseUrl + "logon.aspx");
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.AllowAutoRedirect = false;
request.CookieContainer = Cookies;
// Exception raised below
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
etc....
So it's a pretty basic method
UPDATE 2
Just added this into the class i am testing ON:
public void test()
{
var http = WebRequest.Create("http://moomoo.moomoo") as HttpWebRequest;
var r = http.GetResponse() as HttpWebResponse;
}
It works fine.. so there must be some difference in the other method.. but i certainly can't see anything obvious. Will update when/if i find the solution
Upvotes: 0
Views: 884
Reputation: 27944
Your only made a shim for the string variant of from Create method. You are calling the Url variant:
current:
ShimWebRequest.CreateString = (url) =>
{
return new ShimHttpWebRequest();
};
For Url variant of Create:
ShimWebRequest.CreateUrl = (url) =>
{
return new ShimHttpWebRequest();
};
Upvotes: 3