vendredi 29 mai 2015

integration testing asp.net web api

I am developing an ASP.Net Web Api service for a project i am working on and i wanted to setup NUnit integration tests for my services.

I have TestFixtureSetup which configures Web Api routes, and i have just added OAuth

    [TestFixtureSetUp]
    public void Setup()
    {
        var type = typeof(UpdatesController);

        AppBuilder app = new AppBuilder();
        app.CreatePerOwinContext(CleoDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        OAuthAuthorizationServerOptions oauthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/oauth/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new CustomOAuthProvider(),
            AccessTokenFormat =
                new CustomJwtFormat("http://localhost:4673/")
        };
        app.UseOAuthAuthorizationServer(oauthServerOptions);

        this._configuration = new HttpConfiguration();
        this._configuration.MapHttpAttributeRoutes();
        this._configuration.Routes.MapHttpRoute(
            "DefaultApi", 
            "api/{controller}/{id}", 
            new { id = RouteParameter.Optional });
        this._configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
            new CamelCasePropertyNamesContractResolver();

        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(this._configuration);

        this._server = new HttpServer(this._configuration);

        var customDelegatingHandler = new CustomDelegatingHandler(Constants.AppId, Constants.ApiKey);
        this._server.Configuration.MessageHandlers.Add(customDelegatingHandler);

        this._apiClient = new UpdatesApiClient("http://localhost:4673/", this._server);
    }

The above works and hosts the service for my unit tests to call, because i am creating my database context per owin context i add the following to my base api controller constructor

this._cleoDbContext = this.Request.GetOwinContext().Get<CleoDbContext>();

The issue i face is that this.Request is null when i debug through the unit test (incidentally the unit tests are using HttpClient to connect to the service.

This is the test method

var model = this._apiClient.GetVersionInfo(new Version(1, 0, 0)).Result;

Assert.That(model.LatestVersion == new Version(1, 0, 0));

And this is the api client method

var message = new HttpRequestMessage(HttpMethod.Get, this._apiBaseAddress + "api/updates/info");
message.Content = new ObjectContent(typeof(Version), version, new JsonMediaTypeFormatter());
var response = await this._httpClient.SendAsync(message);

if (response.IsSuccessStatusCode)
{
    return response.Content.ReadAsAsync<VersionInfoModel>().Result;
}

throw new HttpResponseException(response);

I would appreciate any help in understanding what i have missed and understanding why the request object is null in the base api controller

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire