Exemple #1
0
        public void Constructor_CreateInstance_Message_NotNul()
        {
            var obj = new ApiBadRequestResponse("error");

            Assert.NotNull(obj);
            Assert.Equal("error", obj.Message);
        }
Exemple #2
0
        public void Constructor_CreateInstance_ShouldBe_NotNull()
        {
            var modelState = new ModelStateDictionary();

            modelState.AddModelError("", "test error");
            var obj = new ApiBadRequestResponse(modelState);

            Assert.NotNull(obj);
            Assert.NotNull(obj.Errors);
        }
Exemple #3
0
        public void Constructor_CreateInstance_Identity_Result_NotNul()
        {
            var obj = new ApiBadRequestResponse(IdentityResult.Failed(new IdentityError[] {
                new IdentityError()
                {
                    Code = "", Description = ""
                }
            }));

            Assert.NotNull(obj);
            Assert.Equal(400, obj.StatusCode);
        }
Exemple #4
0
        public void ErrorOnInsertWithInvalidCSVFormattedProductListIn()
        {
            //Arrange
            string productcsv  = TestCSV.GetTestCSV2_Wrong();             //input contains two error lines
            var    postContent = new StringContent(productcsv, Encoding.UTF8, "text/plain");

            postContent.Headers.Remove("Content-Type");
            postContent.Headers.Add("Content-Type", "text/plain");

            HttpResponseMessage response = _client.PostAsync("api/v1/Product/", postContent).Result;

            var responseString = response.Content.ReadAsStringAsync().Result;
            ApiBadRequestResponse apiBadResponse = JsonConvert.DeserializeObject <ApiBadRequestResponse>(responseString);

            var result = apiBadResponse.Errors.ToList();


            Assert.Equal((int)StatusCodes.Status400BadRequest, (int)response.StatusCode);
            Assert.True(result.Count == 2);
            Assert.Equal("Check csv parser error list", apiBadResponse.Message);
        }
        public async Task Invoke(HttpContext context)
        {
            var response = new ApiResponse((int)HttpStatusCode.InternalServerError);

            try
            {
                await _next.Invoke(context);
            }
            catch (Exception ex)
            {
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                response = new ApiBadRequestResponse(ex.Message);
            }

            if (!context.Response.HasStarted)
            {
                context.Response.ContentType = "application/json";

                var json = JsonConvert.SerializeObject(response);

                await context.Response.WriteAsync(json);
            }
        }
Exemple #6
0
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            var url = $"{context.HttpContext.Request.Scheme}://{context.HttpContext.Request.Host}{context.HttpContext.Request.Path}{context.HttpContext.Request.QueryString}";

            switch (context.Result)
            {
            case OkObjectResult okObj:
                var apiOkObjResult = new ApiOkResponse(okObj.Value, url);
                context.Result = new JsonResult(apiOkObjResult)
                {
                    StatusCode = apiOkObjResult.StatusCode
                };
                break;

            case OkResult:
                var apiOkResult = new ApiOkResponse(null, url);
                context.Result = new JsonResult(apiOkResult)
                {
                    StatusCode = apiOkResult.StatusCode
                };
                break;

            case ContentResult contentResult:
                var apicontentResult = new ApiOkResponse(contentResult.Content, url);
                context.Result = new JsonResult(apicontentResult)
                {
                    StatusCode = apicontentResult.StatusCode
                };
                break;

            case ObjectResult objResult:

                switch (objResult.StatusCode)
                {
                case 400:
                    var apiBadRequestResult = new ApiBadRequestResponse(url, GetInformationDatas(objResult));
                    context.Result = new JsonResult(apiBadRequestResult)
                    {
                        StatusCode = apiBadRequestResult.StatusCode
                    };
                    break;

                case 404:
                    var apiNotFoundResult = new ApiNotFoundResponse(url, GetInformationDatas(objResult));
                    context.Result = new JsonResult(apiNotFoundResult)
                    {
                        StatusCode = apiNotFoundResult.StatusCode
                    };
                    break;

                case 415:
                    var apiUnSupportResult = new ApiUnSupportResponse(url);
                    context.Result = new JsonResult(apiUnSupportResult)
                    {
                        StatusCode = apiUnSupportResult.StatusCode
                    };
                    break;

                case null:
                    if (objResult.Value is not ApiResponse)
                    {
                        var apiObjResult = new ApiOkResponse(objResult.Value, url);
                        context.Result = new JsonResult(apiObjResult)
                        {
                            StatusCode = apiObjResult.StatusCode
                        };
                    }
                    break;
                }
                break;
            }
            base.OnResultExecuting(context);
        }
Exemple #7
0
        public BadRequestObjectResult FailResponse(ModelStateDictionary modelState)
        {
            ApiBadRequestResponse response = new ApiBadRequestResponse(modelState);

            return(BadRequest(response)); // 回傳400
        }
Exemple #8
0
        public BadRequestObjectResult FailResponse(string message)
        {
            ApiBadRequestResponse response = new ApiBadRequestResponse(message);

            return(BadRequest(response)); // 回傳400
        }