/// <summary>
        /// Make final changes to the testcase before sending the request.
        /// </summary>
        private static void MakeFinalChangesToTestCase(TestCase testCase, ODataFormat format, MyDSPActionProvider actionProvider, TestWebRequest request)
        {
            foreach (var action in testCase.Operations)
            {
                actionProvider.AddAction((ServiceAction)action);
            }

            testCase.AddBaseUriStringToExpectedDescriptors(request.ServiceRoot.OriginalString, format);

            if (testCase.SubstituteAdvertiseServiceAction != null)
            {
                actionProvider.SubstituteAdvertiseServiceAction = testCase.SubstituteAdvertiseServiceAction;
            }
        }
        private void RunPositiveFunctionTest(ODataFormat format, TestCase testCase)
        {
            // All of the functions tests use the PlaybackService since the WCF Data Services server doesn't support functions
            // The PlaybackService itself will not automatically turn Metadata into an absolute URI, so set that to false on all tests.
            // The tests also use absolute URIs for Target, so suppress that as well.
            testCase.AddBaseUriToMetadata = false;
            testCase.AddBaseUriToTarget   = false;

            using (TestWebRequest request = TestWebRequest.CreateForInProcessWcf())
                using (PlaybackService.ProcessRequestOverride.Restore())
                {
                    request.ServiceType = typeof(AstoriaUnitTests.Stubs.PlaybackService);
                    request.StartService();

                    var payloadBuilder = testCase.ResponsePayloadBuilder;
                    PlaybackService.ProcessRequestOverride.Value = (req) =>
                    {
                        string contentType;
                        if (format == ODataFormat.Json)
                        {
                            contentType             = UnitTestsUtil.JsonLightMimeType;
                            payloadBuilder.Metadata = request.BaseUri + "/$metadata#TestService.CustomerEntities/$entity";
                        }
                        else
                        {
                            contentType = UnitTestsUtil.AtomFormat;
                        }

                        req.SetResponseStreamAsText(PayloadGenerator.Generate(payloadBuilder, format));
                        req.ResponseHeaders.Add("Content-Type", contentType);
                        req.SetResponseStatusCode(200);
                        return(req);
                    };

                    testCase.AddBaseUriStringToExpectedDescriptors(request.ServiceRoot.OriginalString, format);

                    Uri uri = new Uri(request.ServiceRoot + "/" + testCase.RequestUriString);
                    DataServiceContext ctx = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                    //ctx.EnableAtom = true;

                    if (format == ODataFormat.Json)
                    {
                        string serviceEdmx = GetServiceEdmxWithOperations(payloadBuilder);
                        JsonLightTestUtil.ConfigureContextForJsonLight(ctx, serviceEdmx);
                    }

                    QueryOperationResponse <CustomerEntity> qor = (QueryOperationResponse <CustomerEntity>)ctx.Execute <CustomerEntity>(uri);
                    Assert.IsNotNull(qor);
                    Assert.IsNull(qor.Error);

                    IEnumerator <CustomerEntity> entities = qor.GetEnumerator();

                    int expectedDescriptorsPerEntity = 0;

                    while (entities.MoveNext())
                    {
                        CustomerEntity   c  = entities.Current;
                        EntityDescriptor ed = ctx.GetEntityDescriptor(c);
                        IEnumerable <OperationDescriptor> actualDescriptors = ed.OperationDescriptors;
                        TestEquality(actualDescriptors, testCase.GetExpectedDescriptors(format)[expectedDescriptorsPerEntity++]);
                    }
                }
        }
        private void RunPositiveFunctionTest(ODataFormat format, TestCase testCase)
        {
            // All of the functions tests use the PlaybackService since the WCF Data Services server doesn't support functions
            // The PlaybackService itself will not automatically turn Metadata into an absolute URI, so set that to false on all tests.
            // The tests also use absolute URIs for Target, so suppress that as well.
            testCase.AddBaseUriToMetadata = false;
            testCase.AddBaseUriToTarget = false;

            using (TestWebRequest request = TestWebRequest.CreateForInProcessWcf())
            using (PlaybackService.ProcessRequestOverride.Restore())
            {
                request.ServiceType = typeof(AstoriaUnitTests.Stubs.PlaybackService);
                request.StartService();

                var payloadBuilder = testCase.ResponsePayloadBuilder;
                PlaybackService.ProcessRequestOverride.Value = (req) =>
                {
                    string contentType;
                    if (format == ODataFormat.Json)
                    {
                        contentType = UnitTestsUtil.JsonLightMimeType;
                        payloadBuilder.Metadata = request.BaseUri + "/$metadata#TestService.CustomerEntities/$entity";
                    }
                    else
                    {
                        contentType = UnitTestsUtil.AtomFormat;
                    }

                    req.SetResponseStreamAsText(PayloadGenerator.Generate(payloadBuilder, format));
                    req.ResponseHeaders.Add("Content-Type", contentType);
                    req.SetResponseStatusCode(200);
                    return req;
                };

                testCase.AddBaseUriStringToExpectedDescriptors(request.ServiceRoot.OriginalString, format);

                Uri uri = new Uri(request.ServiceRoot + "/" + testCase.RequestUriString);
                DataServiceContext ctx = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                ctx.EnableAtom = true;

                if (format == ODataFormat.Json)
                {
                    string serviceEdmx = GetServiceEdmxWithOperations(payloadBuilder);
                    JsonLightTestUtil.ConfigureContextForJsonLight(ctx, serviceEdmx);
                }

                QueryOperationResponse<CustomerEntity> qor = (QueryOperationResponse<CustomerEntity>)ctx.Execute<CustomerEntity>(uri);
                Assert.IsNotNull(qor);
                Assert.IsNull(qor.Error);

                IEnumerator<CustomerEntity> entities = qor.GetEnumerator();

                int expectedDescriptorsPerEntity = 0;

                while (entities.MoveNext())
                {
                    CustomerEntity c = entities.Current;
                    EntityDescriptor ed = ctx.GetEntityDescriptor(c);
                    IEnumerable<OperationDescriptor> actualDescriptors = ed.OperationDescriptors;
                    TestEquality(actualDescriptors, testCase.GetExpectedDescriptors(format)[expectedDescriptorsPerEntity++]);
                }
            }
        }
        /// <summary>
        /// Make final changes to the testcase before sending the request.
        /// </summary>
        private static void MakeFinalChangesToTestCase(TestCase testCase, ODataFormat format, MyDSPActionProvider actionProvider, TestWebRequest request)
        {
            foreach (var action in testCase.Operations)
            {
                actionProvider.AddAction((ServiceAction)action);
            }

            testCase.AddBaseUriStringToExpectedDescriptors(request.ServiceRoot.OriginalString, format);

            if (testCase.SubstituteAdvertiseServiceAction != null)
            {
                actionProvider.SubstituteAdvertiseServiceAction = testCase.SubstituteAdvertiseServiceAction;
            }
        }