diff --git a/src/Microsoft.Restier.Core/Submit/ChangeSetItem.cs b/src/Microsoft.Restier.Core/Submit/ChangeSetItem.cs index 4bd153eb..4d932c81 100644 --- a/src/Microsoft.Restier.Core/Submit/ChangeSetItem.cs +++ b/src/Microsoft.Restier.Core/Submit/ChangeSetItem.cs @@ -352,8 +352,19 @@ private static Expression ApplyPredicate( // Expression value = itemValue != null // ? LinqParameterContainer.Parameterize(itemValue.GetType(), itemValue) // : Expression.Constant(value: null); - var constant = Expression.Constant(itemValue, property.Type); - BinaryExpression equal = Expression.Equal(property, constant); + Expression left = property; + Expression right = Expression.Constant(itemValue, property.Type); + if (property.Type == typeof(byte[])) + { + left = Expression.Call(typeof(BitConverter), "ToString", null, new Expression[] { property }); + right = Expression.Call( + typeof(BitConverter), + "ToString", + null, + new Expression[] { Expression.Constant(itemValue, property.Type) }); + } + + var equal = Expression.Equal(left, right); return where == null ? equal : Expression.AndAlso(where, equal); } } diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Tests/TrippinE2EEtagTestCases.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Tests/TrippinE2EEtagTestCases.cs index 6e5852b6..cd272486 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Tests/TrippinE2EEtagTestCases.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Tests/TrippinE2EEtagTestCases.cs @@ -346,7 +346,7 @@ public void IfNoneMatchCRUDTest() Assert.Equal(200, statusCode); Assert.NotEqual(oldEtag, etag); - // Delete the Entity with If-None-Match matches, should return 412 + // Delete the Entity with If-None-Match matches, should return 412 // If this is not removed, client will auto add If-Match header descriptor = this.TestClientContext.GetEntityDescriptor(flight); descriptor.ETag = null; @@ -392,5 +392,78 @@ public void IfNoneMatchCRUDTest() } Assert.Equal(404, statusCode); } + + [Fact] + public void ByteArrayIfMatchTest() + { + this.TestClientContext.MergeOption = MergeOption.OverwriteChanges; + var airline = new Airline() + { + AirlineCode = "TT", + Name = "Test Airlines" + }; + + this.TestClientContext.AddToAirlines(airline); + this.TestClientContext.SaveChanges(); + + string etag = null; + int statusCode = -1; + EventHandler statusCodeHandler = (sender, eventArgs) => + { + etag = eventArgs.ResponseMessage.GetHeader("ETag"); + statusCode = eventArgs.ResponseMessage.StatusCode; + }; + + this.TestClientContext.ReceivingResponse += statusCodeHandler; + + // Retrieve the matched etag + airline = + this.TestClientContext.Airlines.ByKey(new Dictionary() + { + {"AirlineCode", airline.AirlineCode} + }).GetValue(); + var matchEtag = etag; + + // Retrieve a none match etag + var anotherAirline = + this.TestClientContext.Airlines.ByKey(new Dictionary() {{"AirlineCode", "AA"}}) + .GetValue(); + var nonMatchEtag = etag; + + // Delete the Entity with If-Match does not match, should return 412 + EventHandler sendRequestEvent = (sender, eventArgs) => + { + eventArgs.RequestMessage.SetHeader("If-Match", nonMatchEtag); + }; + + this.TestClientContext.SendingRequest2 += sendRequestEvent; + this.TestClientContext.DeleteObject(airline); + Assert.Throws(() => this.TestClientContext.SaveChanges()); + Assert.Equal(412, statusCode); + + // Delete the Entity with If-Match matches, should return 204 + this.TestClientContext.SendingRequest2 -= sendRequestEvent; + sendRequestEvent = (sender, eventArgs) => + { + eventArgs.RequestMessage.SetHeader("If-Match", matchEtag); + }; + + this.TestClientContext.SendingRequest2 += sendRequestEvent; + this.TestClientContext.DeleteObject(airline); + this.TestClientContext.SaveChanges(); + Assert.Equal(204, statusCode); + + // Query the flight again and entity does not exist. + this.TestClientContext.SendingRequest2 -= sendRequestEvent; + Assert.Throws(() => + airline = + this.TestClientContext.Airlines.ByKey(new Dictionary() + { + {"AirlineCode", airline.AirlineCode} + }).GetValue() + ); + + Assert.Equal(404, statusCode); + } } -} +} \ No newline at end of file