public async Task GivenRequest_WhenProcessing_ThenRequestValidatorHandlesCustomAssertionException() { List <SampleModel> sample = new List <SampleModel>() { new SampleModel(), new SampleModel() }; FakeHttpMessageHandler fake = new FakeHttpMessageHandler() .WithRequestValidator <XunitException>(request => Assert.Equal(HttpMethod.Get, request.Method)) .WithExpectedContent(sample); HttpClient httpClient = new HttpClient(fake); ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient); await exampleController.GetAll(); }
public async Task GivenResponseWithSerializationOptions_WhenProcessing_ControllerIgnoresWhitespace(bool writeIndented) { SampleModel sample = new SampleModel(); FakeHttpMessageHandler fake = new FakeHttpMessageHandler() .WithExpectedContent(sample, new System.Text.Json.JsonSerializerOptions() { WriteIndented = writeIndented }) .WithStatusCode(HttpStatusCode.OK); HttpClient httpClient = new HttpClient(fake); ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient); await exampleController.GetById(Guid.NewGuid()); }
public async Task GivenRequest_WhenProcessing_ThenRequestValidatorPerformed() { List <SampleModel> sample = new List <SampleModel>() { new SampleModel(), new SampleModel() }; FakeHttpMessageHandler fake = new FakeHttpMessageHandler() .WithRequestValidator(request => request.Method == HttpMethod.Get) .WithExpectedContent(sample); HttpClient httpClient = new HttpClient(fake); ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient); await exampleController.GetAll(); }
public async Task GivenMultipleInputsIntoController_WhenProcessing_ThenModelIsReturned() { List <SampleModel> sample = new List <SampleModel>() { new SampleModel(), new SampleModel() }; FakeHttpMessageHandler fake = new FakeHttpMessageHandler().WithStatusCode(HttpStatusCode.OK) .WithResponseHeader("order66", "babyyoda").WithExpectedContent(sample); HttpClient httpClient = new HttpClient(fake); ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient); IEnumerable <SampleModel> output = await exampleController.GetAll(); Assert.Equal(2, output.Count()); }
public async Task GivenPreActionForController_WhenProcessing_ThenActionIsPerformed() { int invocationCount = 0; List <SampleModel> sample = new List <SampleModel>() { new SampleModel(), new SampleModel() }; FakeHttpMessageHandler fake = new FakeHttpMessageHandler().WithPreRequest(() => invocationCount++) .WithExpectedContent(sample); HttpClient httpClient = new HttpClient(fake); ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient); IEnumerable <SampleModel> output = await exampleController.GetAll(); Assert.Equal(1, invocationCount); }
public async Task GivenRequest_WhenProcessingAsync_ThenRequestValidatorHandlesCustomAssertionException() { List <SampleModel> sample = new List <SampleModel>() { new SampleModel(), new SampleModel() }; FakeHttpMessageHandler fake = new FakeHttpMessageHandler() .WithRequestValidatorAsync <XunitException>(async request => { var content = await request.Content.ReadAsStringAsync(); Assert.Null(content); }) .WithExpectedContent(sample); HttpClient httpClient = new HttpClient(fake); ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient); await exampleController.GetAll(); }
public async Task GivenRequest_WhenProcessingAsync_ThenRequestValidatorReturnValue() { List <SampleModel> sample = new List <SampleModel>() { new SampleModel(), new SampleModel() }; FakeHttpMessageHandler fake = new FakeHttpMessageHandler() .WithRequestValidatorAsync(async request => { var content = await request.Content.ReadAsStringAsync(); return(content == null); }) .WithExpectedContent(sample); HttpClient httpClient = new HttpClient(fake); ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient); await exampleController.GetAll(); }
public async Task GivenComplexController_WhenBadRequestIsExpected_ThenBadRequestIsHandled() { FakeHttpMessageHandler fake = new FakeHttpMessageHandler().WithStatusCode(HttpStatusCode.BadRequest); HttpClient httpClient = new HttpClient(fake); ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient); int called = 0; try { await exampleController.GetById(Guid.NewGuid()); } catch (Exception e) { if (e.Message.StartsWith("Unable to find ")) { called++; } } Assert.Equal(1, called); }