Permalink
Browse files

Making sure we use the same casing when loading a document by id whic…

…h differ in casing
  • Loading branch information...
1 parent 903eacf commit 3de314b9ccaa9be186e0dc50f68e4dc1ee65f1e8 @ayende ayende committed May 9, 2012
@@ -10,6 +10,7 @@
using System.Text;
using Newtonsoft.Json;
using System;
+using Raven.Abstractions.Data;
using Raven.Json.Linq;
namespace Raven.Abstractions.Extensions
@@ -116,6 +117,8 @@ public static RavenJObject FilterHeaders(this RavenJObject self, bool isServerDo
{
if(header.Key.StartsWith("Temp"))
continue;
+ if(header.Key == Constants.DocumentIdFieldName)
+ continue;
if (HeadersToIgnoreClient.Contains(header.Key))
continue;
if (isServerDocument && HeadersToIgnoreServerDocument.Contains(header.Key))
@@ -316,7 +316,9 @@ public Task<JsonDocument> GetAsync(string key)
try
{
var requestJson = task.Result;
- return SerializationHelper.DeserializeJsonDocument(key, requestJson, request.ResponseHeaders, request.ResponseStatusCode);
+ var docKey = request.ResponseHeaders[Constants.DocumentIdFieldName] ?? key;
+ request.ResponseHeaders.Remove(Constants.DocumentIdFieldName);
+ return SerializationHelper.DeserializeJsonDocument(docKey, requestJson, request.ResponseHeaders, request.ResponseStatusCode);
}
catch (AggregateException e)
{
@@ -309,7 +309,7 @@ private string ReadStringInternal(Func<WebResponse> getResponse)
}
#endif
- ResponseHeaders = response.Headers;
+ ResponseHeaders = new NameValueCollection(response.Headers);
ResponseStatusCode = ((HttpWebResponse)response).StatusCode;
using (var responseStream = response.GetResponseStreamWithHttpDecompression())
{
@@ -255,7 +255,9 @@ public JsonDocument DirectGet(string serverUrl, string key)
try
{
var responseJson = request.ReadResponseJson();
- return SerializationHelper.DeserializeJsonDocument(key, responseJson, request.ResponseHeaders, request.ResponseStatusCode);
+ var docKey = request.ResponseHeaders[Constants.DocumentIdFieldName] ?? key;
+ request.ResponseHeaders.Remove(Constants.DocumentIdFieldName);
+ return SerializationHelper.DeserializeJsonDocument(docKey, responseJson, request.ResponseHeaders, request.ResponseStatusCode);
}
catch (WebException e)
{
@@ -155,11 +155,18 @@ public Task<JsonDocument> GetAsync(string key)
try
{
var token = task.Result;
+ var docKey = key;
+ IList<string> list;
+ if(request.ResponseHeaders.TryGetValue(Constants.DocumentIdFieldName, out list))
+ {
+ docKey = list.FirstOrDefault() ?? key;
+ request.ResponseHeaders.Remove(Constants.DocumentIdFieldName);
+ }
return new JsonDocument
{
DataAsJson = (RavenJObject)token,
NonAuthoritativeInformation = request.ResponseStatusCode == HttpStatusCode.NonAuthoritativeInformation,
- Key = key,
+ Key = docKey,
LastModified = DateTime.ParseExact(request.ResponseHeaders[Constants.LastModified].First(), "r", CultureInfo.InvariantCulture).ToLocalTime(),
Etag = new Guid(request.ResponseHeaders["ETag"].First()),
Metadata = request.ResponseHeaders.FilterHeaders(isServerDocument: false)
@@ -124,6 +124,7 @@ private void Head(IHttpContext context, string docId)
{
context.SetStatusToNonAuthoritativeInformation();
}
+ documentMetadata.Metadata[Constants.DocumentIdFieldName] = documentMetadata.Key;
documentMetadata.Metadata[Constants.LastModified] = documentMetadata.LastModified; //HACK ? to get the document's last modified value into the response headers
context.WriteHeaders(documentMetadata.Metadata, documentMetadata.Etag.Value);
}
@@ -142,6 +143,7 @@ private void GetDocumentDirectly(IHttpContext context, string docId)
}
Debug.Assert(doc.Etag != null);
doc.Metadata[Constants.LastModified] = doc.LastModified;
+ doc.Metadata[Constants.DocumentIdFieldName] = doc.Key;
context.WriteData(doc.DataAsJson, doc.Metadata, doc.Etag.Value);
}
@@ -4,6 +4,7 @@
// </copyright>
// -----------------------------------------------------------------------
using System;
+using Raven.Client.Document;
using Xunit;
namespace Raven.Tests.MailingList
@@ -46,6 +47,13 @@ public void ShouldNotChange_Munin()
{
var car = session.Load<Car>("BMW_z3_2009");
Assert.True(String.CompareOrdinal(car.Id,"Bmw_z3_2009") == 0);
+ session.SaveChanges();
+ }
+
+ using (var session = store.OpenSession())
+ {
+ var car = session.Load<Car>("BMW_z3_2009");
+ Assert.True(String.CompareOrdinal(car.Id, "Bmw_z3_2009") == 0);
}
}
}
@@ -73,6 +81,51 @@ public void ShouldNotChange_Esent()
{
var car = session.Load<Car>("BMW_z3_2009");
Assert.True(String.CompareOrdinal(car.Id, "Bmw_z3_2009") == 0);
+ session.SaveChanges();
+ }
+
+ using (var session = store.OpenSession())
+ {
+ var car = session.Load<Car>("BMW_z3_2009");
+ Assert.True(String.CompareOrdinal(car.Id, "Bmw_z3_2009") == 0);
+ }
+ }
+ }
+
+ [Fact]
+ public void ShouldNotChange_remote()
+ {
+ using(GetNewServer())
+ using (var store = new DocumentStore
+ {
+ Url = "http://localhost:8079"
+ }.Initialize())
+ {
+ using (var session = store.OpenSession())
+ {
+ session.Store(new Car
+ {
+ Id = "Bmw_z3_2009",
+ Brand =
+ "Test"
+ });
+ session.SaveChanges();
+
+ var car = session.Load<Car>("BMW_z3_2009");
+ Assert.True(String.CompareOrdinal(car.Id, "Bmw_z3_2009") == 0);
+ }
+
+ using (var session = store.OpenSession())
+ {
+ var car = session.Load<Car>("BMW_z3_2009");
+ Assert.True(String.CompareOrdinal(car.Id, "Bmw_z3_2009") == 0);
+ session.SaveChanges();
+ }
+
+ using (var session = store.OpenSession())
+ {
+ var car = session.Load<Car>("BMW_z3_2009");
+ Assert.True(String.CompareOrdinal(car.Id, "Bmw_z3_2009") == 0);
}
}
}

0 comments on commit 3de314b

Please sign in to comment.