Пример #1
0
        public void ProcessRequestCalledTwiceWithSameHostShouldWork()
        {
            var host = new DataServiceHostSimulator {AbsoluteRequestUri = new Uri("http://example.com/Customers(1)"), AbsoluteServiceUri = new Uri("http://example.com/"), RequestHttpMethod = "GET", ResponseStream = new MemoryStream(), ProcessExceptionCallBack = (args) => { }};
            var svc = new TestService();
            svc.AttachHost(host);
            svc.ProcessRequest();
            host.ResponseStatusCode.Should().Be(200);
            host.ResponseStream.Position = 0;
            var customerResponse = new StreamReader(host.ResponseStream).ReadToEnd();
            customerResponse.Should().Contain("Customer");
            customerResponse.Should().Contain("Redmond Way");
            customerResponse.Should().Contain("*****@*****.**");

            host.ResponseStream = new MemoryStream();
            host.AbsoluteRequestUri = new Uri("http://example.com/Customers(1)/Address");
            // re-attach the host before calling ProcessRequest
            svc.AttachHost(host);
            svc.ProcessRequest();
            host.ResponseStatusCode.Should().Be(200);
            host.ResponseStream.Position = 0;
            var addressResponse = new StreamReader(host.ResponseStream).ReadToEnd();
            addressResponse.Should().Contain("Redmond Way");
            addressResponse.Should().NotContain("*****@*****.**");
            addressResponse.Should().NotMatch(customerResponse);
        }
Пример #2
0
        public void ProcessRequestCalledSecondTimeWithoutAttachHostShouldThrow()
        {
            var host = new DataServiceHostSimulator {AbsoluteRequestUri = new Uri("http://example.com/Customers(1)"), AbsoluteServiceUri = new Uri("http://example.com/"), RequestHttpMethod = "GET", ResponseStream = new MemoryStream(), ProcessExceptionCallBack = (args) => { }};
            var svc = new TestService();
            svc.AttachHost(host);
            svc.ProcessRequest();
            host.ResponseStatusCode.Should().Be(200);
            host.ResponseStream.Position = 0;
            var customerResponse = new StreamReader(host.ResponseStream).ReadToEnd();
            customerResponse.Should().Contain("Customer");
            customerResponse.Should().Contain("Redmond Way");
            customerResponse.Should().Contain("*****@*****.**");

            host.ResponseStream = new MemoryStream();
            host.AbsoluteRequestUri = new Uri("http://example.com/Customers(1)/Address");
            Action secondRequest = () => svc.ProcessRequest();
            secondRequest.ShouldThrow<InvalidOperationException>();
        }
Пример #3
0
        public void Limits_Synchronously()
        {
            // ARRANGE
            var innerStream = new MemoryStream(Encoding.ASCII.GetBytes("foobar"));
            var stream = new LimitedStream(innerStream, 3);

            // ACT
            var actual = new StreamReader(stream, Encoding.ASCII).ReadToEnd();

            // ASSERT
            actual.Should().Be("foo");
        }
        public void It_Reads_Synchronously()
        {
            // ARRANGE
            var buffer = Encoding.ASCII.GetBytes("foobarbaz");
            var innerStream = new MemoryStream(Encoding.ASCII.GetBytes("FOOBAR"));
            var stream = new PartiallyBufferedStream(buffer, 3, 3, innerStream);

            // ACT
            var content = new StreamReader(stream, Encoding.ASCII).ReadToEnd();

            // ASSERT
            content.Should().Be("barFOOBAR");
        }
Пример #5
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");
        }
Пример #6
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");
        }
Пример #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");
        }
Пример #8
0
 public void ResponseStatusCodeIsNotSetOnHostAfterProcessRequestWith404Error()
 {
     HandleExceptionArgs exceptionArgs = null;
     var host = new DataServiceHostSimulator { AbsoluteRequestUri = new Uri("http://example.com/invalid"), AbsoluteServiceUri = new Uri("http://example.com/"), RequestHttpMethod = "GET", ResponseStream = new MemoryStream(), ProcessExceptionCallBack = (args) => { exceptionArgs = args; } };
     var svc = new TestService();
     svc.AttachHost(host);
     svc.ProcessRequest();
     //// Astoria Server fails to set IDSH.ResponseStatusCode for custom hosts in an error scenario
     //// The behavior has been this way from V2 of WCF Data Services and on, when this is changed
     //// it will be a breaking change
     host.ResponseStatusCode.Should().Be(0);
     host.ResponseStream.Position = 0;
     var customerResponse = new StreamReader(host.ResponseStream).ReadToEnd();
     customerResponse.Should().Contain("Resource not found for the segment 'invalid'.");
     exceptionArgs.ResponseStatusCode.Should().Be(404);
 }