public async Task GetEntryWithIfNoneMatchShouldReturnNotModifiedETagsTest_ForDouble() { string eTag; var getUri = this.BaseAddress + "/double/ETagsCustomers?$format=json"; using (var response = await Client.GetAsync(getUri)) { Assert.True(response.IsSuccessStatusCode); var json = await response.Content.ReadAsAsync <JObject>(); var result = json.GetValue("value") as JArray; Assert.NotNull(result); // check the first eTag = result[0]["@odata.etag"].ToString(); Assert.False(String.IsNullOrEmpty(eTag)); Assert.Equal("W/\"Mi4w\"", eTag); EntityTagHeaderValue parsedValue; Assert.True(EntityTagHeaderValue.TryParse(eTag, out parsedValue)); HttpConfiguration config = new HttpConfiguration(); IETagHandler handler = config.GetETagHandler(); IDictionary <string, object> tags = handler.ParseETag(parsedValue); KeyValuePair <string, object> pair = Assert.Single(tags); Single value = Assert.IsType <Single>(pair.Value); Assert.Equal((Single)2.0, value); } var getRequestWithEtag = new HttpRequestMessage(HttpMethod.Get, this.BaseAddress + "/double/ETagsCustomers(0)"); getRequestWithEtag.Headers.IfNoneMatch.ParseAdd(eTag); using (var response = await Client.SendAsync(getRequestWithEtag)) { Assert.Equal(HttpStatusCode.NotModified, response.StatusCode); } }
public static ETag GetETag(this HttpRequest request, EntityTagHeaderValue entityTagHeaderValue) { if (request == null) { throw Error.ArgumentNull("request"); } if (entityTagHeaderValue != null) { if (entityTagHeaderValue.Equals(EntityTagHeaderValue.Any)) { return(new ETag { IsAny = true }); } // get the etag handler, and parse the etag IETagHandler etagHandler = request.GetRequestContainer().GetRequiredService <IETagHandler>(); IDictionary <string, object> properties = etagHandler.ParseETag(entityTagHeaderValue) ?? new Dictionary <string, object>(); IList <object> parsedETagValues = properties.Select(property => property.Value).AsList(); // get property names from request ODataPath odataPath = request.ODataFeature().Path; IEdmModel model = request.GetModel(); IEdmNavigationSource source = odataPath.NavigationSource; if (model != null && source != null) { IList <IEdmStructuralProperty> concurrencyProperties = model.GetConcurrencyProperties(source).ToList(); IList <string> concurrencyPropertyNames = concurrencyProperties.OrderBy(c => c.Name).Select(c => c.Name).AsList(); ETag etag = new ETag(); if (parsedETagValues.Count != concurrencyPropertyNames.Count) { etag.IsWellFormed = false; } IEnumerable <KeyValuePair <string, object> > nameValues = concurrencyPropertyNames.Zip( parsedETagValues, (name, value) => new KeyValuePair <string, object>(name, value)); foreach (var nameValue in nameValues) { IEdmStructuralProperty property = concurrencyProperties.SingleOrDefault(e => e.Name == nameValue.Key); Contract.Assert(property != null); Type clrType = EdmLibHelpers.GetClrType(property.Type, model); Contract.Assert(clrType != null); if (nameValue.Value != null) { Type valueType = nameValue.Value.GetType(); etag[nameValue.Key] = valueType != clrType ? Convert.ChangeType(nameValue.Value, clrType, CultureInfo.InvariantCulture) : nameValue.Value; } else { etag[nameValue.Key] = nameValue.Value; } } return(etag); } } return(null); }