예제 #1
0
        public static MethodDefinition FromRequest(string request, CodeBlockAnnotation annotation, DocFile source, IssueLogger issues)
        {
            var method = new MethodDefinition
            {
                Request             = request,
                RequestMetadata     = annotation,
                Identifier          = annotation.MethodName?.FirstOrDefault(),
                SourceFile          = source,
                RequiredScopes      = annotation.RequiredScopes,
                RequiredApiVersions = annotation.RequiredApiVersions,
                RequiredTags        = annotation.RequiredTags
            };

            method.Title = method.Identifier;

            var methodIssues = issues.For(method.Identifier);

            try
            {
                HttpRequest requestExample;
                HttpParser.TryParseHttpRequest(request, out requestExample, methodIssues);
                if (!string.IsNullOrEmpty(requestExample?.Body) && requestExample.Body.Contains("{"))
                {
                    if (string.IsNullOrEmpty(annotation.ResourceType))
                    {
                        annotation.ResourceType = "requestBodyResourceFor." + method.Identifier;
                    }

                    var body = requestExample.Body;
                    if (requestExample.ContentType != null && requestExample.ContentType.StartsWith(MimeTypeMultipartRelated, StringComparison.OrdinalIgnoreCase))
                    {
                        var multipartContent = new MultipartMime.MultipartMimeContent(requestExample.ContentType, body);
                        var part             = multipartContent.PartWithId("<metadata>");
                        body = part.Body;
                    }

                    var requestBodyResource = new JsonResourceDefinition(annotation, body, source, methodIssues);
                    if (requestBodyResource.Parameters != null)
                    {
                        method.RequestBodyParameters.AddRange(requestBodyResource.Parameters);
                    }
                }
            }
            catch (Exception ex)
            {
                methodIssues.Warning(ValidationErrorCode.HttpParserError, "Unable to parse request body resource", ex);
            }

            return(method);
        }
예제 #2
0
        private void ValidateContentForType(MimeContentType contentType, string content, List <ValidationError> detectedErrors, bool validateJsonSchema = false)
        {
            if (contentType.MimeType.Equals(MimeTypeJson, StringComparison.OrdinalIgnoreCase))
            {
                // Verify that the request is valid JSON
                try
                {
                    JsonConvert.DeserializeObject(content);

                    if (validateJsonSchema)
                    {
                        ValidationError[] schemaErrors;
                        if (!ContentMatchesResourceSchema(content, RequestMetadata.ResourceType, SourceFile.Parent.ResourceCollection, out schemaErrors))
                        {
                            detectedErrors.AddRange(schemaErrors);
                        }
                    }
                }
                catch (Exception ex)
                {
                    detectedErrors.Add(new ValidationError(ValidationErrorCode.JsonParserException, null, "Invalid JSON format: {0}", ex.Message));
                }
            }
            else if (contentType.MimeType.Equals(MimeTypeMultipartRelated, StringComparison.OrdinalIgnoreCase))
            {
                // Parse the multipart/form-data body to ensure it's properly formatted
                try
                {
                    MultipartMimeContent multipartContent = new MultipartMime.MultipartMimeContent(contentType, content);
                    var part = multipartContent.PartWithId("<metadata>");
                    ValidateContentForType(part.ContentType, part.Body, detectedErrors, validateJsonSchema: true);
                }
                catch (Exception ex)
                {
                    detectedErrors.Add(new ValidationError(ValidationErrorCode.ContentFormatException, null, "Invalid Multipart MIME content format: {0}", ex.Message));
                }
            }
            else if (contentType.MimeType.Equals(MimeTypePlainText, StringComparison.OrdinalIgnoreCase))
            {
                // Ignore this, because it isn't something we can verify
            }
            else
            {
                detectedErrors.Add(new ValidationWarning(ValidationErrorCode.UnsupportedContentType, null, "Unvalidated request content type: {0}", contentType.MimeType));
            }
        }
        public void RoundtripTest()
        {
            MultipartMime.MultipartMimeContent message = new MultipartMime.MultipartMimeContent();
            message.Parts.Add(new MultipartMime.MessagePart {
                Id = "<metadata>", ContentType = new MimeContentType("application/json"), Body = "{\"foo\": \"bar\"}"
            });
            message.Parts.Add(new MultipartMime.MessagePart {
                Id = "<content", ContentType = new MimeContentType("text/plain"), Body = "This is test message content"
            });

            string body1 = message.ToString();

            MultipartMime.MultipartMimeContent message2 = new MultipartMime.MultipartMimeContent(message.ContentType, body1);
            string body2 = message2.ToString();

            Assert.AreEqual(body1.Length, body2.Length, "Body length changed between roundtrips.");
            Assert.AreEqual(body1, body2, "Body text was different between roundtrips.");
        }
예제 #4
0
        private void ValidateContentForType(MimeContentType contentType, string content, IssueLogger issues, bool validateJsonSchema = false)
        {
            if (contentType.MimeType.Equals(MimeTypeJson, StringComparison.OrdinalIgnoreCase))
            {
                // Verify that the request is valid JSON
                try
                {
                    JsonConvert.DeserializeObject(content);

                    if (validateJsonSchema)
                    {
                        ContentMatchesResourceSchema(content, RequestMetadata.ResourceType, SourceFile.Parent.ResourceCollection, issues);
                    }
                }
                catch (Exception ex)
                {
                    issues.Error(ValidationErrorCode.JsonParserException, "Invalid JSON format.", ex);
                }
            }
            else if (contentType.MimeType.StartsWith(MimeTypeMultipartRelated, StringComparison.OrdinalIgnoreCase))
            {
                // Parse the multipart/form-data body to ensure it's properly formatted
                try
                {
                    MultipartMimeContent multipartContent = new MultipartMime.MultipartMimeContent(contentType, content);
                    var part = multipartContent.PartWithId("<metadata>");
                    ValidateContentForType(part.ContentType, part.Body, issues, validateJsonSchema: true);
                }
                catch (Exception ex)
                {
                    issues.Error(ValidationErrorCode.ContentFormatException, "Invalid Multipart MIME content format.", ex);
                }
            }
            else if (ContentTypesNotToValidate.Contains(contentType.MimeType))
            {
                // Ignore this, because it isn't something we can verify
            }
            else
            {
                issues.Warning(ValidationErrorCode.UnsupportedContentType, $"Unvalidated request content type: {contentType.MimeType}");
            }
        }