I have setup a basic Web API project to test writing integration tests, with the below setup
Startup:
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(WebApiTest.Startup))]
namespace WebApiTest
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
WebApiConfig.Register(new System.Web.Http.HttpConfiguration());
}
}
}
And the following controller:
using System.Web.Http;
namespace WebApiTest.Controllers
{
public class TestController : ApiController
{
public IHttpActionResult Post()
{
return Ok("Hello, world!");
}
}
}
For my Tests project I am using NUnit
for my testing, and have two classes: an abstract BaseTest
class, and a ControllerTests
class which contains one test with the intention of sending a web request out to the above TestController
's GET
Action method:
BaseTest:
using Microsoft.Owin.Testing;
using NUnit.Framework;
using System;
using System.Net.Http;
namespace WebApiTest.Tests
{
public abstract class BaseTest : IDisposable
{
protected const string _url = "http://localhost/";
protected TestServer _server;
public void Dispose()
{
_server.Dispose();
}
[SetUp]
public void SetUp()
{
_server = TestServer.Create<Startup>();
}
protected HttpRequestMessage CreateRequest(string url, string
contentType, HttpMethod method)
{
HttpRequestMessage request = new HttpRequestMessage
{
RequestUri = new Uri(_url + url),
Method = method
};
request.Headers.Accept.Add(new
System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType));
return request;
}
}
}
ControllerTests:
using FluentAssertions;
using NUnit.Framework;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace WebApiTest.Tests
{
[TestFixture]
public class ControllerTests : BaseTest
{
[Test]
public async Task Test1()
{
HttpRequestMessage request = CreateRequest("test", "application/json", HttpMethod.Post);
HttpResponseMessage response = await _server.HttpClient.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
}
}
The problem I am running into is that I'm not able to reach my endpoint with the call await _server.HttpClient.SendAsync(request);
as I always get a 404: Not Found
response.
I'm not entirely sure what I'm doing wrong, whether it be something that's not being setup property or Api routes that aren't being mapped.
update: Here is my WebApiConfig.Register()
method for reference:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}