예제 #1
0
        public void StreamEncodingRemainsInvariant()
        {
            var settings = new ODataMessageWriterSettings {
                JsonPCallback = "functionName", EnableMessageStreamDisposal = false
            };

            settings.SetServiceDocumentUri(new Uri("http://stuff"));
            StreamWriter          streamWriter = new StreamWriter(new MemoryStream(), Encoding.GetEncoding("iso-8859-1"));
            IODataResponseMessage message      = new InMemoryMessage {
                StatusCode = 200, Stream = streamWriter.BaseStream
            };

            message.SetHeader("Content-Type", "application/json");
            var property = new ODataProperty {
                Name = "PropertyName", Value = "value"
            };

            using (var writer = new ODataMessageWriter(message, settings))
            {
                writer.WriteProperty(property);
            }

            var responseStream = message.GetStream();

            responseStream.Position = 0;
            var responseString = new StreamReader(responseStream, Encoding.GetEncoding("iso-8859-1")).ReadToEnd();

            Assert.Equal("functionName({\"@odata.context\":\"http://stuff/$metadata#Edm.String\",\"value\":\"value\"})", responseString);
            Assert.StartsWith("text/javascript", message.GetHeader("Content-Type"));
        }
예제 #2
0
        public void JsonPaddingEnabledWithUserSpecifiedContentType()
        {
            var settings = new ODataMessageWriterSettings {
                JsonPCallback = "functionName", EnableMessageStreamDisposal = false
            };

            settings.SetServiceDocumentUri(new Uri("http://stuff"));
            IODataResponseMessage message = new InMemoryMessage {
                StatusCode = 200, Stream = new MemoryStream()
            };

            message.SetHeader("Content-Type", "application/json");
            var property = new ODataProperty {
                Name = "PropertyName", Value = "value"
            };

            using (var writer = new ODataMessageWriter(message, settings))
            {
                writer.WriteProperty(property);
            }

            var responseStream = message.GetStream();

            responseStream.Position = 0;
            var responseString = new StreamReader(responseStream).ReadToEnd();

            responseString.Should().Be("functionName({\"@odata.context\":\"http://stuff/$metadata#Edm.String\",\"value\":\"value\"})");
            message.GetHeader("Content-Type").Should().StartWith("text/javascript");
        }
예제 #3
0
        public void JsonPaddingEnabledWithRawValueSpecified()
        {
            var settings = new ODataMessageWriterSettings {JsonPCallback = "functionName", DisableMessageStreamDisposal = true};
            settings.SetContentType(ODataFormat.RawValue);
            IODataResponseMessage message = new InMemoryMessage {StatusCode = 200, Stream = new MemoryStream()};

            using (var writer = new ODataMessageWriter(message, settings))
            {
                writer.WriteValue(123);
            }

            var responseStream = message.GetStream();
            responseStream.Position = 0;
            var responseString = new StreamReader(responseStream).ReadToEnd();
            responseString.Should().Be("functionName(123)");
            message.GetHeader("Content-Type").Should().StartWith("text/javascript");
        }
        public void GetHeaderOnInnerMessagePicksUpSetHeaderFromOuterCallOnWriting()
        {
            const string headerName        = "CustomHeaderName";
            const string headerValueBefore = "CustomerHeaderValueBefore";
            const string headerValueAfter  = "CustomerHeaderValueAfter";

            var simulatedRequestMessage = new InMemoryMessage {
                Method = "GET", Url = new Uri("http://example.com/Customers")
            };

            simulatedRequestMessage.SetHeader(headerName, headerValueBefore);

            var odataRequestMessage = new ODataRequestMessage(simulatedRequestMessage, true, false, -1);

            odataRequestMessage.SetHeader(headerName, headerValueAfter);

            simulatedRequestMessage.GetHeader(headerName).Should().Be(headerValueAfter);
        }
