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);
 }
 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));
 }
        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 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 ContentTypeIsFromHost()
        {
            const string contentType = "application/atom+xml";
            var host = new DataServiceHostSimulator { RequestContentType = contentType };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.ContentType.Should().Be(contentType);
        }
        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);
        }
        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);
        }
 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);
 }
        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>();
        }
        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);
        }
        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;
        }
        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 AfterInitializeVerionFieldsAreSetWithHostValues()
        {
            var host = new DataServiceHostSimulator { RequestVersion = "4.0", RequestMaxVersion = "4.0" };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.InitializeRequestVersionHeaders(V4);

            requestMessage.RequestVersion.Should().Be(V4);
            requestMessage.RequestMaxVersion.Should().Be(V4);
        }
        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);
        }
        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 NoCallbackQueryOptionShouldDoNothingSpecial()
 {
     var host = new DataServiceHostSimulator {RequestHttpMethod = "GET"};
     var result = CallbackQueryOptionHandler.HandleCallbackQueryOption(new AstoriaRequestMessage(host), new ODataFormatWithParameters(ODataFormat.Json));
     result.Should().BeNull();
 }
        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 RequestIfNoneMatch()
        {
            const string value = "someguid";
            var host = new DataServiceHostSimulator { RequestIfNoneMatch = value };
            var requestMessage = new AstoriaRequestMessage(host);

            requestMessage.GetHeader("If-None-Match").Should().Be(value);
        }
        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 GetAcceptableContentTypesShouldCallDelegate()
        {
            var contentTypeSelector = new AcceptableContentTypeSelectorSimulator();
            var host = new DataServiceHostSimulator();
            var requestMessage = new AstoriaRequestMessage(host, contentTypeSelector);

            requestMessage.GetAcceptableContentTypes().Should().Be(AcceptableContentTypeSelectorSimulator.GetFormatReturnValue);
        }