The first step is to create an interface that simplifies the services you need: -
public interface IHeaders { public string GetRequestHeader(string headerName); public void AppendResponseHeader(string headerName, string headerValue); }
Now create a default implementation: -
public Headers : IHeaders { public string GetRequestHeader(string headerName) { return HttpContext.Current.Request[headerName]; } public void AppendResponseHeader(string headerName, string headerValue) { HttpContext.Current.Response.AppendHeader(headerName, headerValue); } }
Now add a new field to your controller: -
private IHeaders myHeadersService;
add a new constructor to you: -
public MyController(IHeaders headersService) { myHeadersService = headersService; }
change or add default constructor: -
public MyController() { myHeadersService = new Headers(); }
now in your action code use myHeadersService instead of Response and Request objects.
In your tests, create your own implementation of the IHeaders interface to emulate / test the action code and pass this implementation when building the controller.
source share