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 7173451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:49:37+00:00 2026-05-28T15:49:37+00:00

I’m implementing an oauth provider using DotNetOpenAuth CTP library. So I have created an

  • 0

I’m implementing an oauth provider using DotNetOpenAuth CTP library. So I have created an mvc3 application, which has an OAuth Controller with 3 methods in it with the purpose of authorizing third party applications. The controller has an IOAuthService which encapsulates all the logic that the library must do to complete certain tasks, however, the service methods return DotNetOpenOAuth objects that have their constructors protected.

I would like to test the behavior of the methods within my OAuthController, for this, I’m trying to mock my service methods but I havent’t been able to do this. I have to tell moq library what type of object I’m expecting the service method to return, and since I cannot access constructors of these objects, I’m not able to perform a test over my controller method.

The controller:

public class OAuthController : Controller
{
    private readonly IOAuthService _oAuthService;

    public OAuthController(IOAuthService oAuthService)
    {
        _oAuthService = oAuthService;
    }

    [Authorize, AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult Authorize()
    {
        ClientApplication requestingClient;
        var request = _oAuthService.ReadAuthorizationRequest();
        if (request == null)
        {
            throw new HttpException((int)HttpStatusCode.BadRequest, "Missing authorization request.");
        }

        var response = _oAuthService.RequestClientAuthorization(GetIdentity().Name, out requestingClient, request);
        if (response != null)
        {
            return response.AsActionResult();
        }

        var model = new AuthorizeClientApplicationViewModel
        {
            ClientApplication = requestingClient.Name,
            Scope = request.Scope,
            AuthorizationRequest = request,
        };

        return View(model);
    }

    public virtual IIdentity GetIdentity()
    {
        return User.Identity;
    }
}

I want to test that whenever a third party app has no authorization, a view will pop up to the user asking for his permission to authorize the app. Fot this i need to mock:

  • _oAuthService.RequestClientAuthorization

The setup of my test method will then look like :

var oAuthService = new Mock<IOAuthService>();
oAuthService.Setup(a => a.RequestClientAuthorization(userName, out client, pendingRequest)).Returns(new OutgoingWebResponse()); // DotNetOpenAuth doesn't allow me to do the **new OutgoingWebResponse**

PD: For this question I only wrote one of the controller methods, but there are 3, and they have similar scenarios.

  • 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-28T15:49:38+00:00Added an answer on May 28, 2026 at 3:49 pm

    One possibility is to write a wrapper (the same way ASP.NET MVC abstracts all the HTTP Context specific stuff):

    public abstract class OutgoingWebResponseWrapperBase
    {
        protected OutgoingWebResponseWrapperBase() { }
    
        public abstract ActionResult AsActionResult();
    }
    

    and then have a naïve implementation:

    public class OutgoingWebResponseWrapper: OutgoingWebResponseWrapperBase
    {
        private readonly OutgoingWebResponse _response;
        public OutgoingWebResponseWrapper(OutgoingWebResponse response)
        {
            _response = response;
        }
    
        public override ActionResult AsActionResult()
        {
            return _response.AsActionResult();
        }
    }
    

    Now modify the IOAuthService.RequestClientAuthorization method to return a OutgoingWebResponseWrapperBase instead of OutgoingWebResponse.

    Just like that:

    public interface IOAuthService
    {
        ...
        OutgoingWebResponseWrapperBase RequestClientAuthorization(...);
    }
    

    Obviously your controller code will stay absolutely the same. It’s just that now you can mock the return type of the RequestClientAuthorization in your unit test because it is an abstract class. You can also mock the AsActionResult abstract method call to return some expected mocked instance and you will assert in your unit test that the controller action that you are testing returned this expected action result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.