Пример #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);
        }
        public void Init()
        {
            this.entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake.NS", "Type", false)
            {
                CanReflectOnInstanceType = false
            };
            this.entityType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            this.entityType.SetReadOnly();

            var resourceSet = new ResourceSet("Set", this.entityType);

            resourceSet.SetReadOnly();
            this.resourceSetWrapper = ResourceSetWrapper.CreateForTests(resourceSet);

            this.action = new ServiceAction("Fake", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", this.entityType) }, null);
            this.action.SetReadOnly();

            this.provider = new TestDataServiceProvider();

            this.actionProvider = DataServiceActionProviderWrapper.Create(this.provider, ODataProtocolVersion.V4, () => this.operationContext);
            var host = new DataServiceHostSimulator();

            this.operationContext = new DataServiceOperationContext(host);
            this.operationContext.InitializeAndCacheHeaders(new DataServiceSimulator());
        }
Пример #3
0
        private static void RunNegotiatedFormatTest(string requestAccept, string requestMaxVersion, Microsoft.OData.Client.ODataProtocolVersion maxProtocolVersion, ODataFormat expectedFormat)
        {
            DataServiceHostSimulator host = new DataServiceHostSimulator
            {
                RequestHttpMethod = "GET",
                RequestAccept     = requestAccept,
                RequestMaxVersion = requestMaxVersion,
                RequestVersion    = "4.0",
            };
            DataServiceSimulator service = new DataServiceSimulator
            {
                OperationContext = new DataServiceOperationContext(host),
                Configuration    = new DataServiceConfiguration(new DataServiceProviderSimulator()),
            };

            service.Configuration.DataServiceBehavior.MaxProtocolVersion = maxProtocolVersion;
            service.OperationContext.InitializeAndCacheHeaders(service);
            service.OperationContext.RequestMessage.InitializeRequestVersionHeaders(VersionUtil.ToVersion(maxProtocolVersion));

            var d = new RequestDescription(RequestTargetKind.Primitive, RequestTargetSource.Property, new Uri("http://temp.org/"));

            d.DetermineWhetherResponseBodyOrETagShouldBeWritten(service.OperationContext.RequestMessage.HttpVerb);
            d.DetermineWhetherResponseBodyShouldBeWritten(service.OperationContext.RequestMessage.HttpVerb);
            d.DetermineResponseFormat(service);
            d.ResponseFormat.Should().NotBeNull();
            d.ResponseFormat.Format.Should().BeSameAs(expectedFormat);
        }
Пример #4
0
        public void GetAcceptableContentTypesShouldCallDelegate()
        {
            var contentTypeSelector = new AcceptableContentTypeSelectorSimulator();
            var host           = new DataServiceHostSimulator();
            var requestMessage = new AstoriaRequestMessage(host, contentTypeSelector);

            requestMessage.GetAcceptableContentTypes().Should().Be(AcceptableContentTypeSelectorSimulator.GetFormatReturnValue);
        }
        public void NoCallbackQueryOptionShouldDoNothingSpecial()
        {
            var host = new DataServiceHostSimulator {
                RequestHttpMethod = "GET"
            };
            var result = CallbackQueryOptionHandler.HandleCallbackQueryOption(new AstoriaRequestMessage(host), new ODataFormatWithParameters(ODataFormat.Json));

            result.Should().BeNull();
        }
Пример #6
0
        public void ContentTypeIsFromHost()
        {
            const string contentType = "application/atom+xml";
            var          host        = new DataServiceHostSimulator {
                RequestContentType = contentType
            };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.ContentType.Should().Be(contentType);
        }
Пример #7
0
        public void RequestIfNoneMatch()
        {
            const string value = "someguid";
            var          host  = new DataServiceHostSimulator {
                RequestIfNoneMatch = value
            };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.GetHeader("If-None-Match").Should().Be(value);
        }
Пример #8
0
        public void RequestContentTypeIsFromHost()
        {
            const string value = "a-content-type";
            var          host  = new DataServiceHostSimulator {
                RequestContentType = value
            };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.GetHeader("Content-Type").Should().Be(value);
        }
Пример #9
0
        public void RequestAcceptCharSetIsFromHost()
        {
            const string value = "some_other_value";
            var          host  = new DataServiceHostSimulator {
                RequestAcceptCharSet = value
            };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.GetHeader("Accept-Charset").Should().Be(value);
        }
        public void FailIfMethodIsNotGet()
        {
            var host = new DataServiceHostSimulator {
                RequestHttpMethod = "POST"
            };

            host.SetQueryStringItem("$callback", "foo");
            Action method = () => CallbackQueryOptionHandler.HandleCallbackQueryOption(new AstoriaRequestMessage(host), new ODataFormatWithParameters(ODataFormat.Json));

            method.ShouldThrow <DataServiceException>().WithMessage(ds.Strings.CallbackQueryOptionHandler_GetRequestsOnly);
        }
Пример #11
0
        public void GetAcceptableFormatTypesAcceptHeaderIsFromHost()
        {
            const string value = "some_value";
            var          host  = new DataServiceHostSimulator {
                RequestAccept = value
            };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.InitializeRequestVersionHeaders(V4);
            requestMessage.GetAcceptableContentTypes().Should().Be(value);
        }
        public void CallbackQueryOptionShouldWorkIfRaw()
        {
            var host = new DataServiceHostSimulator {
                RequestHttpMethod = "GET"
            };

            host.SetQueryStringItem("$callback", "foo");
            var result = CallbackQueryOptionHandler.HandleCallbackQueryOption(new AstoriaRequestMessage(host), new ODataFormatWithParameters(ODataFormat.RawValue));

            result.Should().Be("foo");
        }
        public void FailIfContentTypeIsMetadata()
        {
            var host = new DataServiceHostSimulator {
                RequestHttpMethod = "GET"
            };

            host.SetQueryStringItem("$callback", "foo");
            Action method = () => CallbackQueryOptionHandler.HandleCallbackQueryOption(new AstoriaRequestMessage(host), new ODataFormatWithParameters(ODataFormat.Metadata));

            method.ShouldThrow <DataServiceException>().WithMessage(ds.Strings.CallbackQueryOptionHandler_UnsupportedContentType(ODataFormat.Metadata));
        }
Пример #14
0
        public void AfterInitializeVerionFieldsAreSetToDefaults()
        {
            var host           = new DataServiceHostSimulator {
            };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.InitializeRequestVersionHeaders(V4);

            requestMessage.RequestVersion.Should().Be(V4);
            requestMessage.RequestMaxVersion.Should().Be(V4);
        }
Пример #15
0
        public void GetDollarFormatQueryItemShouldGetValueFromHostGetQueryStringItemMethod()
        {
            const string queryKey   = "$format";
            const string queryValue = "custom";
            var          host       = new DataServiceHostSimulator {
            };

            host.SetQueryStringItem(queryKey, queryValue);
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.GetQueryStringItem(queryKey).Should().Be(queryValue);
        }
Пример #16
0
        public void ProcessExceptionShouldCallHost()
        {
            var callbackInvoked = false;
            var host            = new DataServiceHostSimulator {
            };

            host.ProcessExceptionCallBack = args => callbackInvoked = true;
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.ProcessException(new HandleExceptionArgs(new Exception(), true, null, false));
            callbackInvoked.Should().BeTrue();
        }
        public void SettingResponseDataServiceVersionHeaderShouldPassThroughToHost()
        {
            var host = new DataServiceHostSimulator {
            };
            IODataResponseMessage message = new AstoriaResponseMessage(host);
            const string          header  = "OData-Version";
            const string          value   = "value4";

            message.SetHeader(header, value);
            VerifyHeader(message, header, value);
            host.ResponseVersion.Should().Be(value);
        }
        public void StreamIsSetFromMethodNotHost()
        {
            var streamInHost = new MemoryStream();
            var host         = new DataServiceHostSimulator {
                ResponseStream = streamInHost
            };
            IODataResponseMessage message = new AstoriaResponseMessage(host);
            var streamInResponse          = new MemoryStream();

            message.SetStream(streamInResponse);
            message.GetStream().Should().Be(streamInResponse);
        }
        public void IgnoresOtherStuffInHost()
        {
            var host = new DataServiceHostSimulator {
                RequestHttpMethod = "GET"
            };

            host.SetQueryStringItem("$callback", "foo");
            host.SetQueryStringItem("$format", "atom");
            var result = CallbackQueryOptionHandler.HandleCallbackQueryOption(new AstoriaRequestMessage(host), new ODataFormatWithParameters(ODataFormat.Json));

            result.Should().Be("foo");
        }
        public void SettingResponseContentTypeHeaderShouldPassThroughToHost()
        {
            var host = new DataServiceHostSimulator {
            };
            IODataResponseMessage message = new AstoriaResponseMessage(host);
            const string          header  = "Content-Type";
            const string          value   = "value5";

            message.SetHeader(header, value);
            VerifyHeader(message, header, value);
            host.ResponseContentType.Should().Be(value);
        }
Пример #21
0
        public void GetQueryStringItemShouldGetComponentFromHostGetQueryStringItemMethod()
        {
            const string queryKey   = "queryKey";
            const string queryValue = "queryValue";
            var          host       = new DataServiceHostSimulator {
                AbsoluteRequestUri = new Uri("http://www.service.com/there/is/not/even/a/query-string")
            };

            host.SetQueryStringItem(queryKey, queryValue);
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.GetQueryStringItem(queryKey).Should().Be(queryValue);
        }
Пример #22
0
        public void HttpVerbIsFromHost()
        {
            foreach (var verb in HttpVerbUtils.KnownVerbs)
            {
                var host = new DataServiceHostSimulator {
                    RequestHttpMethod = verb.ToString()
                };
                var requestMessage = new AstoriaRequestMessage(host);

                requestMessage.HttpVerb.Should().Be(verb);
                requestMessage.RequestHttpMethod.Should().Be(verb.ToString());
            }
        }
        public void StatusCodeIsTiedToHost()
        {
            var host = new DataServiceHostSimulator {
                ResponseStatusCode = 432
            };
            IODataResponseMessage message = new AstoriaResponseMessage(host);

            message.StatusCode.Should().Be(432);

            const int code = 123;

            message.StatusCode = code;
            message.StatusCode.Should().Be(code);
            host.ResponseStatusCode.Should().Be(code);
        }
Пример #24
0
        public ServiceSimulatorFactory()
        {
            this.baseUri = new Uri("http://localhost");
            this.host    = new DataServiceHostSimulator()
            {
                AbsoluteServiceUri = this.baseUri,
                RequestHttpMethod  = "GET",
                RequestAccept      = "application/atom+xml,application/xml",
                RequestVersion     = "4.0",
                RequestMaxVersion  = "4.0",
            };

            this.provider = new DataServiceProviderSimulator();
            this.behavior = new DataServiceBehavior()
            {
                MaxProtocolVersion = Microsoft.OData.Client.ODataProtocolVersion.V4
            };
        }
Пример #25
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);
        }
