Esempio n. 1
0
        public void WriteTopLevelStringPropertyWithStringEscapeOnlyControlsOptionShouldWork()
        {
            var settings         = new ODataMessageWriterSettings();
            var containerBuilder = new Test.OData.DependencyInjection.TestContainerBuilder();

            containerBuilder.AddDefaultODataServices();
            containerBuilder.AddService <IJsonWriterFactory>(ServiceLifetime.Singleton, sp => new DefaultJsonWriterFactory(ODataStringEscapeOption.EscapeOnlyControls));

            settings.ODataUri.ServiceRoot = new Uri("http://host/service");
            settings.SetContentType(ODataFormat.Json);
            var model = new EdmModel();
            IODataRequestMessage request = new InMemoryMessage()
            {
                Stream    = new MemoryStream(),
                Container = containerBuilder.BuildContainer()
            };
            var    writer = new ODataMessageWriter(request, settings, model);
            Action write  = () => writer.WriteProperty(new ODataProperty()
            {
                Name  = "Name",
                Value = "ия"
            });

            write.ShouldNotThrow();
            request.GetStream().Position = 0;
            var    reader = new StreamReader(request.GetStream());
            string output = reader.ReadToEnd();

            output.Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Edm.String\",\"value\":\"ия\"}");
        }
        private string WriterEntry(IEdmModel userModel, ODataResource entry, EdmEntitySet entitySet, IEdmEntityType entityType,
                                   bool fullMetadata = false, Action <ODataWriter> writeAction = null, ODataStringEscapeOption?stringEscapeOption = null)
        {
            var message = new InMemoryMessage()
            {
                Stream = new MemoryStream()
            };

            if (stringEscapeOption != null)
            {
                var containerBuilder = new Test.OData.DependencyInjection.TestContainerBuilder();
                containerBuilder.AddDefaultODataServices();
                containerBuilder.AddService <IJsonWriterFactory>(ServiceLifetime.Singleton, sp => new DefaultJsonWriterFactory(stringEscapeOption.Value));
                message.Container = containerBuilder.BuildContainer();
            }

            var writerSettings = new ODataMessageWriterSettings {
                EnableMessageStreamDisposal = false
            };

            writerSettings.SetContentType(ODataFormat.Json);
            writerSettings.SetServiceDocumentUri(new Uri("http://www.example.com"));
            writerSettings.SetContentType(fullMetadata ?
                                          "application/json;odata.metadata=full;odata.streaming=false" :
                                          "application/json;odata.metadata=minimal;odata.streaming=false", "utf-8");

            using (var msgReader = new ODataMessageWriter((IODataResponseMessage)message, writerSettings, userModel))
            {
                var writer = msgReader.CreateODataResourceWriter(entitySet, entityType);
                if (writeAction != null)
                {
                    writeAction(writer);
                }
                else
                {
                    writer.WriteStart(entry);
                    writer.WriteEnd();
                }
            }

            message.Stream.Seek(0, SeekOrigin.Begin);
            using (StreamReader reader = new StreamReader(message.Stream))
            {
                return(reader.ReadToEnd());
            }
        }