コード例 #1
0
        public void CanWriteResultODataOutputFormatter_ReturnsBooleanValueAsExpected(Type type, ODataPayloadKind payloadKind, bool expected)
        {
            // Arrange & Act
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <Customer>("Customers");
            IEdmModel        model        = builder.GetEdmModel();
            IEdmEntitySet    entitySet    = model.EntityContainer.FindEntitySet("Customers");
            EntitySetSegment entitySetSeg = new EntitySetSegment(entitySet);
            HttpRequest      request      = RequestFactory.Create(opt => opt.AddRouteComponents("odata", model));

            request.ODataFeature().RoutePrefix = "odata";
            request.ODataFeature().Model       = model;
            request.ODataFeature().Path        = new ODataPath(entitySetSeg);

            OutputFormatterWriteContext context = new OutputFormatterWriteContext(
                request.HttpContext,
                (s, e) => null,
                objectType: type,
                @object: null);

            ODataOutputFormatter formatter = new ODataOutputFormatter(new[] { payloadKind });

            formatter.SupportedMediaTypes.Add("application/json");

            // Assert
            Assert.Equal(expected, formatter.CanWriteResult(context));
        }
コード例 #2
0
        public void CanWriteResultODataOutputFormatter_ThrowsArgumentNull_IfContextIsNull()
        {
            // Arrange & Act
            ODataOutputFormatter formatter = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });

            // Assert
            ExceptionAssert.ThrowsArgumentNull(() => formatter.CanWriteResult(context: null), "context");
        }
コード例 #3
0
        public void WriteResponseHeadersODataOutputFormatter_ThrowsArgumentNull_Context()
        {
            // Arrange & Act
            ODataOutputFormatter formatter = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });

            // Assert
            ExceptionAssert.ThrowsArgumentNull(() => formatter.WriteResponseHeaders(context: null), "context");
        }
コード例 #4
0
        internal static ObjectResult GetContent <T>(T content, ODataOutputFormatter formatter, string mediaType)
        {
            ObjectResult objectResult = new ObjectResult(content);

            objectResult.Formatters.Add(formatter);
            objectResult.ContentTypes.Add(mediaType);

            return(objectResult);
        }
コード例 #5
0
        public void WriteResponseBodyAsyncODataOutputFormatter_ThrowsArgumentNull_Context()
        {
            // Arrange & Act
            ODataOutputFormatter formatter = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });
            Encoding             encoding  = Encoding.UTF8;

            // Assert
            ExceptionAssert.ThrowsArgumentNull(() => formatter.WriteResponseBodyAsync(context: null, encoding), "context");
        }
コード例 #6
0
        private static bool CanWriteType(ODataOutputFormatter formatter, Type type, HttpRequest request)
        {
            var context = new OutputFormatterWriteContext(
                request.HttpContext,
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                objectType: type,
                @object: null);

            return(formatter.CanWriteResult(context));
        }
コード例 #7
0
        internal static ObjectResult GetContent(object content, Type type, ODataOutputFormatter formatter, string mediaType)
        {
            ObjectResult objectResult = new ObjectResult(content);

            objectResult.DeclaredType = type;
            objectResult.Formatters.Add(formatter);
            objectResult.ContentTypes.Add(mediaType);

            return(objectResult);
        }
コード例 #8
0
        public void WriteResponseBodyAsyncODataOutputFormatter_ThrowsArgumentNull_ObjectType()
        {
            // Arrange & Act
            ODataOutputFormatter        formatter = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });
            OutputFormatterWriteContext context   = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                (s, e) => null,
                null,
                null);

            // Assert
            ExceptionAssert.ThrowsArgumentNull(() => formatter.WriteResponseBodyAsync(context, Encoding.UTF8), "type");
        }
