Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #491 #495

Merged
merged 1 commit into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}