using Microsoft.AspNetCore.Http; using Ocelot.Infrastructure.RequestData; using Ocelot.Responses; using Shouldly; using TestStack.BDDfy; using Xunit; namespace Ocelot.UnitTests.Infrastructure { public class HttpDataRepositoryTests { private HttpContext _httpContext; private IHttpContextAccessor _httpContextAccessor; private HttpDataRepository _httpDataRepository; private object _result; public HttpDataRepositoryTests() { _httpContext = new DefaultHttpContext(); _httpContextAccessor = new HttpContextAccessor{HttpContext = _httpContext}; _httpDataRepository = new HttpDataRepository(_httpContextAccessor); } //TODO - Additional tests -> Type mistmatch aka Add string, request int //TODO - Additional tests -> HttpContent null. This should never happen [Fact] public void Get_returns_correct_key_from_http_context() { this.Given(x => x.GivenAHttpContextContaining("key", "string")) .When(x => x.GetIsCalledWithKey("key")) .Then(x => x.ThenTheResultIsAnOkResponse("string")) .BDDfy(); } [Fact] public void Get_returns_error_response_if_the_key_is_not_found() //Therefore does not return null { this.Given(x => x.GivenAHttpContextContaining("key", "string")) .When(x => x.GetIsCalledWithKey("keyDoesNotExist")) .Then(x => x.ThenTheResultIsAnErrorReposnse("string1")) .BDDfy(); } private void GivenAHttpContextContaining(string key, object o) { _httpContext.Items.Add(key, o); } private void GetIsCalledWithKey(string key) { _result = _httpDataRepository.Get(key); } private void ThenTheResultIsAnErrorReposnse(object resultValue) { _result.ShouldBeOfType>(); ((ErrorResponse) _result).Data.ShouldBeNull(); ((ErrorResponse)_result).IsError.ShouldBe(true); ((ErrorResponse) _result).Errors.ShouldHaveSingleItem() .ShouldBeOfType() .Message.ShouldStartWith("Unable to find data for key: "); } private void ThenTheResultIsAnOkResponse(object resultValue) { _result.ShouldBeOfType>(); ((OkResponse)_result).Data.ShouldBe(resultValue); } } }