コード例 #9
0
        public void CanWriteResultODataOutputFormatter_ReturnsFalseIfNoODataPathSet()
        {
            // Arrange & Act
            OutputFormatterWriteContext context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                (s, e) => null,
                typeof(int),
                6);

            ODataOutputFormatter formatter = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });

            // Assert
            Assert.False(formatter.CanWriteResult(context));
        }
コード例 #10
0
        public void GetSupportedContentTypesODataOutputFormatter_WorksForContentType()
        {
            // Arrange
            ODataOutputFormatter formatter = ODataOutputFormatterFactory.Create().First();

            Assert.NotNull(formatter); // guard

            // Act & Assert
            IReadOnlyList <string> contentTypes = formatter.GetSupportedContentTypes("application/json", typeof(string));

            Assert.Equal(12, contentTypes.Count);

            // Act & Assert
            formatter.SupportedMediaTypes.Clear();
            ExceptionAssert.DoesNotThrow(() => formatter.GetSupportedContentTypes("application/json", typeof(string)));
        }
コード例 #11
0
        public void CanWriteResultODataOutputFormatter_Throws_IfRequestIsNull()
        {
            // Arrange & Act
            ODataOutputFormatter formatter   = new ODataOutputFormatter(new[] { ODataPayloadKind.Resource });
            Mock <HttpContext>   httpContext = new Mock <HttpContext>();

            httpContext.Setup(c => c.Request).Returns((HttpRequest)null);
            OutputFormatterWriteContext context = new OutputFormatterWriteContext(httpContext.Object,
                                                                                  (s, e) => null,
                                                                                  typeof(int),
                                                                                  6);

            // Assert
            ExceptionAssert.Throws <InvalidOperationException>(
                () => formatter.CanWriteResult(context),
                "The OData formatter requires an attached request in order to serialize.");
        }
コード例 #12
0
        internal static ODataOutputFormatter GetFormatter(ODataPayloadKind[] payload, HttpRequest request, string mediaType = null)
        {
            // request is not needed on AspNetCore.
            ODataOutputFormatter formatter;

            formatter = new ODataOutputFormatter(payload);
            formatter.SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
            formatter.SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true));

            if (mediaType != null)
            {
                formatter.SupportedMediaTypes.Add(mediaType);
            }
            else
            {
                formatter.SupportedMediaTypes.Add(ODataMediaTypes.ApplicationJsonODataMinimalMetadata);
                formatter.SupportedMediaTypes.Add(ODataMediaTypes.ApplicationXml);
            }
            return(formatter);
        }
コード例 #13
0
        public void OutputFormatsSkipsOfOdataFormatExists()
        {
            var builder = new WebHostBuilder()
                          .UseStartup <TestStartup>();

            TestServer testServer = new TestServer(builder);

            var options    = testServer.Host.Services.GetRequiredService <IConfigureOptions <MvcOptions> >();
            var mvcOptions = new MvcOptions();

            var format = new ODataOutputFormatter(new List <ODataPayloadKind>());

            format.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("text/plain"));
            mvcOptions.OutputFormatters.Add(format);

            options.Configure(mvcOptions);

            var formater = (ODataOutputFormatter)mvcOptions.OutputFormatters.First();

            Assert.AreEqual(1, formater.SupportedMediaTypes.Count());
            Assert.AreEqual("text/plain", formater.SupportedMediaTypes[0].ToString());
        }
コード例 #14
0
 public void TryGetContentHeaderODataOutputFormatter_ThrowsArgumentNull_Type()
 {
     // Arrange & Act & Assert
     ExceptionAssert.ThrowsArgumentNull(() => ODataOutputFormatter.TryGetContentHeader(null, null, out _), "type");
 }
コード例 #15
0
 public void GetDefaultBaseAddressODataOutputFormatter_ThrowsArgumentNull_Request()
 {
     // Arrange & Act & Assert
     ExceptionAssert.ThrowsArgumentNull(() => ODataOutputFormatter.GetDefaultBaseAddress(null), "request");
 }