예제 #5
0
        public void JsonPaddingEnabledWithJsonFormatSpecified()
        {
            var settings = new ODataMessageWriterSettings {JsonPCallback = "functionName", DisableMessageStreamDisposal = true};
            settings.SetContentType(ODataFormat.Json);
            settings.SetServiceDocumentUri(new Uri("http://stuff"));
            IODataResponseMessage message = new InMemoryMessage {StatusCode = 200, Stream = new MemoryStream()};
            var property = new ODataProperty {Name = "PropertyName", Value = "value"};
            
            using (var writer = new ODataMessageWriter(message, settings))
            {
                writer.WriteProperty(property);
            }

            var responseStream = message.GetStream();
            responseStream.Position = 0;
            var responseString = new StreamReader(responseStream).ReadToEnd();
            responseString.Should().Be("functionName({\"@odata.context\":\"http://stuff/$metadata#Edm.String\",\"value\":\"value\"})");
            message.GetHeader("Content-Type").Should().StartWith("text/javascript");
        }
예제 #6
0
        public void JsonPaddingEnabledWithRawValueSpecified()
        {
            var settings = new ODataMessageWriterSettings {
                JsonPCallback = "functionName", EnableMessageStreamDisposal = false
            };

            settings.SetContentType(ODataFormat.RawValue);
            IODataResponseMessage message = new InMemoryMessage {
                StatusCode = 200, Stream = new MemoryStream()
            };

            using (var writer = new ODataMessageWriter(message, settings))
            {
                writer.WriteValue(123);
            }

            var responseStream = message.GetStream();

            responseStream.Position = 0;
            var responseString = new StreamReader(responseStream).ReadToEnd();

            responseString.Should().Be("functionName(123)");
            message.GetHeader("Content-Type").Should().StartWith("text/javascript");
        }
예제 #7
0
        public void StreamEncodingRemainsInvariant()
        {
            var settings = new ODataMessageWriterSettings { JsonPCallback = "functionName", DisableMessageStreamDisposal = true };
            settings.SetServiceDocumentUri(new Uri("http://stuff"));
            StreamWriter streamWriter = new StreamWriter(new MemoryStream(), Encoding.GetEncoding("iso-8859-1"));
            IODataResponseMessage message = new InMemoryMessage { StatusCode = 200, Stream = streamWriter.BaseStream};
            message.SetHeader("Content-Type", "application/json");
            var property = new ODataProperty { Name = "PropertyName", Value = "value" };

            using (var writer = new ODataMessageWriter(message, settings))
            {
                writer.WriteProperty(property);
            }

            var responseStream = message.GetStream();
            responseStream.Position = 0;
            var responseString = new StreamReader(responseStream, Encoding.GetEncoding("iso-8859-1")).ReadToEnd();       
            responseString.Should().Be("functionName({\"@odata.context\":\"http://stuff/$metadata#Edm.String\",\"value\":\"value\"})");
            message.GetHeader("Content-Type").Should().StartWith("text/javascript");
        }
        private void WriteAndValidateContextUri(ODataFormat odataFormat, EdmModel edmModel, Action<ODataMessageWriter> test, string expectedUri, out string payload, out string contentType)
        {
            var message = new InMemoryMessage() { Stream = new MemoryStream() };

            var writerSettings = new ODataMessageWriterSettings
            {
                DisableMessageStreamDisposal = true,
                PayloadBaseUri = new Uri(TestBaseUri),
                AutoComputePayloadMetadataInJson = true,
                EnableAtom = true
            };
            writerSettings.SetServiceDocumentUri(new Uri(TestBaseUri));
            writerSettings.SetContentType(odataFormat);

            using (var msgWriter = new ODataMessageWriter((IODataResponseMessage)message, writerSettings, edmModel))
            {
                test(msgWriter);
            }

            message.Stream.Seek(0, SeekOrigin.Begin);
            using (StreamReader reader = new StreamReader(message.Stream))
            {
                contentType = message.GetHeader("Content-Type");
                payload = reader.ReadToEnd();
                payload.Should().Contain(expectedUri);
            }
        }