﻿using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;

namespace Api.Tests
{
    public abstract class BaseControllerTest : IDisposable
    {
        protected IConfiguration Configuration { get; }
        protected TestServer Server { get; }
        protected HttpClient Client { get; }

        protected BaseControllerTest()
        {
            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", false)
               .Build();

            var server = new TestServer(WebHost.CreateDefaultBuilder()
                .UseStartup<Startup>());

            var client = server.CreateClient();

            client.DefaultRequestHeaders.Clear();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf8"));

            Server = server;

            Client = client;
        }

        protected virtual void Disposing()
        {
            Server.Dispose();

            Client.Dispose();
        }

        public void Dispose()
        {
            Disposing();
        }
    }
}
