Ejemplo n.º 1
0
        private void LoadRequirements()
        {
            ApiRequirementsFile[] requirements = TryLoadConfigurationFiles <ApiRequirementsFile>(this.SourceFolderPath);
            var foundRequirements = requirements.FirstOrDefault();

            if (null != foundRequirements)
            {
                Console.WriteLine("Using API requirements file: {0}", foundRequirements.SourcePath);
                this.Requirements = foundRequirements.ApiRequirements;
            }
        }
        public static ValidationResult <bool> IsResponseValid(this HttpResponse response, string sourceFile, ApiRequirements requirements, IssueLogger issues)
        {
            if (null == requirements || null == requirements.HttpResponse)
            {
                return(new ValidationResult <bool>(true));
            }

            List <ValidationError> errors = new List <ValidationError>();

            var reqs = requirements.HttpResponse;

            var responseMimeType = new MultipartMime.MimeContentType(response.ContentType);

            if (reqs.ContentTypes != null && null != responseMimeType.MimeType &&
                !reqs.ContentTypes.Contains(responseMimeType.MimeType))
            {
                issues.Warning(ValidationErrorCode.InvalidContentType, $"Response Content-Type header value is not in the supported list of content-types: {response.ContentType}");
            }

            return(new ValidationResult <bool>(!errors.Any()));
        }
        /// <summary>
        /// Evaluate the ApiRequirements file and return any inconsistencies
        /// as ValidationErrors.
        /// </summary>
        public static ValidationResult <bool> IsRequestValid(this HttpRequest request, string sourceFile, ApiRequirements apiRequirements, IssueLogger issues)
        {
            if (null == apiRequirements || null == apiRequirements.HttpRequest)
            {
                return(new ValidationResult <bool>(true));
            }

            var reqs = apiRequirements.HttpRequest;

            var requestMimeType = new MultipartMime.MimeContentType(request.ContentType);

            if (null != reqs.ContentTypes && null != requestMimeType.MimeType &&
                !reqs.ContentTypes.Contains(requestMimeType.MimeType))
            {
                issues.Warning(ValidationErrorCode.InvalidContentType, $"Request content-type header value is not in the supported list of content-types: {request.ContentType}");
            }

            if (reqs.HttpMethods != null && !reqs.HttpMethods.Contains(request.Method))
            {
                issues.Error(ValidationErrorCode.InvalidHttpMethod, $"Request HTTP method is not in the supported list of HTTP methods: {request.Method}");
            }

            if (reqs.MaxUrlLength > 0 && request.Url.Length > reqs.MaxUrlLength)
            {
                issues.Error(ValidationErrorCode.UrlLengthExceedsMaximum, $"Request URL is longer than the defined maximum URL length: [{request.Url.Length}] {request.Url}");
            }

            if (reqs.StandardHeaders != null && request.Headers.Count > 0)
            {
                foreach (var headerName in request.Headers.AllKeys)
                {
                    if (!reqs.StandardHeaders.ContainsString(headerName, apiRequirements.CaseSensativeHeaders))
                    {
                        issues.Warning(ValidationErrorCode.NonStandardHeaderUsed, $"Request includes a non-standard header: {headerName}");
                    }
                }
            }

            return(new ValidationResult <bool>(issues.IssuesInCurrentScope == 0));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Evaluate the ApiRequirements file and return any inconsistencies
        /// as ValidationErrors.
        /// </summary>
        /// <returns>The http request.</returns>
        /// <param name="request">Request.</param>
        /// <param name="sourceFile"></param>
        /// <param name="apiRequirements"></param>
        public static ValidationResult <bool> IsRequestValid(this HttpRequest request, string sourceFile, ApiRequirements apiRequirements)
        {
            if (null == apiRequirements || null == apiRequirements.HttpRequest)
            {
                return(new ValidationResult <bool>(true));
            }

            List <ValidationError> errors = new List <ValidationError>();

            var reqs = apiRequirements.HttpRequest;

            var requestMimeType = ConvertToMimeType(request.ContentType);

            if (null != reqs.ContentTypes && null != requestMimeType &&
                !reqs.ContentTypes.Contains(requestMimeType))
            {
                errors.Add(new ValidationWarning(ValidationErrorCode.InvalidContentType, sourceFile, "Request content-type header value is not in the supported list of content-types: {0}", request.ContentType));
            }

            if (reqs.HttpMethods != null && !reqs.HttpMethods.Contains(request.Method))
            {
                errors.Add(new ValidationError(ValidationErrorCode.InvalidHttpMethod, sourceFile, "Request HTTP method is not in the supported list of HTTP methods: {0}", request.Method));
            }

            if (reqs.MaxUrlLength > 0 && request.Url.Length > reqs.MaxUrlLength)
            {
                errors.Add(new ValidationError(ValidationErrorCode.UrlLengthExceedsMaximum, sourceFile, "Request URL is longer than the defined maximum URL length: [{0}] {1}", request.Url.Length, request.Url));
            }

            if (reqs.StandardHeaders != null && request.Headers.Count > 0)
            {
                foreach (var headerName in request.Headers.AllKeys)
                {
                    if (!reqs.StandardHeaders.Contains(headerName))
                    {
                        errors.Add(new ValidationWarning(ValidationErrorCode.NonStandardHeaderUsed, sourceFile, "Request includes a non-standard header: {0}", headerName));
                    }
                }
            }

            return(new ValidationResult <bool>(!errors.Any(), errors));
        }
Ejemplo n.º 5
0
        public static ValidationResult <bool> IsResponseValid(this HttpResponse response, string sourceFile, ApiRequirements requirements)
        {
            if (null == requirements || null == requirements.HttpResponse)
            {
                return(new ValidationResult <bool>(true));
            }

            List <ValidationError> errors = new List <ValidationError>();

            var reqs = requirements.HttpResponse;

            var responseMimeType = ConvertToMimeType(response.ContentType);

            if (reqs.ContentTypes != null && null != responseMimeType &&
                !reqs.ContentTypes.Contains(responseMimeType))
            {
                errors.Add(new ValidationWarning(ValidationErrorCode.InvalidContentType, sourceFile, "Response Content-Type header value is not in the supported list of content-types: {0}", response.ContentType));
            }

            return(new ValidationResult <bool>(!errors.Any(), errors));
        }