Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neontapir/6254122 to your computer and use it in GitHub Desktop.
Save neontapir/6254122 to your computer and use it in GitHub Desktop.
Unit test example of Web API service using AttributeRouting with MSTest
using System;
using System.Collections;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Web.Http;
using AttributeRouting.Web.Http.Framework;
using AttributeRouting.Web.Http.SelfHost;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace WebApiWithAttributeRoutingServiceTest
{
[TestClass]
public class ServiceTests
{
[TestMethod]
public void Can_read_routes_from_attributes()
{
var config = new HttpConfiguration();
config.Routes.MapHttpAttributeRoutes();
var routes = config.Routes.Cast<HttpAttributeRoute>().Select(x => string.Format("{0}", x.RouteName)).ToArray();
string routeString = string.Join(";", routes);
StringAssert.Contains(routeString, "Test_Get;Test_Get_1;Test_Post;Test_Put;Test_Delete");
}
[TestMethod]
public void Can_get_simple_service_to_respond()
{
TestUsingService("http://localhost/api/values", response => TestValue(response, new[] {"value1", "value2"}));
}
private static void TestValue<T>(HttpResponseMessage response, T expectedValue)
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
var content = response.Content as ObjectContent;
Assert.IsNotNull(content);
var actualValue = (T) content.Value;
Assert.IsNotNull(actualValue);
if (typeof(T).IsAssignableFrom(typeof(ICollection)) || typeof(T).IsArray)
{
var expectedCollection = (ICollection)expectedValue;
var actualCollection = (ICollection)actualValue;
CollectionAssert.AreEquivalent(expectedCollection, actualCollection);
}
else
Assert.AreEqual(expectedValue, actualValue);
}
private static void TestUsingService(string routeUnderTest, Action<HttpResponseMessage> responseTest)
{
var config = new HttpConfiguration();
config.Routes.MapHttpAttributeRoutes();
var server = new HttpServer(config);
using (var client = new HttpMessageInvoker(server))
{
using (var request = new HttpRequestMessage(HttpMethod.Get, routeUnderTest))
{
using (HttpResponseMessage response = client.SendAsync(request, CancellationToken.None).Result)
{
responseTest(response);
}
}
}
}
}
public class TestController : ApiController
{
// GET api/values
[GET("api/values"), HttpGet]
public IEnumerable<string> Get()
{
return new[] { "value1", "value2" };
}
// GET api/values/5
[GET("api/values/{id}"), HttpGet]
public string Get(int id)
{
return "value";
}
// POST api/values
[POST("api/values"), HttpPost]
public void Post([FromBody]string value)
{
}
// PUT api/values/5
[PUT("api/values/{id}"), HttpPut]
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
[DELETE("api/values/{id}"), HttpDelete]
public void Delete(int id)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment