Mocking property depending on HttpRequest

mock httprequest
httptestingcontroller
mock http request headers c#
how to create mock response in soapui
how to create mock service in soapui without wsdl
soapui mock service dynamic response
mock soap service java
http client test

If you want to mock something, you should first allow mocking on it. If you want to mock UserDetails you should allow mocking on its getter and pass required context inside newly crafted contract:

public class ExampleBaseController : Microsoft.AspNetCore.Mvc.Controller
{
    private readonly IUserDetailsProvider _userDetailsProvider;
    public UserDetails UserDetails => _userDetailsProvider.Get(Request);

    public ExampleBaseController(IUserDetailsProvider userDetailsProvider)
    {
        _userDetailsProvider = userDetailsProvider;
    }
}

So, in test you mock IUserDetailsProvider to return some "foobar". In production you just invoking GetUserDetailsFromHttpHeaders() method on passed inside Request.

To answer question about Request and Controller relations. Controller depends on Request, yes, and Microsoft thought that it will be good to strongly merge them together instead of passing dependency, for example like this:

public class FooBarController : Microsoft.AspNetCore.Mvc.Controller
{
    private readonly System.Web.HttpRequestBase _request;

    public FooBarController(System.Web.HttpRequestBase request)
    {
        _request = request;
    }
}

Or even like this:

public class FooBarController : Microsoft.AspNetCore.Mvc.Controller
{
    public void ProcessRequest(System.Web.HttpRequestBase request)
    {
        //request here
    }
}

They instead used Property injection, which leaves developer with no way to affect injection. This is a problem. But not unsolvable - you just pass context inside (by delegate, by interface, by reference), if you need one of those coupled together objects.

Mocking property depending on HttpRequest, Mocking property depending on HttpRequest. httptestingcontroller match example requests-mock pytest mock external api python mock requests session First, the HttpClient does not have an interface which you could use for mocking. Secondly, it doesn’t even have an abstract base class you could use. Even worse, the methods that you call on the HttpClient are not virtual, so you don’t have a chance to override them. First (not so good) idea: Wrap the HttpClient


It could be not so convinient as solution, proposed by @eocron, but still:

public interface IWithUserDetails
{
    UserDetails UserDetails();
}

public class ExampleBaseController : Microsoft.AspNetCore.Mvc.Controller, IWithUserDetails
{
    public UserDetails UserDetails()
    {
        return Request.GetUserDetailsFromHttpHeaders();
    }
}

Same name for class and method is not a best way to do, but it was like it in the example with a property

Terraform: Up & Running: Writing Infrastructure as Code, You could create a mock of it, but that's a lot of work. 2. Create a request object, which is of type HTTPRequest. 'text/plain', 'Not Found'] end end end There are two key properties to notice about this new Handlers class: as inputs The Handlers class does not depend on HTTPServer, HTTPRequest, or HTTPResponse. How to Successfully Mock ASP.NET HttpContext For web developers, mocking an HttpContext is one of the hardest things to do. Today, I use two mocking frameworks to show you a number of ways to mock the HttpContext.


Here is another point of view:

public interface IUserDetailsProviderOptions
{
    Func<UserDetails> UserDetailsProvider { get; set; }
}

public class DefaultUserDetailsProviderOptions : IUserDetailsProviderOptions
{
    public Func<UserDetails> UserDetailsProvider {get; set;}
}

public class ExampleBaseController : Microsoft.AspNetCore.Mvc.Controller
{
    private readonly Func<UserDetails> _userDetailsProvider;
    public UserDetails UserDetails => _userDetailsProvider();

    public ExampleBaseController(IUserDetailsProviderOptions options)
    {
         _userDetailsProvider = options.UserDetailsProvider ??
                              Request.GetUserDetailsFromHttpHeaders;
    }
}

Register in Startup.cs like this:

services.AddSingleton<IUserDetailsProviderOptions, DefaultUserDetailsProviderOptions>();

In testing you could do:

public class StubUserDetailsOption : IUserDetailsProviderOptions
{
    public Func<UserDetails> UserDetailsProvider { get; set; } = () => new StubDetails();
}

var controller = new ExampleBaseController(new StubUserDetailsOption());

and do testing.

Beginning Spring, The org.springframework.mock.env package contains mock implementations of convenient to use than creating dynamic mock objects for HttpServletRequest annotation you can enable or disable tests according to some property value. Different ways to fake a backend. There are a few possibilities to »serve« dummy data depending on your needs, your tech stack and the requirements of your project: Static JSON files via Ajax; Online mocking services »JSON Server« An own server; We are going to explore the details as well as the pros and cons of each and every possibility.


ASP.NET Core 3 and React: Hands-On full stack web development , toBeNull(); }); We create the mock property using jest.fn(). This is because the HomePage component is making an HTTP request to get the data but there is no​  Through this object you can get hold of the incoming message, its headers, etc. Also the underlying HttpRequest and HttpResponse objects are available for “low-level” processing. mockResult – an object encapsulating the result of the dispatched request, this is available in the MockService.afterRequest script.


Mocking HttpContext HttpRequest and HttpResponse for UnitTests , The solution is based on the Moq Mocking framework and on the mocking of HttpContext, HttpRequest and HttpResponse (very useful for  F# Data: HTTP Utilities. The .NET library provides a powerful API for creating and sending HTTP web requests. There is a simple WebClient type (see MSDN) and a more flexible HttpWebRequest type (see MSDN). However, these two types are quite difficult to use if you want to quickly run a simple HTTP request and specify parameters such as method


Working with MockServices | SOAP Mocking, The SOAP Service Mocking functionality in SoapUI lets you create a standards compliant simulation of a WSDL based service just from its WSDL below that is the standard list of tabs for adding a description, properties and a number of  In order to populate the Request.Form object on a controller, you have to populate the ControllerContext property. After investigating the Request property in the reflector, I saw that it just pulls its information from the ControllerContext property.