/// <summary>
 /// Tests whether action result contains the same content type as the provided MediaTypeHeaderValue.
 /// </summary>
 /// <param name="contentType">Expected content type as <see cref="MediaTypeHeaderValue"/>.</param>
 protected void ValidateContainingOfContentType(MediaTypeHeaderValue contentType)
 {
     ContentTypeValidator.ValidateContainingOfContentType(
         this.ActionResult,
         contentType,
         this.ThrowNewFailedValidationException);
 }
        internal void SiteCollectionContentTypeAdding(string url)
        {
            using (var cc = TestCommon.CreateClientContext(url))
            {
                // Add supporting files, note that files validation will be done in the files test cases
                TestProvisioningTemplate(cc, "contenttype_files.xml", Handlers.Files | Handlers.Features);

                // Ensure we can test clean
                DeleteContentTypes(cc);

                // Add content types
                var result = TestProvisioningTemplate(cc, "contenttype_add.xml", Handlers.ContentTypes | Handlers.Fields);

                // Ensure the needed tokens are added to the target token parser, this is needed due to the tokenparser perf optimalizations
                result.TargetTokenParser.Tokens.Add(new SiteToken(cc.Web));

                ContentTypeValidator cv = new ContentTypeValidator(cc.Web);
                Assert.IsTrue(cv.Validate(result.SourceTemplate.ContentTypes, result.TargetTemplate.ContentTypes, result.TargetTokenParser));

                // change content types
                var result2 = TestProvisioningTemplate(cc, "contenttype_delta_1.xml", Handlers.ContentTypes);

                // Ensure the needed tokens are added to the target token parser, this is needed due to the tokenparser perf optimalizations
                result2.TargetTokenParser.Tokens.Add(new SiteToken(cc.Web));

                Assert.IsTrue(cv.Validate(result2.SourceTemplate.ContentTypes, result2.TargetTemplate.ContentTypes, result2.TargetTokenParser));
            }
        }
 /// <summary>
 /// Validates whether file result has the provided content type.
 /// </summary>
 /// <param name="contentType">Content type as string.</param>
 protected void ValidateContentType(string contentType)
 {
     ContentTypeValidator.ValidateContentType(
         this.ActionResult,
         contentType,
         this.ThrowNewFileResultAssertionException);
 }
 /// <summary>
 /// Tests whether action result contains the same content types as the provided ones.
 /// </summary>
 /// <param name="contentTypes">Expected content types as collection of <see cref="MediaTypeHeaderValue"/>.</param>
 protected void ValidateContentTypes(IEnumerable <MediaTypeHeaderValue> contentTypes)
 {
     ContentTypeValidator.ValidateContentTypes(
         this.ActionResult,
         contentTypes,
         this.ThrowNewFailedValidationException);
 }
        /// <summary>
        /// Creates a new instance of the <see cref="CloudEvent{T}"/> class.
        /// </summary>
        /// <typeparam name="T">Type of data.</typeparam>
        /// <param name="contentType">Content type.</param>
        /// <param name="data">Event data.</param>
        /// <param name="cloudEventsVersion"><see cref="CloudEventsVersion"/> value.</param>
        /// <returns>Returns the <see cref="CloudEvent{T}"/> instance created.</returns>
        public static CloudEvent <T> Create <T>(string contentType, T data, string cloudEventsVersion = CloudEventsVersion.Version01)
        {
            if (IsJson(contentType))
            {
                var objectEventised = new ObjectEvent <T>(cloudEventsVersion)
                {
                    ContentType = contentType, Data = data
                };

                return(objectEventised);
            }

            if (ContentTypeValidator.IsText(contentType))
            {
                var stringified     = data as string;
                var stringEventised = new StringEvent(cloudEventsVersion)
                {
                    ContentType = contentType, Data = stringified
                };

                return(stringEventised as CloudEvent <T>);
            }

            var binarified      = data as byte[];
            var binaryEventised = new BinaryEvent(cloudEventsVersion)
            {
                ContentType = contentType, Data = binarified
            };

            return(binaryEventised as CloudEvent <T>);
        }
        public void Given_Type_Should_IsDataString_ReturnValue()
        {
            var result = ContentTypeValidator.IsDataString(1);

            result.Should().BeFalse();

            result = ContentTypeValidator.IsDataString("hello world");
            result.Should().BeTrue();
        }
        public void Given_Type_Should_IsTypeByteArray_ReturnValue()
        {
            var result = ContentTypeValidator.IsTypeByteArray(typeof(int));

            result.Should().BeFalse();

            result = ContentTypeValidator.IsTypeByteArray(typeof(byte[]));
            result.Should().BeTrue();
        }
        public void Given_Type_Should_IsTypeString_ReturnValue()
        {
            var result = ContentTypeValidator.IsTypeString(typeof(int));

            result.Should().BeFalse();

            result = ContentTypeValidator.IsTypeString(typeof(string));
            result.Should().BeTrue();
        }
        public void Given_Type_Should_IsText_ReturnValue()
        {
            var result = ContentTypeValidator.IsText("lorem ipsum");

            result.Should().BeFalse();

            result = ContentTypeValidator.IsText("text/plain");
            result.Should().BeTrue();
        }
        public void Given_Type_Should_ImpliesJson_ReturnValue()
        {
            var result = ContentTypeValidator.ImpliesJson("lorem ipsum");

            result.Should().BeFalse();

            result = ContentTypeValidator.ImpliesJson("text/json");
            result.Should().BeTrue();
        }
        public void Given_Type_Should_HasJsonSuffix_ReturnValue()
        {
            var result = ContentTypeValidator.HasJsonSuffix("lorem ipsum");

            result.Should().BeFalse();

            result = ContentTypeValidator.HasJsonSuffix("application/cloudevents+json");
            result.Should().BeTrue();
        }
        public void Given_Type_Should_IsDataByteArray_ReturnValue()
        {
            var result = ContentTypeValidator.IsDataByteArray(1);

            result.Should().BeFalse();

            result = ContentTypeValidator.IsDataByteArray(Encoding.UTF8.GetBytes("hello world"));
            result.Should().BeTrue();
        }
        /// <inheritdoc />
        public IAndContentTestBuilder WithContentType(string contentType)
        {
            ContentTypeValidator.ValidateContentType(
                contentType,
                this.ActionResult.ContentType,
                this.ThrowNewContentResultAssertionException);

            return(this);
        }
        /// <inheritdoc />
        public IAndViewTestBuilder WithContentType(string contentType)
        {
            ContentTypeValidator.ValidateContentType(
                this.TestContext.MethodResult,
                contentType,
                this.ThrowNewViewResultAssertionException);

            return(this);
        }
        /// <inheritdoc />
        public IAndJsonTestBuilder WithContentType(string contentType)
        {
            ContentTypeValidator.ValidateContentType(
                contentType,
                this.GetJsonResult().ContentType,
                this.ThrowNewJsonResultAssertionException);

            return(this);
        }
