private string WriteAndVerifyRequestMessage(ODataMessageWriterSettings settings, string mimeType, bool hasModel) { // create an order entry to POST var order = new ODataEntry() { TypeName = NameSpace + "Order" }; var orderP1 = new ODataProperty { Name = "OrderId", Value = -10 }; var orderp2 = new ODataProperty { Name = "CustomerId", Value = 8212 }; var orderp3 = new ODataProperty { Name = "Concurrency", Value = null }; order.Properties = new[] { orderP1, orderp2, orderp3 }; if (!hasModel) { order.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceName = "Order", NavigationSourceEntityTypeName = NameSpace + "Order" }); orderP1.SetSerializationInfo(new ODataPropertySerializationInfo() { PropertyKind = ODataPropertyKind.Key }); } Dictionary<string, object> expectedOrderObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.OrderType, "Order(-10)", order, hasModel); // write the request message and read using ODL reader var requestMessage = new StreamRequestMessage(new MemoryStream(), new Uri(this.ServiceUri + "Order"), "POST"); requestMessage.SetHeader("Content-Type", mimeType); string result = string.Empty; using (var messageWriter = this.CreateODataMessageWriter(requestMessage, settings, hasModel)) { var odataWriter = this.CreateODataEntryWriter(messageWriter, WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType, hasModel); odataWriter.WriteStart(order); odataWriter.WriteEnd(); Stream stream = requestMessage.GetStream(); result = WritePayloadHelper.ReadStreamContent(stream); if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata)) { stream.Seek(0, SeekOrigin.Begin); var readerSetting = new ODataMessageReaderSettings() { BaseUri = this.ServiceUri }; ODataMessageReader messageReader = new ODataMessageReader(requestMessage, readerSetting, WritePayloadHelper.Model); ODataReader reader = messageReader.CreateODataEntryReader(WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType); bool verifyEntryCalled = false; while (reader.Read()) { if (reader.State == ODataReaderState.EntryEnd) { ODataEntry entry = reader.Item as ODataEntry; Assert.AreEqual(3, entry.Properties.Count(), "entry.Properties.Count"); verifyEntryCalled = true; } } Assert.AreEqual(ODataReaderState.Completed, reader.State); Assert.IsTrue(verifyEntryCalled, "verifyEntryCalled"); } } // For Json light, verify the resulting metadata is as expected if (mimeType != MimeTypes.ApplicationAtomXml) { JavaScriptSerializer jScriptSerializer = new JavaScriptSerializer(); Dictionary<string, object> resultObject = jScriptSerializer.DeserializeObject(result) as Dictionary<string, object>; // AutoComputePayloadMetadataInJson has no effect on request message metadata Assert.AreEqual(this.ServiceUri + "$metadata#Order/$entity", resultObject.Single(e => e.Key == JsonLightConstants.ODataContextAnnotationName).Value); resultObject.Remove(JsonLightConstants.ODataContextAnnotationName); foreach (var pair in resultObject) { Assert.IsFalse(pair.Key.Contains("odata.") || pair.Key.StartsWith("#")); } } return result; }
public void RequestMessageTest() { foreach (var mimeType in this.mimeTypes) { var settings = new ODataMessageWriterSettings(); settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri }; string outputWithModel = null; string outputWithoutModel = null; var requestMessageWithModel = new StreamRequestMessage(new MemoryStream(), new Uri(this.ServiceUri + "Order"), "POST"); requestMessageWithModel.SetHeader("Content-Type", mimeType); using (var messageWriter = new ODataMessageWriter(requestMessageWithModel, settings, WritePayloadHelper.Model)) { var odataWriter = messageWriter.CreateODataEntryWriter(WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType); outputWithModel = this.WriteAndVerifyRequestMessage(requestMessageWithModel, odataWriter, true, mimeType); } var requestMessageWithoutModel = new StreamRequestMessage(new MemoryStream(), new Uri(this.ServiceUri + "Order"), "POST"); requestMessageWithoutModel.SetHeader("Content-Type", mimeType); using (var messageWriter = new ODataMessageWriter(requestMessageWithoutModel, settings)) { var odataWriter = messageWriter.CreateODataEntryWriter(); outputWithoutModel = this.WriteAndVerifyRequestMessage(requestMessageWithoutModel, odataWriter, false, mimeType); } WritePayloadHelper.VerifyPayloadString(outputWithModel, outputWithoutModel, mimeType); } }