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

  • SEARCH
  • Home
  • 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 7495935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:23:54+00:00 2026-05-29T18:23:54+00:00

I am very, very new to unit testing and am trying to write a

  • 0

I am very, very new to unit testing and am trying to write a test for a pretty simple method:

public class myClass : RequireHttpsAttribute
{
    public override void OnAuthorization(AuthoizationContext filterContext)
    {
        var request = filterContext.HttpContext.Request;
        var header = Convert.ToBoolean(request.Headers["Special-Header-Name"]);

        if (!(header || request.IsSecureConnection))
        {
            HandleNonHttpsRequest(filterContext);
        }
    }
}

This method, which inherits from the RequireHttpsAttribute, checks if a certain header is present from a page, if it’s missing or false, and the page is not secure, then it will call HandleNonHttpsRequest, otherwise it does nothing.

We are using Moq and Nunit for testing. I have found some resources to help build a fakeHttpContext with Moq, but honestly I’m not sure how to use it or where to go within my unit tests to ensure that fake HttpContexts are or are not causing the HandleNonHttpsRequest method to call.

I really appreciate any guidance with this issue.

  • 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-29T18:23:57+00:00Added an answer on May 29, 2026 at 6:23 pm
    // arrange
    var context = new Mock<HttpContextBase>();
    var request = new Mock<HttpRequestBase>();
    var headers = new NameValueCollection
    {
        { "Special-Header-Name", "false" }
    };
    request.Setup(x => x.Headers).Returns(headers);
    request.Setup(x => x.HttpMethod).Returns("GET");
    request.Setup(x => x.Url).Returns(new Uri("http://www.example.com"));
    request.Setup(x => x.RawUrl).Returns("/home/index");
    context.Setup(x => x.Request).Returns(request.Object);
    var controller = new Mock<ControllerBase>();
    
    var actionDescriptor = new Mock<ActionDescriptor>();
    var controllerContext = new ControllerContext(context.Object, new RouteData(), controller.Object);
    var filterContext = new AuthorizationContext(controllerContext, actionDescriptor.Object);
    var sut = new myClass();
    
    // act
    sut.OnAuthorization(filterContext);
    
    // assert
    Assert.IsInstanceOfType(filterContext.Result, typeof(RedirectResult));
    var redirectResult = (RedirectResult)filterContext.Result;
    Assert.AreEqual("https://www.example.com/home/index", redirectResult.Url);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to IoC, Dependency Injection and Unit Testing. I'm starting a new
I am new to testing so I am trying to test some random things
I am very new to unit testing and writing/using exceptions. I am currently making
I'm very new to JBoss, EJB3, hibernate, and MySQL. I am trying to write
I'm trying to unit test a method that performs a fairly complex operation, but
I am very new to unit testing even though i have been coding for
I am very new to unit testing when it comes to databases and especially
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
Im very new to SQL but need to write a query to do the
Warning - I am very new to NHibernate. I know this question seems simple

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.