0

I'm currently working on a .Net WebAPI to allow a Single-page-application written in Angular to exchange data back and forth with the business.

A particularity of the SPA is that it always runs under different domains (I have enabled CORS for a given list of domains). My API will therefore always address the data according to the origin of a request eg:

request origin                   domainId
--------------                   --------
https://www.client1.com    ->    client1.com
https://client1.com        ->    client1.com
https://www.client2.xxx    ->    client2.com

At the present moment, I made a utility class that recover the domainId based on a given Request:

public static class DomainId
    {
        public static string Get(HttpRequestMessage Request)
        {
            InMemoryDomainRepository repo = new InMemoryDomainRepository();

            try
            {
                IEnumerable<string> headerValues = Request.Headers.GetValues("Origin");
                var origin = headerValues.FirstOrDefault();
                return repo.GetDomainId(origin);
            }
            catch
            {
                return repo.GetDomainId("http://client1.com");
            }
        }

Then, in a controller, to get for instance a specific userProfile, I will use my utility class like so:

[Authorize]
public IHttpActionResult GetProfile()
{
    var domainId = DomainId.Get(Request); // uses the utility class above
    IClientRepository repo = new ClientRepositoryAx30();
    var client = repo.GetClient(domainId, User.Identity.Name);
    return Ok(client);
}

The question:

When writing test cases, there is no origin since I am not querying the API from within a browser. So how can I specify the origin of the request? As I understand so far, it is not possible to set the origin like this:

Request.Headers.SetValues("Origin", "https://client1.com");

Fetching out the DomainId from the origin (Request.Headers.GetValues("Origin");) is the best way I have found so far. But maybe there is a better solution?

I am currently stuck with the following test method:

[TestClass]
public class ClientControllerTests
{
    [TestMethod]
    public void TestGetProfile()
    {
        var identity = new GenericIdentity("Bob");
        Thread.CurrentPrincipal = new GenericPrincipal(identity, null);

        // how to set a Generic Domain?

        var ctrl = new ClientController();
        var result = ctrl.GetProfile();

        IHttpActionResult actionResult = ctrl.GetProfile();

        System.Diagnostics.Debug.WriteLine(actionResult);
        //Assert.IsInstanceOfType(actionResult, typeof(OkResult));
    }
}

I would like to be able to specify a "generic" domain, just like I define a "Generic Identity"

Any help will be greatly appreciated.

Best, Mikou

Mikou
  • 579
  • 1
  • 8
  • 17
  • you could take a look at this post: http://stackoverflow.com/questions/970198/how-to-mock-the-request-on-controller-in-asp-net-mvc ,but - personal opinion - I don't like to write a lot of lines of codes for mocks (see: http://enterprisecraftsmanship.com/2015/06/29/test-induced-design-damage-or-why-tdd-is-so-painful/ ) – wilver Sep 23 '15 at 14:43
  • I actually tried out the solution you refer to but for some reason, I get this error: The type or namespace name 'HttpRequestBase' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) And I don't understand why – Mikou Sep 25 '15 at 08:02

0 Answers0