public void ReadValueOfTypeDefinitionShouldWork() { Stream stream = new MemoryStream(Encoding.Default.GetBytes("123")); IODataResponseMessage responseMessage = new InMemoryMessage() { StatusCode = 200, Stream = stream }; ODataMessageReader reader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), new EdmModel()); reader.ReadValue(new EdmTypeDefinitionReference(new EdmTypeDefinition("NS", "Length", EdmPrimitiveTypeKind.Int32), true)).Should().Be(123); }
public void ReadValueOfAbbreviativeDateShouldWork() { Stream stream = new MemoryStream(Encoding.Default.GetBytes("2014-1-3")); IODataResponseMessage responseMessage = new InMemoryMessage() { StatusCode = 200, Stream = stream }; ODataMessageReader reader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), new EdmModel()); reader.ReadValue(new EdmTypeDefinitionReference(new EdmTypeDefinition("NS", "DateValue", EdmPrimitiveTypeKind.Date), true)).Should().Be(new Date(2014, 1, 3)); }
public void ReadValueOfTimeOfDayShouldWork() { Stream stream = new MemoryStream(Encoding.Default.GetBytes("12:30:04.998")); IODataResponseMessage responseMessage = new InMemoryMessage() { StatusCode = 200, Stream = stream }; ODataMessageReader reader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), new EdmModel()); reader.ReadValue(new EdmTypeDefinitionReference(new EdmTypeDefinition("NS", "TimeOfDayValue", EdmPrimitiveTypeKind.TimeOfDay), true)).Should().Be(new TimeOfDay(12, 30, 4, 998)); }
public void CreateMessageReaderShouldSetAnnotationFilterWhenODataAnnotationIsSetOnPreferenceAppliedHeader() { IODataResponseMessage responseMessage = new InMemoryMessage(); responseMessage.PreferenceAppliedHeader().AnnotationFilter = "*"; ODataMessageReader reader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings()); reader.Settings.ShouldIncludeAnnotation.Should().NotBeNull(); }
public void EncodingShouldRemainInvariantInReader() { Stream stream = new MemoryStream(Encoding.GetEncoding("iso-8859-1").GetBytes("{\"@odata.context\":\"http://stuff/#Edm.Int32\",\"value\":4}")); IODataResponseMessage responseMessage = new InMemoryMessage() { StatusCode = 200, Stream = stream }; responseMessage.SetHeader("Content-Type", "application/json;odata.metadata=minimal;"); ODataMessageReader reader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), new EdmModel()); reader.ReadProperty(); }
public void CreateMessageReaderShouldNotSetAnnotationFilterWhenItIsAlreadySet() { IODataResponseMessage responseMessage = new InMemoryMessage(); responseMessage.PreferenceAppliedHeader().AnnotationFilter = "*"; Func<string, bool> shouldWrite = name => false; ODataMessageReader reader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings { ShouldIncludeAnnotation = shouldWrite }); reader.Settings.ShouldIncludeAnnotation.Should().BeSameAs(shouldWrite); }
public void GetDataServiceVersionWorksForResponse() { InMemoryMessage simulatedRequestMessage = new InMemoryMessage(); simulatedRequestMessage.SetHeader(ODataConstants.ODataVersionHeader, "4.0"); IODataResponseMessage response = new ODataResponseMessage(simulatedRequestMessage, false, false, long.MaxValue); ODataVersion version = response.GetODataVersion(ODataVersion.V4); version.Should().Be(ODataUtils.StringToODataVersion(response.GetHeader(ODataConstants.ODataVersionHeader))); }
public void GetAcceptHeaderWithInnerValueGetsInnerValue() { const string headerName = "Accept"; const string headerValue = "json-rox"; var simulatedRequestMessage = new InMemoryMessage(); simulatedRequestMessage.SetHeader(headerName, headerValue); var odataRequestMessage = new ODataRequestMessage(simulatedRequestMessage, false, false, -1); odataRequestMessage.GetHeader(headerName).Should().Be(headerValue); }
public void GetHeaderGoesToInnerMessageAfterConstruction() { const string headerName = "CustomHeaderName"; const string headerValue = "CustomerHeaderValue"; var simulatedRequestMessage = new InMemoryMessage(); simulatedRequestMessage.SetHeader(headerName, headerValue); var odataRequestMessage = new ODataRequestMessage(simulatedRequestMessage, false, false, -1); odataRequestMessage.GetHeader(headerName).Should().Be(headerValue); }
public void SetHeaderIsNotAllowedWhenReading() { const string headerName = "CustomHeaderName"; const string headerValueBefore = "CustomerHeaderValueBefore"; const string headerValueAfter = "CustomerHeaderValueAfter"; var simulatedRequestMessage = new InMemoryMessage(); simulatedRequestMessage.SetHeader(headerName, headerValueBefore); var odataRequestMessage = new ODataRequestMessage(simulatedRequestMessage, false, false, -1); Action setHeader = (() => odataRequestMessage.SetHeader(headerName, headerValueAfter)); setHeader.ShouldThrow<ODataException>().WithMessage(Strings.ODataMessage_MustNotModifyMessage); }
public void GetHeaderOnInnerMessagePicksUpSetHeaderFromOuterCallOnWriting() { const string headerName = "CustomHeaderName"; const string headerValueBefore = "CustomerHeaderValueBefore"; const string headerValueAfter = "CustomerHeaderValueAfter"; var simulatedRequestMessage = new InMemoryMessage { Method = "GET", Url = new Uri("http://example.com/Customers") }; simulatedRequestMessage.SetHeader(headerName, headerValueBefore); var odataRequestMessage = new ODataRequestMessage(simulatedRequestMessage, true, false, -1); odataRequestMessage.SetHeader(headerName, headerValueAfter); simulatedRequestMessage.GetHeader(headerName).Should().Be(headerValueAfter); }
public void WriteTopLevelUIntPropertyShouldWork() { var settings = new ODataMessageWriterSettings(); settings.ODataUri.ServiceRoot = new Uri("http://host/service"); settings.SetContentType(ODataFormat.Json); var model = new EdmModel(); model.GetUInt32("MyNS", false); IODataRequestMessage request = new InMemoryMessage() { Stream = new MemoryStream() }; var writer = new ODataMessageWriter(request, settings, model); Action write = () => writer.WriteProperty(new ODataProperty() { Name = "Id", Value = (UInt32)123 }); write.ShouldNotThrow(); request.GetStream().Position = 0; var reader = new StreamReader(request.GetStream()); string output = reader.ReadToEnd(); output.Should().Be("{\"@odata.context\":\"http://host/service/$metadata#MyNS.UInt32\",\"value\":123}"); }
public void DisposeShouldBeCalledOnResponseMessageForExecuteWithNoContent() { DataServiceContext context = new DataServiceContext(); bool responseMessageDisposed = false; context.Configurations.RequestPipeline.OnMessageCreating = args => { var requestMessage = new InMemoryMessage { Url = args.RequestUri, Method = args.Method, Stream = new MemoryStream() }; var responseMessage = new InMemoryMessage { StatusCode = 204, Stream = new MemoryStream() }; responseMessage.DisposeAction = () => responseMessageDisposed = true; return new TestDataServiceClientRequestMessage(requestMessage, () => responseMessage); }; context.Execute(new Uri("http://host/voidAction", UriKind.Absolute), "POST").StatusCode.Should().Be(204); responseMessageDisposed.Should().BeTrue(); }
protected virtual InMemoryMessage CreateRequestMessage(DataServiceClientRequestMessageArgs requestMessageArgs) { var requestMessage = new InMemoryMessage { Url = requestMessageArgs.RequestUri, Method = requestMessageArgs.Method, Stream = new MemoryStream() }; foreach (var header in requestMessageArgs.Headers) { requestMessage.SetHeader(header.Key, header.Value); } return requestMessage; }
protected override InMemoryMessage CreateRequestMessage(DataServiceClientRequestMessageArgs requestMessageArgs) { this.lastMessageCreated = base.CreateRequestMessage(requestMessageArgs); return this.lastMessageCreated; }
private static void RunClientRequest(Action<DataServiceContext> runTest, string expectedRequestPayload = null, string responsePayload = null, Action<IODataResponseMessage> setupResponse = null) { using (var requestStream = new MemoryStream()) using (var responseStream = responsePayload == null ? new MemoryStream() : new MemoryStream(Encoding.UTF8.GetBytes(responsePayload))) { responseStream.Position = 0; var requestMessage = new InMemoryMessage {Stream = requestStream}; var responseMessage = new InMemoryMessage {Stream = responseStream}; if (setupResponse != null) { setupResponse(responseMessage); } DataServiceContext ctx = new DataServiceContextWithCustomTransportLayer(ODataProtocolVersion.V4, requestMessage, responseMessage); runTest(ctx); if (expectedRequestPayload != null) { var actualRequestPayload = Encoding.UTF8.GetString(requestStream.ToArray()); Assert.AreEqual(expectedRequestPayload, actualRequestPayload); } } }
public void WriteTopLevelEntityReferenceLinks() { ODataEntityReferenceLink link1 = new ODataEntityReferenceLink { Url = new Uri("http://host/Customers(1)") }; link1.InstanceAnnotations.Add(new ODataInstanceAnnotation("Is.New", new ODataPrimitiveValue(true))); ODataEntityReferenceLink link2 = new ODataEntityReferenceLink { Url = new Uri("http://host/Customers(2)") }; link2.InstanceAnnotations.Add(new ODataInstanceAnnotation("TestNamespace.unknown", new ODataPrimitiveValue(123))); link2.InstanceAnnotations.Add(new ODataInstanceAnnotation("custom.annotation", new ODataPrimitiveValue(456))); ODataEntityReferenceLinks referencelinks = new ODataEntityReferenceLinks() { Links = new[] { link1, link2 } }; var writerSettings = new ODataMessageWriterSettings { DisableMessageStreamDisposal = true }; writerSettings.SetContentType(ODataFormat.Json); writerSettings.SetServiceDocumentUri(new Uri("http://odata.org/test")); MemoryStream stream = new MemoryStream(); IODataResponseMessage requestMessageToWrite = new InMemoryMessage { StatusCode = 200, Stream = stream }; requestMessageToWrite.PreferenceAppliedHeader().AnnotationFilter = "*"; using (var messageWriter = new ODataMessageWriter(requestMessageToWrite, writerSettings, EdmModel)) { messageWriter.WriteEntityReferenceLinks(referencelinks); } stream.Position = 0; string payload = (new StreamReader(stream)).ReadToEnd(); string expectedPayload = "{\"@odata.context\":\"http://odata.org/test/$metadata#Collection($ref)\",\"value\":[{\"@odata.id\":\"http://host/Customers(1)\",\"@Is.New\":true},{\"@odata.id\":\"http://host/Customers(2)\",\"@TestNamespace.unknown\":123,\"@custom.annotation\":456}]}"; Assert.Equal(expectedPayload, payload); }
public void ErrorLocationReportedByMessageReaderForBadEdmxOfSingleLineShouldBeAbsolute() { Stream stream = new MemoryStream(Encoding.UTF8.GetBytes( "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<!--Remark-->" + "<edmx:Edmx xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\" Version=\"4.0\">" + "<edmx:DataServices>" + "<Schema xmlns=\"http://docs.oasis-open.org/odata/ns/edm\" Namespace=\"Org.OData.Core.V1\" Alias=\"Core\">" + "<Annotation Term=\"Core.Description\">" + "<String>Core terms needed to write vocabularies</String>" + "</Annotation>" + "</Schema>" + "</edmx:DataServices>" + "</edmx:Edmx>")); IODataResponseMessage responseMessage = new InMemoryMessage() { StatusCode = 200, Stream = stream }; responseMessage.SetHeader("Content-Type", "application/xml"); ODataMessageReader reader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), new EdmModel()); const string expectedErrorMessage = "The metadata document could not be read from the message content.\r\n" + "UnexpectedXmlElement : The schema element 'Annotation' was not expected in the given context. : (1, 250)\r\n"; Action test = () => reader.ReadMetadataDocument(); test.ShouldThrow<ODataException>().WithMessage(expectedErrorMessage, ComparisonMode.Exact); }
public void ReadTopLevelPropertyWithTypeDefinitionShouldWork() { Stream stream = new MemoryStream(Encoding.UTF8.GetBytes("{value:123}")); IODataRequestMessage requestMessage = new InMemoryMessage() { Stream = stream }; var model = new EdmModel(); ODataMessageReader reader = new ODataMessageReader(requestMessage, new ODataMessageReaderSettings(), model); Action read = () => reader.ReadProperty(model.GetUInt32("MyNS", false)); read.ShouldNotThrow(); }
public void ReadMetadataDocumentShouldIncludeConverterForDefaultUnsignedIntImplementation() { Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(@"<?xml version=""1.0"" encoding=""utf-8""?> <!--Remark--> <edmx:Edmx xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"" Version=""4.0""> <edmx:DataServices> <Schema xmlns=""http://docs.oasis-open.org/odata/ns/edm"" Namespace=""MyNS""> <TypeDefinition Name=""UInt16"" UnderlyingType=""Edm.Int32"" /> <TypeDefinition Name=""UInt32"" UnderlyingType=""Edm.Decimal"" /> <TypeDefinition Name=""UInt64"" UnderlyingType=""Edm.Decimal"" /> <EntityType Name=""Person""> <Key> <PropertyRef Name=""Id"" /> </Key> <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"" /> <Property Name=""UInt16"" Type=""MyNS.UInt16"" Nullable=""false"" /> <Property Name=""UInt32"" Type=""MyNS.UInt32"" Nullable=""false"" /> <Property Name=""UInt64"" Type=""MyNS.UInt64"" Nullable=""false"" /> </EntityType> </Schema> </edmx:DataServices> </edmx:Edmx>")); IODataResponseMessage responseMessage = new InMemoryMessage() { StatusCode = 200, Stream = stream }; responseMessage.SetHeader("Content-Type", "application/xml"); ODataMessageReader reader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), new EdmModel()); var model = reader.ReadMetadataDocument(); var personType = model.FindDeclaredType("MyNS.Person") as IEdmEntityType; Assert.NotNull(personType); var uint16Type = personType.FindProperty("UInt16").Type; var uint32Type = personType.FindProperty("UInt32").Type; var uint64Type = personType.FindProperty("UInt64").Type; var uint16Converter = model.GetPrimitiveValueConverter(uint16Type.AsTypeDefinition()); var uint32Converter = model.GetPrimitiveValueConverter(uint32Type.AsTypeDefinition()); var uint64Converter = model.GetPrimitiveValueConverter(uint64Type.AsTypeDefinition()); Assert.Equal(Type.GetTypeCode(uint16Converter.ConvertToUnderlyingType((UInt16)123).GetType()), TypeCode.Int32); Assert.Equal(Type.GetTypeCode(uint32Converter.ConvertToUnderlyingType((UInt32)123).GetType()), TypeCode.UInt32); Assert.Equal(Type.GetTypeCode(uint64Converter.ConvertToUnderlyingType((UInt64)123).GetType()), TypeCode.Decimal); Assert.Equal(Type.GetTypeCode(uint16Converter.ConvertFromUnderlyingType(123).GetType()), TypeCode.UInt16); Assert.Equal(Type.GetTypeCode(uint32Converter.ConvertFromUnderlyingType((Int64)123).GetType()), TypeCode.Int64); Assert.Equal(Type.GetTypeCode(uint64Converter.ConvertFromUnderlyingType((Decimal)123).GetType()), TypeCode.UInt64); }