Unable to test with mock HttpContextBase in .Net

Questions about the WURFL Cloud service.
sorzy
Posts: 2
Joined: Sun Sep 22, 2013 1:44 am

Unable to test with mock HttpContextBase in .Net

Postby sorzy » Sun Sep 22, 2013 1:54 am

Hi Guys,

I am testing out the cloud service but running into problems trying to write a few unit / integration tests. Error is: "Object reference not set to an instance of an object". Below is my code, any ideas what needs to be set in HttpContextBase for the request to work? And why doesn't the cloud API have a method where you can only send in the user agent string like the stand-alone API does? Would make it a lot easier to write tests...

[TestMethod]
public void TestGalaxyTab()
{
string ua = "Mozilla/5.0 (Linux; U; Android 4.1.2; nb-no; GT-N8000 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30";
var context = FakeHttpContext(ua);
var config = new DefaultCloudClientConfig
{
ApiKey = "xxxxxx:.........."
};
var manager = new CloudClientManager(config);
var info = manager.GetDeviceInfo(context);
...
}

public static HttpContextBase FakeHttpContext(string userAgent)
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var cookies = new HttpCookieCollection();
var items = new ListDictionary();

request.Setup(r => r.Cookies).Returns(cookies);
response.Setup(r => r.Cookies).Returns(cookies);

context.Setup(ctx => ctx.Items).Returns(items);

context.SetupGet(ctx => ctx.Request).Returns(request.Object);
context.SetupGet(ctx => ctx.Response).Returns(response.Object);
context.SetupGet(ctx => ctx.Session).Returns(session.Object);
context.SetupGet(ctx => ctx.Server).Returns(server.Object);

request.Setup(r => r.UserAgent).Returns(userAgent);

return context.Object;
}


Thanks,
Frank

sm_support2
Posts: 294
Joined: Mon Jun 20, 2011 5:04 pm

Re: Unable to test with mock HttpContextBase in .Net

Postby sm_support2 » Sun Sep 22, 2013 4:05 pm

Hi there, as luck has it, we are planning an improvement of the .NET Cloud Clients in this area. Do you have an email we can reach you at? is the one with which you registered to the forum a valid email address?

you can also contact as at wurfldb#scientiamobile.com

Thanks

sm_support2
Posts: 294
Joined: Mon Jun 20, 2011 5:04 pm

Re: Unable to test with mock HttpContextBase in .Net

Postby sm_support2 » Mon Sep 23, 2013 9:16 am

Hi, we emailed you a newer pre-release of the .NET Cloud Client. Can you please confirm that you have received it?

Thanks

sorzy
Posts: 2
Joined: Sun Sep 22, 2013 1:44 am

Re: Unable to test with mock HttpContextBase in .Net

Postby sorzy » Mon Sep 23, 2013 3:54 pm

I'm very happy to report back that a new API which I believe is soon to be released includes an option to query by user agent string instead of the full HttpContext. This also allows the cloud service to be easily tested without having to mock up a lot of things :-)

Example code with new API:

var userAgent = "Mozilla/5.0 (Linux; U; Android 4.1.2;...";
var info = manager.GetDeviceInfo(userAgent, new string[] { "is_tablet", "is_wireless_device" });

sm_support2
Posts: 294
Joined: Mon Jun 20, 2011 5:04 pm

Re: Unable to test with mock HttpContextBase in .Net

Postby sm_support2 » Mon Sep 23, 2013 5:13 pm

Excellent. We are planning an improvement to the Cloud Clients that will include this (for .NET) and the ability to set a proxy for the .NET, Java and PHP

Thanks

abcde
Posts: 2
Joined: Thu Oct 06, 2011 11:04 pm

Re: Unable to test with mock HttpContextBase in .Net

Postby abcde » Thu Feb 06, 2014 8:52 am

Is this version shipped? Means can I use?

var userAgent = "Mozilla/5.0 (Linux; U; Android 4.1.2;...";
var info = manager.GetDeviceInfo(userAgent, new string[] { "is_tablet", "is_wireless_device" });

Elliotfehr

Re: Unable to test with mock HttpContextBase in .Net

Postby Elliotfehr » Thu Feb 06, 2014 9:40 am

Are you able to post the User-Agent that you are having trouble detecting? Are you having any other issues using those capabilities at the moment?

Thank you,

Elliot


Who is online

Users browsing this forum: No registered users and 1 guest