Пример #26
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>();
        }
Пример #27
0
        private static DataServiceProviderWrapper CreateProvider(out DataServiceConfiguration config, out DataServiceOperationContext operationContext)
        {
            var baseUri = new Uri("http://localhost");
            var host    = new DataServiceHostSimulator()
            {
                AbsoluteServiceUri = baseUri,
                AbsoluteRequestUri = new Uri(baseUri.AbsoluteUri + "/$metadata", UriKind.Absolute),
                RequestHttpMethod  = "GET",
                RequestAccept      = "application/xml+atom",
                RequestVersion     = "4.0",
                RequestMaxVersion  = "4.0",
            };

            operationContext = new DataServiceOperationContext(host);
            var dataService = new DataServiceSimulator()
            {
                OperationContext = operationContext
            };

            operationContext.InitializeAndCacheHeaders(dataService);

            DataServiceProviderSimulator providerSimulator = new DataServiceProviderSimulator();

            providerSimulator.ContainerNamespace = "MyModel";
            providerSimulator.ContainerName      = "CustomersContainer";

            ResourceType customerEntityType = new ResourceType(
                typeof(object), ResourceTypeKind.EntityType, null, "MyModel", "Customer", false)
            {
                CanReflectOnInstanceType = false
            };

            ResourcePropertyKind idPropertyKind = ResourcePropertyKind.Primitive | ResourcePropertyKind.Key;
            ResourceProperty     idProperty     = new ResourceProperty(
                "Id", idPropertyKind, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            };

            customerEntityType.AddProperty(idProperty);

            ResourcePropertyKind firstNamePropertyKind = ResourcePropertyKind.Primitive | ResourcePropertyKind.Key;
            ResourceProperty     firstNameProperty     = new ResourceProperty(
                "FirstName", firstNamePropertyKind, ResourceType.GetPrimitiveResourceType(typeof(string)))
            {
                CanReflectOnInstanceTypeProperty = false
            };

            customerEntityType.AddProperty(firstNameProperty);

            customerEntityType.SetReadOnly();
            providerSimulator.AddResourceType(customerEntityType);

            ResourceSet customerSet = new ResourceSet("Customers", customerEntityType);

            customerSet.SetReadOnly();
            providerSimulator.AddResourceSet(customerSet);

            config = new DataServiceConfiguration(providerSimulator);
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4;

            IDataServiceProviderBehavior   providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;
            DataServiceStaticConfiguration staticConfig     = new DataServiceStaticConfiguration(dataService.Instance.GetType(), providerSimulator);

            DataServiceProviderWrapper provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(config, staticConfig), providerSimulator, providerSimulator, dataService, false);

            dataService.ProcessingPipeline = new DataServiceProcessingPipeline();
            dataService.Provider           = provider;
            provider.ProviderBehavior      = providerBehavior;
            dataService.ActionProvider     = DataServiceActionProviderWrapper.Create(dataService);
