Unit Test ASP.NET Web API controller with fake HTTPContext - c#

I'm using the following approach to upload files through ASP.NET Web API controllers.
[System.Web.Http.HttpPost]
public HttpResponseMessage UploadFile()
{
HttpResponseMessage response;
try
{
int id = 0;
int? qId = null;
if (int.TryParse(HttpContext.Current.Request.Form["id"], out id))
{
qId = id;
}
var file = HttpContext.Current.Request.Files[0];
int filePursuitId = bl.UploadFile(qId, file);
}
catch (Exception ex)
{
}
return response;
}
In my unit tests I've created an HTTPContext class manually before calling the UploadFile action:
var request = new HttpRequest("", "http://localhost", "");
var context = new HttpContext(request, new HttpResponse(new StringWriter()));
HttpContext.Current = context;
response = controller.UploadFile();
Unfortunately, I wasn't able to add custom values to the Form collection, since it's read-only. Also I couldn't change the Files collection.
Is there any way to add custom values to the Form and Files properties of the Request to add needed data (id and file content) during the unit test?

Use some mocking framework like Moq instead. Create a mock HttpRequestBase and mock HttpContextBase with whatever data you need and set them on the controller.
using Moq;
using NUnit.Framework;
using SharpTestsEx;
namespace StackOverflowExample.Moq
{
public class MyController : Controller
{
public string UploadFile()
{
return Request.Form["id"];
}
}
[TestFixture]
public class WebApiTests
{
[Test]
public void Should_return_form_data()
{
//arrange
var formData = new NameValueCollection {{"id", "test"}};
var request = new Mock<HttpRequestBase>();
request.SetupGet(r => r.Form).Returns(formData);
var context = new Mock<HttpContextBase>();
context.SetupGet(c => c.Request).Returns(request.Object);
var myController = new MyController();
myController.ControllerContext = new ControllerContext(context.Object, new RouteData(), myController);
//act
var result = myController.UploadFile();
//assert
result.Should().Be.EqualTo(formData["id"]);
}
}
}

Since you have no control over those classes why not wrap/abstract the functionality behind one do control
IRequestService request;
[HttpPost]
public HttpResponseMessage UploadFile() {
HttpResponseMessage response;
try {
int id = 0;
int? qId = null;
if (int.TryParse(request.GetFormValue("id"), out id)) {
qId = id;
}
var file = request.GetFile(0);
int filePursuitId = bl.UploadFile(qId, file);
} catch (Exception ex) {
//...
}
return response;
}
Where request is one of your custom defined types IRequestService
public interface IRequestService {
string GetFormValue(string key);
HttpPostedFileBase GetFile(int index);
//...other functionality you may need to abstract
}
and can be implemented like this to be injected into your controller
public class RequestService : IRequestService {
public string GetFormValue(string key) {
return HttpContext.Current.Request.Form[key];
}
public HttpPostedFileBase GetFile(int index) {
return new HttpPostedFileWrapper(HttpContext.Current.Request.Files[index]);
}
}
in your unit test
var requestMock = new Mock<IRequestService>();
//you then setup the mock to return your fake data
//...
//and then inject it into your controller
var controller = new MyController(requestMock.Object);
//Act
response = controller.UploadFile();

Related

How to do Integration Tests with Mediatr on .net framework 4.7?

