Skip to content

Commit

Permalink
fix issue OData#491
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsking committed Aug 18, 2016
1 parent e1cc300 commit e2343a2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/Microsoft.Restier.Core/Submit/ChangeSetItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ReceivingResponseEventArgs> 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<string, object>()
{
{"AirlineCode", airline.AirlineCode}
}).GetValue();
var matchEtag = etag;

// Retrieve a none match etag
var anotherAirline =
this.TestClientContext.Airlines.ByKey(new Dictionary<string, object>() {{"AirlineCode", "AA"}})
.GetValue();
var nonMatchEtag = etag;

// Delete the Entity with If-Match does not match, should return 412
EventHandler<SendingRequest2EventArgs> sendRequestEvent = (sender, eventArgs) =>
{
eventArgs.RequestMessage.SetHeader("If-Match", nonMatchEtag);
};

this.TestClientContext.SendingRequest2 += sendRequestEvent;
this.TestClientContext.DeleteObject(airline);
Assert.Throws<DataServiceRequestException>(() => 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<DataServiceQueryException>(() =>
airline =
this.TestClientContext.Airlines.ByKey(new Dictionary<string, object>()
{
{"AirlineCode", airline.AirlineCode}
}).GetValue()
);

Assert.Equal(404, statusCode);
}
}
}
}

0 comments on commit e2343a2

Please sign in to comment.