示例#1
0
        public async Task <CheckProfanityResult> Post(EnumExecuteDetail exectionDetail)
        {
            var profanityList = new ProfanityListInMemoryService();
            var service       = new CheckProfanityService(profanityList, null);

            byte[] buf = new byte[(int)Request.ContentLength.Value];
            await Request.Body.ReadAsync(buf, 0, (int)Request.ContentLength.Value);

            var ms = new MemoryStream(buf);

            return(await service.CheckProfanity(ms, exectionDetail));
        }
示例#2
0
        public async void TestHasProfanityVerbosityResult()
        {
            var profanityList = new ProfanityListServiceStub(new List <string> {
                "ah", "oh", "eh"
            });
            var function = new CheckProfanityService(profanityList, null);

            var errText = "ah oh uh oh";
            var stream  = this.GetStream(errText);

            var res = await function.CheckProfanity(stream, EnumExecuteDetail.Verbosity);

            Assert.Equal(EnumResultStatus.TextHasProfanity, res.ResultStatus);
            Assert.Equal(3, res.CheckingLog.Count);
        }
示例#3
0
        public async void TestHasProfanityDetailResult()
        {
            var context       = new TestLambdaContext();
            var profanityList = new ProfanityListServiceStub(new List <string> {
                "ah", "oh", "eh"
            });
            var function = new CheckProfanityService(profanityList, null);

            var errText = "ah oh uh oh";
            var stream  = this.GetStream(errText);

            var res = await function.CheckProfanity(stream, EnumExecuteDetail.Detailed);

            Assert.Equal(EnumResultStatus.TextHasProfanity, res.ResultStatus);
            Assert.Equal(3, res.ProfanityMessagesCount);
            Assert.Equal(2, res.CheckingLog.Count);
            Assert.Equal(2, res.CheckingLog.Single(x => x.Profanity == "oh").Count);
            Assert.Equal(1, res.CheckingLog.Single(x => x.Profanity == "ah").Count);
        }
示例#4
0
        /// <summary> Profanity words checker </summary>
        public async Task <APIGatewayProxyResponse> CheckProfanityHandler(APIGatewayProxyRequest request, ILambdaContext context)
        {
            var logger = LambdaLoggerExtension.TryCreateSerilogLogger();

            try
            {
                var profanityList = ProfanityListFactory.GetProfanityListService();

                var service = new CheckProfanityService(profanityList, logger);

                var exectionDetail = EnumExecuteDetail.CountResult;
                if (request.MultiValueQueryStringParameters != null &&
                    request.MultiValueQueryStringParameters.ContainsKey("exectionDetail")
                    )
                {
                    var executionDetailStr = request
                                             .MultiValueQueryStringParameters["exectionDetail"]
                                             .FirstOrDefault();

                    if (Enum.TryParse(executionDetailStr,
                                      out EnumExecuteDetail val))
                    {
                        exectionDetail = val;
                    }
                    else
                    {
                        throw new ParseRequestException($"Can't parse ExecuteDetail '{executionDetailStr}'");
                    }
                }

                if (string.IsNullOrEmpty(request.Body))
                {
                    throw new ParseRequestException("Request is empty");
                }

                var buf = Encoding.UTF8.GetBytes(request.Body);
                var ms  = new MemoryStream(buf);

                var result = await service.CheckProfanity(ms, exectionDetail);

                return(new APIGatewayProxyResponse
                {
                    StatusCode = result.ResultStatus == EnumResultStatus.TextIsOk ||
                                 result.ResultStatus == EnumResultStatus.TextHasProfanity
                        ? (int)HttpStatusCode.OK
                        : (int)HttpStatusCode.InternalServerError,
                    Body = JsonConvert.SerializeObject(result)
                });
            }
            catch (ParseRequestException e)
            {
                logger?.Error(e, "Request exception");

                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Body = e.Message
                });
            }
            catch (Exception e)
            {
                logger?.Error(e, "Undefined exception");

                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Body = e.Message
                });
            }
            finally
            {
                logger?.TryDispose();
            }
        }