private string WriteAndVerifyCollection(StreamResponseMessage responseMessage, ODataCollectionWriter odataWriter,
                                                bool hasModel, string mimeType)
        {
            var collectionStart = new ODataCollectionStart()
            {
                Name = "BackupContactInfo", Count = 12, NextPageLink = new Uri("http://localhost")
            };

            if (!hasModel)
            {
                collectionStart.SetSerializationInfo(new ODataCollectionStartSerializationInfo()
                {
                    CollectionTypeName = "Collection(" + NameSpace + "ContactDetails)"
                });
            }

            odataWriter.WriteStart(collectionStart);
            odataWriter.WriteItem(WritePayloadHelper.CreatePrimaryContactODataComplexValue());
            odataWriter.WriteEnd();

            Stream stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                var settings = new ODataMessageReaderSettings()
                {
                    BaseUri = this.ServiceUri
                };

                ODataMessageReader    messageReader = new ODataMessageReader(responseMessage, settings, WritePayloadHelper.Model);
                ODataCollectionReader reader        = messageReader.CreateODataCollectionReader(WritePayloadHelper.ContactDetailType);
                bool collectionRead = false;
                while (reader.Read())
                {
                    if (reader.State == ODataCollectionReaderState.CollectionEnd)
                    {
                        collectionRead = true;
                    }
                }

                Assert.IsTrue(collectionRead, "collectionRead");
                Assert.AreEqual(ODataCollectionReaderState.Completed, reader.State);
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
示例#2
0
        public void WriteEntryWithWrongSerializationInfo()
        {
            foreach (var mimeType in this.testMimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri()
                {
                    ServiceRoot = this.ServiceUri
                };

                // wrong EntitySetName for entry
                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataEntryWriter();
                    var entry       = this.CreatePersonEntryWithoutSerializationInfo();

                    entry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                    {
                        NavigationSourceName = "Parsen", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains("Person(-5)\",\"PersonId\":-5"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Parsen/$entity"));
                    }
                }

                // wrong EntitySetName for feed
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataFeedWriter();

                    var feed = this.CreatePersonFeed();
                    feed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                    {
                        NavigationSourceName = "Parsen", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    var entry = this.CreatePersonEntryWithoutSerializationInfo();
                    odataWriter.WriteStart(feed);
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();
                    odataWriter.WriteEnd();
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains("Person(-5)\",\"PersonId\":-5"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Parsen\""));
                    }
                }

                // wrong collection type name
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter    = messageWriter.CreateODataCollectionWriter();
                    var collctionStart = new ODataCollectionStart()
                    {
                        Name = "BackupContactInfo"
                    };
                    collctionStart.SetSerializationInfo(new ODataCollectionStartSerializationInfo()
                    {
                        CollectionTypeName = "Collection(" + NameSpace + "ContactDETAIL)"
                    });

                    odataWriter.WriteStart(collctionStart);
                    odataWriter.WriteItem(
                        WritePayloadHelper.CreatePrimaryContactODataComplexValue());
                    odataWriter.WriteEnd();
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains("value\":[{\"EmailBag\":["));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Collection(" + NameSpace + "ContactDETAIL)"));
                    }
                }

                // The following two cases of wrong reference link info is no longer active. The ODataEntityReferenceLinkSerializationInfo is ignored when writer writes the payload.
                // Consider removing them.

                // wrong reference link info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var link = new ODataEntityReferenceLink()
                    {
                        Url = new Uri(this.ServiceUri + "Order(-10)")
                    };
                    messageWriter.WriteEntityReferenceLink(link);
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains(this.ServiceUri + "Order(-10)"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.context
                        Assert.IsTrue(result.Contains("$metadata#$ref"));
                    }
                }

                // wrong reference links info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var links = new ODataEntityReferenceLinks()
                    {
                        Links = new[]
                        {
                            new ODataEntityReferenceLink()
                            {
                                Url = new Uri(this.ServiceUri + "Order(-10)")
                            }
                        },
                    };

                    messageWriter.WriteEntityReferenceLinks(links);
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains(this.ServiceUri + "Order(-10)"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Collection($ref)"));
                    }
                }
            }
        }