Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7558833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:28:33+00:00 2026-05-30T12:28:33+00:00

From the standard LogOn method in the account controller in MVC3 applications, how can

  • 0

From the standard LogOn method in the account controller in MVC3 applications, how can I test the

Url.IsLocalUrl(returnUrl.ToString()) 

line of code where the url is not local? In other words, what url do I have to feed into this line of code when unit testing, to get it to return false?

I have used the following thinking this would be returned as false(non local):

Uri uri = new Uri(@"http://www.google.com/blahblah.html");

But it just threw a null exception in the unit tests

Edit: I should add that the LogOn method now looks like this:

public ActionResult LogOn(LogOnModel model, System.Uri returnUrl)

if (ModelState.IsValid) {

            bool loggedOn = LogOn(model);

            if (loggedOn) {
                if (Url.IsLocalUrl(returnUrl.ToString())) {
                    return Redirect(returnUrl.ToString());
                }
                else {
                    return RedirectToAction("Index", "Home");
                }
            }
            else {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(viewModel);
    }

Some style cop/code analysis errors forced a changed from a string parameter to System.uri parameter but it is very similar to the standard original.

Just to clarify, in a unit test – I want to test and assert the outcome of hitting the Else line where it redirects to Home/Index, so I need to pass something to into the (System.Uri)returnUrl that will make it return false on Url.IsLocalUrl and not throw an exception

Further Edit:

I am using MvcContrib testhelper, which is pretty good at mocking a lot of the httpcontext and web stuff:

Builder = new TestControllerBuilder();
UserController = new UserController();
    Builder.InitializeController(UserController);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:28:34+00:00Added an answer on May 30, 2026 at 12:28 pm

    You need to mock the HttpContext as well as the UrlHelper instance on the controller that you are unit testing. Here’s an example of how that unit test might look like if you are using Moq:

    [TestMethod]
    public void LogOn_Should_Redirect_To_Home_If_Authentication_Succeeds_But_Not_Local_ReturnUrl_Is_Provided()
    {
        // arrange
        var sut = new AccountController();
        var model = new LogOnModel();
        var returnUrl = new Uri("http://www.google.com");
        var httpContext = new Mock<HttpContextBase>();
        var request = new Mock<HttpRequestBase>();
        httpContext.Setup(x => x.Request).Returns(request.Object);
        request.Setup(x => x.Url).Returns(new Uri("http://localhost:123"));
        var requestContext = new RequestContext(httpContext.Object, new RouteData());
        sut.Url = new UrlHelper(requestContext);
    
        // act
        var actual = sut.LogOn(model, returnUrl);
    
        // assert
        Assert.IsInstanceOfType(actual, typeof(RedirectToRouteResult));
        var result = (RedirectToRouteResult)actual;
        Assert.AreEqual("Home", result.RouteValues["controller"]);
        Assert.AreEqual("Index", result.RouteValues["action"]);
    }
    

    Remark: since you have actually shown the LogOn implementation that you are calling to verify the credentials, you might need to adapt the unit test to ensure that this method returns true in the first place given the model in order to enter the if (loggedOn) clause.


    UPDATE:

    It seems that you are using MvcContrib.TestHelper which does all the HttpContext mocking setup for you. So all that you need to do is to mock the relevant parts for your unit test:

    [TestMethod]
    public void LogOn_Should_Redirect_To_Home_If_Authentication_Succeeds_But_Not_Local_ReturnUrl_Is_Provided()
    {
        // arrange
        var sut = new AccountController();
        new TestControllerBuilder().InitializeController(sut);
        var model = new LogOnModel();
        var returnUrl = new Uri("http://www.google.com");
        sut.HttpContext.Request.Expect(x => x.Url).Return(new Uri("http://localhost:123"));
    
        // act
        var actual = sut.LogOn(model, returnUrl);
    
        // assert
        actual
            .AssertActionRedirect()
            .ToController("Home")
            .ToAction("Index");
    }
    

    Normally the first 2 lines of the unit test could be moved to the global [SetUp] method to avoid repeating them in each unit test for this controller so that now your test becomes a bit cleaner:

    [TestMethod]
    public void LogOn_Should_Redirect_To_Home_If_Authentication_Succeeds_But_Not_Local_ReturnUrl_Is_Provided()
    {
        // arrange
        var model = new LogOnModel();
        var returnUrl = new Uri("http://www.google.com");
        _sut.HttpContext.Request.Expect(x => x.Url).Return(new Uri("http://localhost:123"));
    
        // act
        var actual = _sut.LogOn(model, returnUrl);
    
        // assert
        actual
            .AssertActionRedirect()
            .ToController("Home")
            .ToAction("Index");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: What is the standard method for accessing back end data, which logon should
The following is the LogOn user control from a standard default ASP.NET MVC project
I refactor some classes from standard SQL to JPA/ORM usage. In most cases the
How a user exception class is created from standard exception? Addressing below cases Say
Is it possible to set welcome-file tag from standard web.xml file by servlet 3.0
Current situation: Ubuntu 8.04 server edition (live server) Postgresql 8.3.7 (from standard repositories) Postgis
Is there any easy way to change the color of the UITableViewCellAccessoryCheckmark from standard
I Develop a Form Designer, also develop some custom control that inherit from standard
I'm reading from the standard input using the read() system call but there's a
When I get a runtime exception from the standard library, it's almost always a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.