示例#1
0
        /// <summary>Sends the current request to the server.</summary>
        public override void SendRequest()
        {
            if (this.ServiceType == null)
            {
                throw new InvalidOperationException("DataServiceType or ServiceType property should be set for in-process requests.");
            }

            // Set up an in-process host.
            host = new TestServiceHost2();
            host.RequestAcceptCharSet = this.AcceptCharset;
            host.RequestHttpMethod    = this.HttpMethod;
            host.RequestPathInfo      = this.RequestUriString;
            host.RequestStream        = this.RequestStream;
            host.RequestContentType   = this.RequestContentType;
            host.RequestContentLength = this.RequestContentLength;
            host.RequestIfMatch       = this.IfMatch;
            host.RequestIfNoneMatch   = this.IfNoneMatch;
            host.RequestMaxVersion    = this.RequestMaxVersion;
            foreach (var header in this.RequestHeaders)
            {
                host.RequestHeaders.Add(header.Key, header.Value);
            }

            host.RequestVersion = this.RequestVersion;

            if (this.Accept != null)
            {
                host.RequestAccept = this.Accept;
            }

            // An important side-effect of the following line of code is to
            // clean the TestArguments static for tests which don't use it.
            BaseTestWebRequest.LoadSerializedTestArguments(ArgumentsToString(this.TestArguments));
            try
            {
                // Create a data service instance and hook into the processing directly - only GET supported right now.
                Type serviceType = this.ServiceType;
                try
                {
                    object serviceInstance = Activator.CreateInstance(serviceType);

                    MethodInfo attachMethod = serviceType.GetMethod("AttachHost");
                    attachMethod.Invoke(serviceInstance, new object[] { host });

                    MethodInfo method = serviceType.GetMethod("ProcessRequest", Type.EmptyTypes);
                    method.Invoke(serviceInstance, null);

                    this.responseStream = GetResponseStream(host);
                }
                catch (TargetInvocationException invocationException)
                {
                    this.responseStream = GetResponseStream(host);
                    if (invocationException.InnerException != null)
                    {
                        if (BaseTestWebRequest.SaveOriginalStackTrace &&
                            invocationException.InnerException.Data != null)
                        {
                            invocationException.InnerException.Data["OriginalStackTrace"] = invocationException.InnerException.StackTrace;
                        }

                        throw invocationException.InnerException;
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    if (this.RequestStream != null)
                    {
                        AstoriaTestLog.IsTrue(this.RequestStream.CanRead, "request stream must be open");
                    }

                    if (this.responseStream != null)
                    {
                        AstoriaTestLog.IsTrue(this.responseStream.CanRead, "response stream must be open");
                    }
                }
            }
            finally
            {
                LoadSerializedTestArguments(null);
            }
        }
 public DataServiceHostRequestMessage(TestServiceHost2 host)
 {
     this.host = host;
 }
        private static void SendRequestAndVerifyUriAndContext(DSPServiceDefinition service,
            Func<IEnumerable<Tuple<Func<DataServiceContext, IQueryable>[], Func<DataServiceContext, string>, Action<DataServiceContext, IQueryable, List<object>>>>> queryUrisAndValidations)
        {
            using (InProcessWebRequest request = (InProcessWebRequest)service.CreateForInProcess())
            {
                request.StartService();
                ODataProtocolVersion[] versions = new ODataProtocolVersion[] { ODataProtocolVersion.V4 };
                t.TestUtil.RunCombinations(queryUrisAndValidations(), versions, (testCase, version) =>
                {
                    var getQueries = testCase.Item1;
                    var getExpectedUri = testCase.Item2;
                    var validate = testCase.Item3;
                    foreach (var getQuery in getQueries)
                    {
                        var host = new t.TestServiceHost2();
                        var requestMessage = new DataServiceHostRequestMessage(host);
                        var ctx = new DataServiceContextWithCustomTransportLayer(request.ServiceRoot, version, () => requestMessage, () =>
                        {
                            request.SendRequest(host);
                            return new DataServiceHostResponseMessage(host);
                        });
                        ctx.EnableAtom = true;
                        ctx.Format.UseAtom();

                        if (version < ODataProtocolVersion.V4)
                        {
                            var query = getQuery(ctx);
                            try
                            {
                                foreach (var p in query) { }
                                Assert.Fail("Exception expected but received none.");
                            }
                            catch (NotSupportedException e)
                            {
                                Assert.IsTrue(
                                    "The expression 'TypeAs' is not supported when MaxProtocolVersion is less than '4.0'." == e.Message ||
                                    "The method 'OfType' is not supported when MaxProtocolVersion is less than '4.0'." == e.Message);
                            }
                        }
                        else
                        {
                            #region Setup resolvers and events

                            ctx.ResolveType = (typeName) =>
                            {
                                if (typeName == typeof(Employee).FullName)
                                {
                                    return typeof(Employee);
                                }

                                if (typeName == typeof(PeopleManager).FullName)
                                {
                                    return typeof(PeopleManager);
                                }

                                return null;
                            };

                            ctx.SendingRequest2 += (sender, e) =>
                            {
                                Assert.AreEqual("4.0", e.RequestMessage.GetHeader("OData-Version"), "OData-Version mismatch.");
                                Assert.AreEqual("4.0", e.RequestMessage.GetHeader("OData-MaxVersion"), "OData-MaxVersion mismatch.");
                            };

                            #endregion Setup resolvers and events

                            // Validate Uri
                            var query = getQuery(ctx);
                            string clientGeneratedUri = query.ToString();
                            Assert.AreEqual(getExpectedUri(ctx), clientGeneratedUri);

                            #region Validate entities

                            List<object> materializedObjects = new List<object>();

                            // Materialize and validate LinkInfos
                            foreach (var e in query)
                            {
                                EntityDescriptor descriptor = ctx.GetEntityDescriptor(e);
                                if (descriptor != null)
                                {
                                    foreach (var link in descriptor.LinkInfos)
                                    {
                                        switch (link.Name)
                                        {
                                            case "Aquaintances":
                                            case "DirectReports":
                                            case "Manager":
                                            case "Colleagues":
                                            case "BestFriend":
                                            case "Friends":
                                                // If the entity is not of the base type (Person), then expect navigation links to have a type segment.
                                                if (descriptor.ServerTypeName != "AstoriaUnitTests.Tests.DerivedProperty.Person")
                                                {
                                                    t.TestUtil.AssertContains(link.NavigationLink.OriginalString, descriptor.ServerTypeName);
                                                    if (link.AssociationLink != null)
                                                    {
                                                        t.TestUtil.AssertContains(link.AssociationLink.OriginalString, descriptor.ServerTypeName);
                                                    }
                                                }
                                                break;
                                            default:
                                                Assert.Fail("Unexpected link: " + link.Name);
                                                return;
                                        }
                                    }
                                }

                                materializedObjects.Add(e);
                            }

                            #endregion Validate entities

                            #region Validate Links

                            // Validate LinkDescriptors
                            foreach (LinkDescriptor link in ctx.Links)
                            {
                                string identity = ctx.GetEntityDescriptor(link.Source).Identity.AbsoluteUri;
                                int startIdx = identity.IndexOf('(') + 1;
                                int endIdx = identity.IndexOf(')');
                                int sourceId = int.Parse(identity.Substring(startIdx, endIdx - startIdx));

                                identity = ctx.GetEntityDescriptor(link.Target).Identity.AbsoluteUri;
                                startIdx = identity.IndexOf('(') + 1;
                                endIdx = identity.IndexOf(')');
                                int targetId = int.Parse(identity.Substring(startIdx, endIdx - startIdx));

                                switch (link.SourceProperty)
                                {
                                    case "DirectReports":
                                        switch (sourceId)
                                        {
                                            case 1: //"Foo":
                                                Assert.Fail("DirectReports link not expected for Foo");
                                                break;
                                            case 2: //"Andy":
                                                //Assert.IsTrue(targetName == "Pratik" || targetName == "Jimmy");
                                                Assert.IsTrue(targetId == 3 || targetId == 4);
                                                break;
                                            case 3: //"Pratik":
                                                Assert.Fail("DirectReports link not expected for Pratik");
                                                break;
                                            case 4: //"Jimmy":
                                                Assert.Fail("DirectReports link not expected for Jimmy");
                                                break;
                                            case 5: //"Shyam":
                                                //Assert.IsTrue(targetName == "Andy" || targetName == "Marcelo");
                                                Assert.IsTrue(targetId == 2 || targetId == 6);
                                                break;
                                            case 6: //"Marcelo":
                                                Assert.Fail("DirectReports link not expected for Marcelo");
                                                break;
                                            default:
                                                Assert.Fail("Unrecognized id: " + sourceId);
                                                break;
                                        }
                                        break;
                                    case "Manager":
                                        switch (sourceId)
                                        {
                                            case 1: //"Foo":
                                                Assert.Fail("Manager link not expected for Foo");
                                                break;
                                            case 2: //"Andy":
                                                //Assert.AreEqual("Shyam", targetName);
                                                Assert.AreEqual(5, targetId);
                                                break;
                                            case 3: //"Pratik":
                                                //Assert.AreEqual("Andy", targetName);
                                                Assert.AreEqual(2, targetId);
                                                break;
                                            case 4: //"Jimmy":
                                                //Assert.AreEqual("Andy", targetName);
                                                Assert.AreEqual(2, targetId);
                                                break;
                                            case 5: //"Shyam":
                                                //Assert.AreEqual("Shyam", targetName);
                                                Assert.AreEqual(5, targetId);
                                                break;
                                            case 6: //"Marcelo":
                                                //Assert.AreEqual("Shyam", targetName);
                                                Assert.AreEqual(5, targetId);
                                                break;
                                            default:
                                                Assert.Fail("Unrecognized id: " + sourceId);
                                                break;
                                        }
                                        break;
                                    case "Colleagues":
                                        switch (sourceId)
                                        {
                                            case 1: //"Foo":
                                                Assert.Fail("Colleagues link not expected for Foo");
                                                break;
                                            case 2: //"Andy":
                                                //Assert.AreEqual("Marcelo", targetName);
                                                Assert.AreEqual(6, targetId);
                                                break;
                                            case 3: //"Pratik":
                                                //Assert.AreEqual("Jimmy", targetName);
                                                Assert.AreEqual(4, targetId);
                                                break;
                                            case 4: //"Jimmy":
                                                //Assert.AreEqual("Pratik", targetName);
                                                Assert.AreEqual(3, targetId);
                                                break;
                                            case 5: //"Shyam":
                                                Assert.Fail("Colleagues link not expected for Shyam");
                                                break;
                                            case 6: //"Marcelo":
                                                //Assert.AreEqual("Andy", targetName);
                                                Assert.AreEqual(2, targetId);
                                                break;
                                            default:
                                                Assert.Fail("Unrecognized id: " + sourceId);
                                                break;
                                        }
                                        break;
                                    case "BestFriend":
                                        switch (sourceId)
                                        {
                                            case 1: //"Foo":
                                                //Assert.AreEqual("Pratik", targetName);
                                                Assert.AreEqual(3, targetId);
                                                break;
                                            case 2: //"Andy":
                                                //Assert.AreEqual("Shyam", targetName);
                                                Assert.AreEqual(5, targetId);
                                                break;
                                            case 3: //"Pratik":
                                                //Assert.AreEqual("Andy", targetName);
                                                Assert.AreEqual(2, targetId);
                                                break;
                                            case 4: //"Jimmy":
                                                //Assert.AreEqual("Foo", targetName);
                                                Assert.AreEqual(1, targetId);
                                                break;
                                            case 5: //"Shyam":
                                                //Assert.AreEqual("Marcelo", targetName);
                                                Assert.AreEqual(6, targetId);
                                                break;
                                            case 6: //"Marcelo":
                                                //Assert.AreEqual("Jimmy", targetName);
                                                Assert.AreEqual(4, targetId);
                                                break;
                                            default:
                                                Assert.Fail("Unrecognized id: " + sourceId);
                                                break;
                                        }
                                        break;
                                    case "Friends":
                                        break;
                                    default:
                                        Assert.Fail("Unexpected link descriptor: " + link.SourceProperty);
                                        return;
                                }
                            }

                            #endregion Validate Links

                            // Validation specific to the test case.
                            validate(ctx, query, materializedObjects);
                        }
                    }
                });
            }
        }