Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mock the ASP.NET ServerVariables["HTTP_HOST"] value?

i have the following code, which fails during runtime...

var mock = new Mock<ControllerContext>();
mock.SetupGet(x => x.HttpContext.Request
    .ServerVariables["HTTP_HOST"]).Returns(domain);

** RunTime Error: Invalid setup on non-overridable property

I've got some code in my controller, which needs to check the domain the user has requested/gone to.

I'm not sure how to mock it up? any ideas?

PS. I'm using the Moq framewoke in the above example .. so I'm not sure if that's an issue, etc.?

like image 634
Pure.Krome Avatar asked Feb 23 '10 00:02

Pure.Krome


2 Answers

You can't mock the indexer on a NameValueCollection because it's not virtual. What I would do is mock the ServerVariables property since that IS virtual. You can fill in your own NameValueCollection. See below

Here's what I would do:

 var context = new Mock<ControllerContext>();
 NameValueCollection variables = new NameValueCollection();
 variables.Add("HTTP_HOST", "www.google.com"); 
 context.Setup(c => c.HttpContext.Request.ServerVariables).Returns(variables);
 //This next line is just an example of executing the method 
 var domain = context.Object.HttpContext.Request.ServerVariables["HTTP_HOST"];
 Assert.AreEqual("www.google.com", domain);
like image 145
Eric Avatar answered Oct 11 '22 15:10

Eric


You can cover HttpContext with interface and mock it in your tests:

interface IHttpContextValues
{
    string HttpHost { get; }
}

class HttpContextValues : IHttpContextValues
{
    public string HttpHost
    {
        get { return HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; }
    }
}

class BaseController : Controller
{
    public IHttpContextValues HttpContextValues;
    BaseController()
    {
        HttpContextValues = new HttpContextValues();
    }
}

Then you use HttpContextValues instead of ControllerContext.HttpContext in your controller code. You don't have to make any combinations about mocking.

like image 45
LukLed Avatar answered Oct 11 '22 15:10

LukLed