示例#16
0
        /// <inheritdoc />
        protected override bool IsValidDataType(string data)
        {
            if (ContentTypeValidator.ImpliesJson(this.ContentType))
            {
                return(false);
            }

            if (ContentTypeValidator.IsText(this.ContentType))
            {
                return(true);
            }

            return(false);
        }
示例#17
0
        /// <summary>
        /// Tests whether the <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/>
        /// has the same content type as the provided string.
        /// </summary>
        /// <param name="baseTestBuilderWithContentTypeResult">
        /// Instance of <see cref="IBaseTestBuilderWithContentTypeResult{TContentTypeResultTestBuilder}"/> type.
        /// </param>
        /// <param name="contentType">Content type as string.</param>
        /// <returns>The same content type <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/> test builder.</returns>
        public static TContentTypeResultTestBuilder WithContentType <TContentTypeResultTestBuilder>(
            this IBaseTestBuilderWithContentTypeResult <TContentTypeResultTestBuilder> baseTestBuilderWithContentTypeResult,
            string contentType)
            where TContentTypeResultTestBuilder : IBaseTestBuilderWithActionResult
        {
            var actualBuilder = GetActualBuilder(baseTestBuilderWithContentTypeResult);

            ContentTypeValidator.ValidateContentType(
                actualBuilder.TestContext.MethodResult,
                contentType,
                actualBuilder.ThrowNewFailedValidationException);

            return(actualBuilder.ResultTestBuilder);
        }
        /// <summary>
        /// Tests whether the <see cref="ActionResult"/>
        /// contains the same content types provided as collection of <see cref="MediaTypeHeaderValue"/>.
        /// </summary>
        /// <param name="baseTestBuilderWithOutputResult">
        /// Instance of <see cref="IBaseTestBuilderWithOutputResult{TOutputResultTestBuilder}"/> type.
        /// </param>
        /// <param name="contentTypes">Content types as collection of <see cref="MediaTypeHeaderValue"/>.</param>
        /// <returns>The same output <see cref="ActionResult"/> test builder.</returns>
        public static TOutputResultTestBuilder ContainingContentTypes <TOutputResultTestBuilder>(
            this IBaseTestBuilderWithOutputResult <TOutputResultTestBuilder> baseTestBuilderWithOutputResult,
            IEnumerable <MediaTypeHeaderValue> contentTypes)
            where TOutputResultTestBuilder : IBaseTestBuilderWithActionResult
        {
            var actualBuilder = GetActualBuilder(baseTestBuilderWithOutputResult);

            ValidateObjectResult(actualBuilder);

            ContentTypeValidator.ValidateContentTypes(
                actualBuilder.TestContext.MethodResult,
                contentTypes,
                actualBuilder.ThrowNewFailedValidationException);

            return(actualBuilder.ResultTestBuilder);
        }