#if DEBUG
            dataService.ProcessingPipeline.SkipDebugAssert = true;
#endif
            operationContext.RequestMessage.InitializeRequestVersionHeaders(VersionUtil.ToVersion(config.DataServiceBehavior.MaxProtocolVersion));
            return(provider);
        }
Пример #28
0
        private void InitWithQueryOptions(string selectQueryOption, string expandQueryOption)
        {
            string queryOption = string.Empty;

            if (selectQueryOption != null && expandQueryOption != null)
            {
                queryOption = "?$select=" + selectQueryOption + "&$expand=" + expandQueryOption;
            }
            else if (selectQueryOption != null)
            {
                queryOption = "?$select=" + selectQueryOption;
            }
            else
            {
                queryOption = "?$expand=" + expandQueryOption;
            }

            this.host = new DataServiceHostSimulator
            {
                AbsoluteRequestUri = new Uri("http://fake.org/FakeSet" + queryOption),
                AbsoluteServiceUri = new Uri("http://fake.org/"),
                RequestHttpMethod  = "GET",
                RequestVersion     = "2.0",
            };

            if (selectQueryOption != null)
            {
                this.host.SetQueryStringItem("$select", selectQueryOption);
            }

            if (expandQueryOption != null)
            {
                this.host.SetQueryStringItem("$expand", expandQueryOption);
            }

            DataServiceProviderSimulator provider = new DataServiceProviderSimulator
            {
                ContainerName      = "SelectTestContainer",
                ContainerNamespace = "SelectTestNamespace"
            };

            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "SelectTestNamespace", "Fake", false)
            {
                CanReflectOnInstanceType = false, IsOpenType = true
            };

            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            var resourceSet = new ResourceSet("FakeSet", resourceType);

            resourceSet.SetReadOnly();

            provider.AddResourceSet(resourceSet);

            DataServiceConfiguration configuration = new DataServiceConfiguration(provider);

            configuration.SetEntitySetAccessRule("*", EntitySetRights.All);
            this.service = new DataServiceSimulator
            {
                OperationContext = new DataServiceOperationContext(this.host),
                Configuration    = configuration,
            };

            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(this.service.Instance.GetType(), provider);
            IDataServiceProviderBehavior   providerBehavior    = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            this.service.Provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(
                    configuration,
                    staticConfiguration),
                provider,
                provider,
                this.service,
                false);

            this.service.ProcessingPipeline        = new DataServiceProcessingPipeline();
            this.service.Provider.ProviderBehavior = providerBehavior;
            this.service.ActionProvider            = DataServiceActionProviderWrapper.Create(this.service);
            this.service.OperationContext.InitializeAndCacheHeaders(service);
        }