I'm using the Mediatr library to register and call my RequestHandlers.
Everything went fine until I started reading more about integrated tests.
PLEASE READ AFTER EDIT
I can't call my class which inherits from the RequesHandler.
My class looks like this:
public class MyRequestHandler : RequestHandler<MyRequest, MyResponse>
{
....
}
I'm not using the Meditr async and I'm using .net framework 4.7 instead of asp.net core, so, everything looks like returns me answers for asp.net core.
When I construct MyTestClass, to construct the RequestHandler I have to create a ServiceFactory and maybe this is the problem because I don't know how.
public MyClassTest()
{
ServiceFactory sv = null;
_mediator = new Mediator(sv);
}
EDIT
Providing more info
I have this Handler in my Application Layer
public class LogInUserByFormHandler : RequestHandler<LogInUserByFormRequest, LogInUserByFormResponse>
{
private readonly IValidator<LogInUserByFormRequest> _validator;
public LogInUserByFormHandler(IValidator<LogInUserByFormRequest> validator)
{
_validator = validator;
}
protected override LogInUserByFormResponse Handle(LogInUserByFormRequest request)
{
_validator.ValidateAndThrow(request);
var userInfo = GetUserInfo(request);
ValidateLogInUserByFormRules(userInfo);
var userLoginInfo = GetValidUserLoginInfo(request);
ValidateUserLoginInfoByFormRules(userLoginInfo);
var sessionKey = CreateUserSessionKey(userInfo);
var response = new LogInUserByFormResponse
{
UserName = request.UserName,
SessionKey = sessionKey,
UserId = userInfo.id_usuario
};
return response;
}
//A LOT OF CODE HERE, methods and etc
}
As it's possible to see, it implements the Mediatr.
On my Web Project on Presentation Layer, I used AutoFac to Inject the Handlers, so, any Request I do is always handled by the right method.
All I have to do is call, like this:
var logInByFormRequest = new LogInUserByFormRequest
{
UserName = viewModel.UserName,
Password = viewModel.Password
};
var response = _mediator.Send(logInByFormRequest).Result;
This works like a charm. The problem now is on the Test project. It references the Application as the Presentation Project does.
I don't know how to make the mediator.send find the right method.
EDIT²
Here comes my test code
[TestClass]
public class LogInUserByFormTest
{
private LogInUserByFormRequest CreateRequest(string userName, string password)
{
LogInUserByFormRequest request = new LogInUserByFormRequest
{
UserName = userName,
Password = password
};
return request;
}
[TestMethod]
[Description("")]
public void UserName_ShouldHave_Max_30Characters_Exception()
{
try
{
var request = CreateRequest("UserNameIsGreaterThanAllowed", "password");
var mediator = new Mock<IMediator>();
var response = mediator.Object.Send(request).Result;
}
catch (System.Exception ex)
{
throw;
}
}
}
The result (response) is always null and the mediator doesn't call the right handler.
EDIT3
Here is how I register the handlers and validators.
I use autofac. This class here is called on the global.asax
public class AutofacConfig
{
public static void ConfigureContainer()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly()).InstancePerRequest();
builder.RegisterType<Mediator>().As<IMediator>().InstancePerLifetimeScope();
builder.RegisterType<AutofacValidatorFactory>().As<IValidatorFactory>().SingleInstance();
builder.RegisterType<FluentValidationModelValidatorProvider>().As<ModelValidatorProvider>();
builder.RegisterType<RegistryManagerService>().As<IRegistryManagerService>().SingleInstance().WithParameter("appName", ConfigurationManager.AppSettings["APPNAME"]);
builder.Register<ServiceFactory>(context =>
{
var c = context.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
builder.RegisterAssemblyTypes(Assembly.Load("Docspider.Application"))
.Where(x => x.Name.EndsWith("Handler"))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(Assembly.Load("Docspider.Application"))
.Where(x => x.Name.EndsWith("Validator"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
public class AutofacValidatorFactory : ValidatorFactoryBase
{
private readonly IComponentContext _context;
public AutofacValidatorFactory(IComponentContext context)
{
_context = context;
}
public override IValidator CreateInstance(Type validatorType)
{
if (_context.TryResolve(validatorType, out object instance))
{
var validator = instance as IValidator;
return validator;
}
return null;
}
}
For such an integration test you would need to configure the necessary dependencies. Since you have indicated that Autofac is being used then configure a container just as you would have in production. Use the container to get the mediator and perform the desired test.
For example.
[TestClass]
public class LogInUserByForm_IntegrartionTest {
private LogInUserByFormRequest CreateRequest(string userName, string password) {
LogInUserByFormRequest request = new LogInUserByFormRequest {
UserName = userName,
Password = password
};
return request;
}
IMediator BuildMediator() {
//AutoFac
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces();
var mediatrOpenTypes = new[] {
typeof(IRequestHandler<,>)
};
foreach (var mediatrOpenType in mediatrOpenTypes) {
builder
.RegisterAssemblyTypes(typeof(LogInUserByFormRequest).GetTypeInfo().Assembly)
.AsClosedTypesOf(mediatrOpenType)
.AsImplementedInterfaces();
}
builder.Register<ServiceFactory>(ctx => {
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
//...all other needed dependencies.
//...
var container = builder.Build();
var mediator = container.Resolve<IMediator>();
return mediator;
}
[TestMethod]
[Description("")]
public async Task UserName_ShouldHave_Max_30Characters_Exception() {
try
{
//Arrange
var request = CreateRequest("UserNameIsGreaterThanAllowed", "password");
var mediator = BuildMediator();
//Act
var response = await mediator.Send(request);
//Assert
//...assert the expected values of response.
}
catch (System.Exception ex)
{
throw;
}
}
}
The above was modeled after the examples provided by MediatR.Examples.Autofac

Unit test Controller with HttpClient in .NET MVC

So I have a controller that is using HttpClient to call a webservice like so:
public class DemoController : Controller
{
HttpClient client;
string baseUrl = "http://localhost:90/webservice";
public DemoController()
{
client = new HttpClient
{
BaseAddress = new Uri(baseUrl)
};
}
// GET: DemoInfo
public async Task<ActionResult> Index()
{
HttpResponseMessage response = await client.GetAsync(baseUrl + "vehicle/menu/year");
string content = "";
MenuItems result = null;
if (response.IsSuccessStatusCode)
{
content = await response.Content.ReadAsStringAsync();
result = (MenuItems)new XmlSerializer(typeof(MenuItems)).Deserialize(new StringReader(content));
}
return View("Index", result);
}
}
My unit test for this action is as follows:
[TestMethod]
public async Task Test_Index()
{
// Arrange
DemoController controller = new DemoController();
// Act
var result = await controller.Index();
ViewResult viewResult = (ViewResult) result;
// Assert
Assert.AreEqual("Index", viewResult.ViewName);
Assert.IsNotNull(viewResult.Model);
}
So obviously I would like to avoid making the web service call every time the test is run. Would I be on the right track in opting for an IoC container like Unity so that HttpClient would be injected into the controller? Is that overkill for what I'm trying to achieve? I'm aware that there is a lot of history with people struggling with properly mocking httpclient in there unit tests through this github issue. Any help would be greatly appreciated in giving some insight into how to write the controller to make a service call while still being testable.
All dependencies which makes tests slow should be abstracted.
Wrap HttpClient with an abstraction, which you can mock in your tests.
public interface IMyClient
{
Task<string> GetRawDataFrom(string url);
}
Then your controller will depend on that abstraction
public class DemoController : Controller
{
private readonly IMyClient _client;
private string _baseUrl = "http://localhost:90/webservice";
public DemoController(IMyClient client)
{
_client = client;
}
public async Task<ActionResult> Index()
{
var rawData = _client.GetRawDataFrom($"{_baseUrl}vehicle/menu/year");
using (var reader = new StringReader(rawData))
{
var result =
(MenuItems)new XmlSerializer(typeof(MenuItems)).Deserialize(reader);
return View("Index", result);
}
}
}
Then in tests you can mock your abstraction to return expected data
public class FakeClient : IMyClient
{
public string RawData { get; set; }
public Task<string> GetRawDataFrom(string url)
{
return Task.FromResult(RawData);
}
}
[TestMethod]
public async Task Test_Index()
{
// Arrange
var fakeClient = new FakeClient
{
RawData = #"[
{ Name: "One", Path: "/one" },
{ Name: "Two", Path: "/two" }
]"
};
DemoController controller = new DemoController(fakeClient);
// Act
var result = await controller.Index();
ViewResult viewResult = (ViewResult)result;
// Assert
Assert.AreEqual("Index", viewResult.ViewName);
Assert.IsNotNull(viewResult.Model);
}
Actual implementation will use HttpClient
public class MyHttpClient : IMyClient
{
public Task<string> GetRawDataFrom(string url)
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
}
}
An alternative approach to testing HttpClient calls without service wrappers, mocks, or IoC containers is to use Flurl, a small wrapper library around HttpClient that provides (among other things) some robust testing features. [Disclaimer: I'm the author]
Here's what your controller would look like. There's a few ways to do this, but this approach uses string extension methods that abstract away the client entirely. (A single HttpClient instance per host is managed for you to prevent trouble.)
using Flurl.Http;
public class DemoController : Controller
{
string baseUrl = "http://localhost:90/webservice";
// GET: DemoInfo
public async Task<ActionResult> Index()
{
var content = await baseUrl
.AppendPathSegment("vehicle/menu/year")
.GetStringAsync();
var result = (MenuItems)new XmlSerializer(typeof(MenuItems)).Deserialize(new StringReader(content));
return View("Index", result);
}
}
And the test:
using Flurl.Http;
[TestMethod]
public async Task Test_Index()
{
// fake & record all HTTP calls in the test subject
using (var httpTest = new HttpTest())
{
// Arrange
httpTest.RespondWith(200, "<xml>some fake response xml...</xml>");
DemoController controller = new DemoController();
// Act
var result = await controller.Index();
ViewResult viewResult = (ViewResult) result;
// Assert
Assert.AreEqual("Index", viewResult.ViewName);
Assert.IsNotNull(viewResult.Model);
}
}
Flurl.Http is available on NuGet.

Unit test the web api controller errors while reading file path from the config file as well response type

Hi I am creating a web api controller which reads a path of the file from the web.config and then uses open xml sdk to read and load the excel document. I need to write a Nunit test to test this controller for the response.
I am basically having two problems and both are related
Problem 1
Since my Nunit is in the class librabry project it cant read the value from the config settings and errors out. How do I handle this in my test for the controller.
It errors out at this line of the code in the Nunit test method
_response = customerController.GetCustomer();
Problem 2
The same line of code i.e _response = customerController.GetCustomer(); also errors out because it returns type viewmodel and not response. How do I test the response object. Or do I need to test the view model object. Any insights would be helpful
WebApi Controller method
public IEnumerable<CustomerViewModel> GetCustomer()
{
string relativePath = ConfigurationManager.AppSettings["filePath"];
return (OpenSpreadsheetDocument(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)));
}
Nunit test method
[Test]
public void GetCustomerTest()
{
var customerController = new CustomerController()
{
Request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(ServiceBaseURL + "api/getcustomer")
}
};
customerController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
_response = customerController.GetCustomer();
var responseResult = JsonConvert.DeserializeObject<List<CustomerViewModel>>(_response.Content.ReadAsStringAsync().Result);
Assert.AreEqual(_response.StatusCode, HttpStatusCode.OK);
Assert.AreEqual(responseResult.Any(), true);
}
Based on suggestion
Updated WebAPI method
[HttpGet]
public HttpResponseMessage GetCustomer()
{
string relativePath = ConfigurationManager.AppSettings["filePath"];
IEnumerable <CustomerViewModel> customerViewModel = (OpenSpreadsheetDocument(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)));
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, customerViewModel);
return response;
}
1) update app settings in app.config file of test to match web.config of the web project.
<appSettings>
<add key="filePath" value="...." />
</appSettings>
2) have action return IHttpActionResult abstraction which would allow for more flexibility when testing.
public IHttpActionResult GetCustomer() {
string relativePath = ConfigurationManager.AppSettings["filePath"];
IEnumerable<CustomerViewModel> customer = (OpenSpreadsheetDocument(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)));
return Ok(customer);
}
you should also abstract the openxml to allow for mocking file access during unit testing
public class CustomerController : ApiController {
private readonly IExcelService service;
public CustomerController(IExcelService service) {
this.service = service;
}
[HttpGet]
public IHttpActionResult GetCustomer() {
IEnumerable<CustomerViewModel> customer = service.GetSpreadsheet();
return Ok(customer);
}
}
Service contract could look like this
public interface IExcelService {
IEnumerable<CustomerViewModel> GetSpreadsheet();
}
with an implementation that has what originally had in your controller.
public class ExcelService : IExcelService {
public IEnumerable<CustomerViewModel> GetSpreadsheet() {
string relativePath = ConfigurationManager.AppSettings["filePath"];
IEnumerable<CustomerViewModel> customer = (OpenSpreadsheetDocument(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)));
return customer;
}
}
To Test lets make a fake service that is not dependent on path. (note: this could also be easily done with a mocking framework but for example purposes we'll use a fake)
public class FakeService : IExcelService {
public IEnumerable<CustomerViewModel> GetSpreadsheet() {
return new List<CustomerViewModel>() { new CustomerViewModel() };
}
}
And now the test
[Test]
public void GetCustomerTest() {
//Arrange
var fakeService = new FakeService();
var customerController = new CustomerController(fakeService) {
Request = new HttpRequestMessage {
Method = HttpMethod.Get,
RequestUri = new Uri(ServiceBaseURL + "api/getcustomer")
}
};
customerController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
//Act
var _response = customerController.GetCustomer() as OkNegotiatedContentResult<IEnumerable<CustomerViewModel>>;
//Assert
Assert.IsNotNull(_response);
var responseResult = _response.Content;
Assert.IsNotNull(responseResult);
Assert.AreEqual(responseResult.Any(), true);
}

Fake HttpSessionStateBase in MS Fakes

I am writing a unit test to test an MVC controller method (CRUD operation). The controller method accepts the usernameIdentity from session for auditing purposes.
I am trying the following to setup the ShimHttpSessionStateBase:
[TestMethod()]
public void CategoryCreateTest()
{
using (ShimsContext.Create())
{
var controller = new WebcamsController();
var category = new DataModel.Webcams.Category() { Name = "TestCategory" };
var currentSessionWrapper = new HttpSessionStateWrapper(HttpContext.Current.Session);
var session = new System.Web.Fakes.ShimHttpSessionStateBase(currentSessionWrapper);
session.Instance.Add("usernameIdentity", "DOMAIN\\USERNAME");
controller.ControllerContext = new ControllerContext(controller);
var result = controller.CategoryCreate(category);
Assert.IsInstanceOfType(result, typeof(PartialViewResult));
}
}
I am not sure how to appropriately create the controllerContext so that the Session will be read in the controller method.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CategoryCreate([Bind(Include = "ID,Name")] Category category)
{
try
{
if (category != null
&& Validation.ValidNonNullableString(Session["usernameIdentity"].ToString(), 1, 60) && ModelState.IsValid)
{
businessLayer.SetCategories(category, "Add", AuthenticationHandler.GetHostInternetProtocolAddress, Session["usernameIdentity"].ToString(), AuthenticationHandler.GetUserInternetProtocolAddress);
return PartialView("CategoryList", businessLayer.GetCategoriesByCategory(0, AuthenticationHandler.GetHostInternetProtocolAddress));
}
return jsonResultHandler.JsonResultMessage("Failure", Properties.Resources.GlobalAttributeCreateFailed);
}
catch
{
throw;
}
}

Moq C#, How can I mock this class?

I need to mock this C# WebApi class using the Moq framework
public class PvValuesController
{
private readonly IDataServices dataService;
public PvValuesController(IDataServices dataServices)
{
dataService = dataServices;
}
[HttpGet]
public IHttpActionResult Get(string id, string st, string et)
{
if (dataService == null)
{
return BadRequest("DataService not found");
}
var result = dataService.GetPvData(id, st, et);
return Ok(result);
}
}
Problem is, if I mock it like this:
var controllerMock = new Mock<PvValuesController>();
I'm not passing any DataService to it, so the Get() function will always return a bad request code.
The original line was:
var controller = new PvValuesController(dataService);
Where dataService is a concrete instance of IDataService and is a complex object
How can I effectively mock such class?
EDIT:
my new test code:
[Test]
public async void TestMoq()
{
var a = new List<dynamic>();
a.Add(23);
// arrange
var dataService = new Mock<IDataServices>();
dataService.Setup(l => l.GetPvData(It.IsAny<string>(), It.IsAny<DateTime>(), It.IsAny<DateTime>())).Returns(a);
var controller = new PvValuesController(dataService.Object);
// act
var actionResult = controller.Get("groupid", "timestampstart", "timestampend");
var httpResponseMessage = await actionResult.ExecuteAsync(CancellationToken.None);
// assert
Assert.AreEqual(HttpStatusCode.BadRequest, httpResponseMessage.StatusCode);
}
I get an exception on the await line:
System.InvalidOperationException: HttpControllerContext.Configuration must not be null
Mock your dependency interface as shown below
var service = new Mock<IDataServices>();
Inject it to your controller
var ctrlObj = new PvValuesController(service.Object);
Then continue with your setup as usual for the service
service.SetUp(l => l.Get()).Returns("Hello World!!!");
Finally call your controller method using the controller instance
ctrlObj.Get("A","B","C");

Categories

Resources