示例#19
0
        /// <summary>
        /// Checks whether the content type of the <see cref="CloudEvent{T}"/> instance indicates structured or not.
        /// </summary>
        /// <param name="ce"><see cref="CloudEvent{T}"/> instance.</param>
        /// <returns>Returns <c>True</c>, if the content type indicates structured; otherwise returns <c>False</c>.</returns>
        protected static bool IsStructuredCloudEventContentType(CloudEvent <T> ce)
        {
            if (ContentTypeValidator.IsJson(ce.ContentType))
            {
                return(true);
            }

            if (ContentTypeValidator.HasJsonSuffix(ce.ContentType))
            {
                return(true);
            }

            if (ContentTypeValidator.ImpliesJson(ce.ContentType))
            {
                return(true);
            }

            return(false);
        }
示例#20
0
        private static bool IsJson(string contentType)
        {
            if (ContentTypeValidator.IsJson(contentType))
            {
                return(true);
            }

            if (ContentTypeValidator.HasJsonSuffix(contentType))
            {
                return(true);
            }

            if (ContentTypeValidator.ImpliesJson(contentType))
            {
                return(true);
            }

            return(false);
        }
示例#21
0
        public void SiteCollectionContentTypeAddingTest()
        {
            using (var cc = TestCommon.CreateClientContext(centralSiteCollectionUrl))
            {
                // Add supporting files, note that files validation will be done in the files test cases
                TestProvisioningTemplate(cc, "contenttype_files.xml", Handlers.Files);

                // Ensure we can test clean
                DeleteContentTypes(cc);

                // Add content types
                var result = TestProvisioningTemplate(cc, "contenttype_add.xml", Handlers.ContentTypes | Handlers.Fields);
                ContentTypeValidator cv = new ContentTypeValidator(cc.Web);
                Assert.IsTrue(cv.Validate(result.SourceTemplate.ContentTypes, result.TargetTemplate.ContentTypes, result.TargetTokenParser));

                // change content types
                var result2 = TestProvisioningTemplate(cc, "contenttype_delta_1.xml", Handlers.ContentTypes);
                Assert.IsTrue(cv.Validate(result2.SourceTemplate.ContentTypes, result2.TargetTemplate.ContentTypes, result2.TargetTokenParser));
            }
        }
示例#22
0
        /// <summary>
        /// Tests whether the collected attributes contain <see cref="ProducesAttribute"/>.
        /// </summary>
        /// <param name="controllerActionAttributesTestBuilder">
        /// Instance of <see cref="IControllerActionAttributesTestBuilder{TAttributesTestBuilder}"/> type.
        /// </param>
        /// <param name="ofContentTypes">Expected content types.</param>
        /// <returns>The same attributes test builder.</returns>
        public static TAttributesTestBuilder SpecifyingProduction <TAttributesTestBuilder>(
            this IControllerActionAttributesTestBuilder <TAttributesTestBuilder> controllerActionAttributesTestBuilder,
            IEnumerable <string> ofContentTypes)
            where TAttributesTestBuilder : IControllerActionAttributesTestBuilder <TAttributesTestBuilder>
        {
            var actualBuilder = (BaseAttributesTestBuilder <TAttributesTestBuilder>)controllerActionAttributesTestBuilder;

            actualBuilder.ContainingAttributeOfType <ProducesAttribute>();

            actualBuilder.Validations.Add(attrs =>
            {
                var consumesAttribute = actualBuilder.GetAttributeOfType <ProducesAttribute>(attrs);

                ContentTypeValidator.ValidateAttributeContentTypes(
                    consumesAttribute,
                    ofContentTypes,
                    actualBuilder.ThrowNewAttributeAssertionException);
            });

            return(actualBuilder.AttributesTestBuilder);
        }