示例#1
0
        public async Task HandleResponseAsync_Failure(HttpStatusCode code, Exception exception)
        {
            // Arrange
            var env = new Mock <IWebHostEnvironment>();

            env.Setup(m => m.EnvironmentName).Returns("Development");
            var model    = new Models.ErrorResponseModel(env.Object, exception);
            var json     = System.Text.Json.JsonSerializer.Serialize(model);
            var response = new HttpResponseMessage
            {
                StatusCode = code,
                Content    = new StringContent(json)
            };

            // Act
            var actionResult = await response.HandleResponseAsync <Model.ParcelModel>();

            // Assert
            var jsonResult = Assert.IsType <JsonResult>(actionResult);

            Assert.Equal((int)code, jsonResult.StatusCode);
            var actualResult = Assert.IsType <Models.ErrorResponseModel>(jsonResult.Value);

            Assert.Equal(json, actualResult.Error);
        }
示例#2
0
        public async Task HandleResponseAsync_JsonFailure(HttpStatusCode code, Exception exception)
        {
            // Arrange
            var env = new Mock <IWebHostEnvironment>();

            env.Setup(m => m.EnvironmentName).Returns("Development");
            var model    = new Models.ErrorResponseModel(env.Object, exception);
            var json     = System.Text.Json.JsonSerializer.Serialize(model);
            var response = new HttpResponseMessage
            {
                StatusCode = code,
                Content    = new StringContent(json)
            };

            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

            // Act
            var actionResult = await response.HandleResponseAsync <Model.ParcelModel>();

            // Assert
            var jsonResult = Assert.IsType <JsonResult>(actionResult);

            Assert.Equal((int)code, jsonResult.StatusCode);
            var actualResult = Assert.IsType <System.Text.Json.JsonElement>(jsonResult.Value);

            Assert.Equal($"{model.Error}", $"{actualResult.GetProperty(nameof(Models.ErrorResponseModel.Error))}");
            Assert.Equal($"{model.Details}", $"{actualResult.GetProperty(nameof(Models.ErrorResponseModel.Details))}");
            Assert.Equal($"{model.Type}", $"{actualResult.GetProperty(nameof(Models.ErrorResponseModel.Type))}");
            Assert.Equal($"{model.StackTrace}", $"{actualResult.GetProperty(nameof(Models.ErrorResponseModel.StackTrace))}");
        }