Page 1 of 1

Unable to test with mock HttpContextBase in .Net

Posted: Sun Sep 22, 2013 1:54 am
by sorzy
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

Re: Unable to test with mock HttpContextBase in .Net

Posted: Sun Sep 22, 2013 4:05 pm
by sm_support2
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

Re: Unable to test with mock HttpContextBase in .Net

Posted: Mon Sep 23, 2013 9:16 am
by sm_support2
Hi, we emailed you a newer pre-release of the .NET Cloud Client. Can you please confirm that you have received it?

Thanks

Re: Unable to test with mock HttpContextBase in .Net

Posted: Mon Sep 23, 2013 3:54 pm
by sorzy
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" });

Re: Unable to test with mock HttpContextBase in .Net

Posted: Mon Sep 23, 2013 5:13 pm
by sm_support2
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

Re: Unable to test with mock HttpContextBase in .Net

Posted: Thu Feb 06, 2014 8:52 am
by abcde
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" });

Re: Unable to test with mock HttpContextBase in .Net

Posted: Thu Feb 06, 2014 9:40 am
by Elliotfehr
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