Skip to content
Draft
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
96 changes: 65 additions & 31 deletions src/CosmosDB.InMemoryEmulator/InMemoryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ namespace CosmosDB.InMemoryEmulator;
/// </remarks>
internal class InMemoryContainer : Container, IContainerTestSetup
{
// Ref: https://learn.microsoft.com/en-us/rest/api/cosmos-db/
// Error message constants matching real Cosmos DB REST API response bodies.
private const string MessageEntityDoesNotExist = "Entity with the specified id does not exist in the system.";
private const string MessageEntityAlreadyExists = "Entity with the specified id already exists in the system.";
// Ref: https://learn.microsoft.com/en-us/rest/api/cosmos-db/replace-a-document (412 response)
private const string MessagePreconditionFailed = "One of the specified pre-condition is not met";

private static readonly JsonSerializerSettings JsonSettings = new()
{
TypeNameHandling = TypeNameHandling.None,
Expand Down Expand Up @@ -717,7 +724,7 @@ public override async Task<ItemResponse<T>> CreateItemAsync<T>(
ValidateUniqueKeys(jObj, pk);
if (!_items.TryAdd(key, json))
{
throw new InMemoryCosmosException($"Entity with the specified id already exists in the system. id = {itemId}",
throw new InMemoryCosmosException($"{MessageEntityAlreadyExists} id = {itemId}",
HttpStatusCode.Conflict, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}
}
Expand All @@ -726,7 +733,7 @@ public override async Task<ItemResponse<T>> CreateItemAsync<T>(
{
if (!_items.TryAdd(key, json))
{
throw new InMemoryCosmosException($"Entity with the specified id already exists in the system. id = {itemId}",
throw new InMemoryCosmosException($"{MessageEntityAlreadyExists} id = {itemId}",
HttpStatusCode.Conflict, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}
}
Expand Down Expand Up @@ -786,7 +793,7 @@ public override Task<ItemResponse<T>> ReadItemAsync<T>(
if (!_items.TryGetValue(key, out var json) || IsExpired(key))
{
EvictIfExpired(key);
throw new InMemoryCosmosException($"Entity with the specified id does not exist in the system. id = {id}",
throw new InMemoryCosmosException($"{MessageEntityDoesNotExist} id = {id}",
HttpStatusCode.NotFound, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}

Expand Down Expand Up @@ -824,7 +831,7 @@ public override async Task<ItemResponse<T>> UpsertItemAsync<T>(

if (requestOptions?.IfMatchEtag is not null && !_items.ContainsKey(key))
{
throw new InMemoryCosmosException($"Entity with the specified id does not exist. id = {itemId}",
throw new InMemoryCosmosException($"{MessageEntityDoesNotExist} id = {itemId}",
HttpStatusCode.NotFound, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}

Expand Down Expand Up @@ -934,7 +941,7 @@ public override async Task<ItemResponse<T>> ReplaceItemAsync<T>(
if (!_items.ContainsKey(key) || IsExpired(key))
{
EvictIfExpired(key);
throw new InMemoryCosmosException($"Entity with the specified id does not exist. id = {id}",
throw new InMemoryCosmosException($"{MessageEntityDoesNotExist} id = {id}",
HttpStatusCode.NotFound, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}

Expand Down Expand Up @@ -1018,7 +1025,7 @@ public override async Task<ItemResponse<T>> DeleteItemAsync<T>(
if (!_items.TryGetValue(key, out var existingJson) || IsExpired(key))
{
EvictIfExpired(key);
throw new InMemoryCosmosException($"Entity with the specified id does not exist. id = {id}",
throw new InMemoryCosmosException($"{MessageEntityDoesNotExist} id = {id}",
HttpStatusCode.NotFound, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}

Expand Down Expand Up @@ -1099,7 +1106,7 @@ private ItemResponse<T> PatchItemCore<T>(
if (!_items.TryGetValue(key, out var existingJson) || IsExpired(key))
{
EvictIfExpired(key);
throw new InMemoryCosmosException($"Entity with the specified id does not exist. id = {id}",
throw new InMemoryCosmosException($"{MessageEntityDoesNotExist} id = {id}",
HttpStatusCode.NotFound, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}

Expand All @@ -1118,7 +1125,7 @@ private ItemResponse<T> PatchItemCore<T>(
new Dictionary<string, object>(), null);
if (!matches)
{
throw new InMemoryCosmosException("Precondition Failed",
throw new InMemoryCosmosException(MessagePreconditionFailed,
HttpStatusCode.PreconditionFailed, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}
}
Expand Down Expand Up @@ -1208,16 +1215,19 @@ public override async Task<ResponseMessage> CreateItemStreamAsync(
lock (_uniqueKeyWriteLock)
{
if (!ValidateUniqueKeysStream(jObj, pk))
return CreateResponseMessage(HttpStatusCode.Conflict);
return CreateResponseMessage(HttpStatusCode.Conflict,
reason: $"{MessageEntityAlreadyExists} id = {itemId}");

if (!_items.TryAdd(key, json))
return CreateResponseMessage(HttpStatusCode.Conflict);
return CreateResponseMessage(HttpStatusCode.Conflict,
reason: $"{MessageEntityAlreadyExists} id = {itemId}");
}
}
else
{
if (!_items.TryAdd(key, json))
return CreateResponseMessage(HttpStatusCode.Conflict);
return CreateResponseMessage(HttpStatusCode.Conflict,
reason: $"{MessageEntityAlreadyExists} id = {itemId}");
}

var etag = GenerateETag();
Expand Down Expand Up @@ -1258,7 +1268,8 @@ public override Task<ResponseMessage> ReadItemStreamAsync(
if (!_items.TryGetValue(key, out var json) || IsExpired(key))
{
EvictIfExpired(key);
return Task.FromResult(CreateResponseMessage(HttpStatusCode.NotFound));
return Task.FromResult(CreateResponseMessage(HttpStatusCode.NotFound,
reason: $"{MessageEntityDoesNotExist} id = {id}"));
}
var etag = _etags.GetValueOrDefault(key);
if (!CheckIfNoneMatchStream(requestOptions, key))
Expand Down Expand Up @@ -1306,12 +1317,14 @@ public override async Task<ResponseMessage> UpsertItemStreamAsync(
// IfMatch on non-existent item: return NotFound (matching real Cosmos)
if (requestOptions?.IfMatchEtag is not null && !_items.ContainsKey(key))
{
return CreateResponseMessage(HttpStatusCode.NotFound);
return CreateResponseMessage(HttpStatusCode.NotFound,
reason: $"{MessageEntityDoesNotExist} id = {itemId}");
}

if (!CheckIfMatchStream(requestOptions, key))
{
return CreateResponseMessage(HttpStatusCode.PreconditionFailed);
return CreateResponseMessage(HttpStatusCode.PreconditionFailed,
reason: MessagePreconditionFailed);
}
bool existed;
string etag;
Expand All @@ -1324,7 +1337,8 @@ public override async Task<ResponseMessage> UpsertItemStreamAsync(
lock (_uniqueKeyWriteLock)
{
if (!ValidateUniqueKeysStream(jObj, pk, excludeItemId: itemId))
return CreateResponseMessage(HttpStatusCode.Conflict);
return CreateResponseMessage(HttpStatusCode.Conflict,
reason: $"{MessageEntityAlreadyExists} id = {itemId}");
existed = _items.TryGetValue(key, out previousJson);
if (existed) { previousEtag = _etags.GetValueOrDefault(key); previousTimestamp = _timestamps.GetValueOrDefault(key); }
etag = GenerateETag();
Expand All @@ -1337,7 +1351,8 @@ public override async Task<ResponseMessage> UpsertItemStreamAsync(
else
{
if (!ValidateUniqueKeysStream(jObj, pk, excludeItemId: itemId))
return CreateResponseMessage(HttpStatusCode.Conflict);
return CreateResponseMessage(HttpStatusCode.Conflict,
reason: $"{MessageEntityAlreadyExists} id = {itemId}");
existed = _items.TryGetValue(key, out previousJson);
if (existed) { previousEtag = _etags.GetValueOrDefault(key); previousTimestamp = _timestamps.GetValueOrDefault(key); }
etag = GenerateETag();
Expand Down Expand Up @@ -1412,12 +1427,14 @@ public override async Task<ResponseMessage> ReplaceItemStreamAsync(
if (!_items.TryGetValue(key, out var previousJson) || IsExpired(key))
{
EvictIfExpired(key);
return CreateResponseMessage(HttpStatusCode.NotFound);
return CreateResponseMessage(HttpStatusCode.NotFound,
reason: $"{MessageEntityDoesNotExist} id = {id}");
}

if (!CheckIfMatchStream(requestOptions, key))
{
return CreateResponseMessage(HttpStatusCode.PreconditionFailed);
return CreateResponseMessage(HttpStatusCode.PreconditionFailed,
reason: MessagePreconditionFailed);
}

var previousEtag = _etags.GetValueOrDefault(key);
Expand All @@ -1437,7 +1454,8 @@ public override async Task<ResponseMessage> ReplaceItemStreamAsync(
lock (_uniqueKeyWriteLock)
{
if (!ValidateUniqueKeysStream(jObj, pk, excludeItemId: id))
return CreateResponseMessage(HttpStatusCode.Conflict);
return CreateResponseMessage(HttpStatusCode.Conflict,
reason: $"{MessageEntityAlreadyExists} id = {id}");
etag = GenerateETag();
_etags[key] = etag;
_timestamps[key] = DateTimeOffset.UtcNow;
Expand All @@ -1448,7 +1466,8 @@ public override async Task<ResponseMessage> ReplaceItemStreamAsync(
else
{
if (!ValidateUniqueKeysStream(jObj, pk, excludeItemId: id))
return CreateResponseMessage(HttpStatusCode.Conflict);
return CreateResponseMessage(HttpStatusCode.Conflict,
reason: $"{MessageEntityAlreadyExists} id = {id}");
etag = GenerateETag();
_etags[key] = etag;
_timestamps[key] = DateTimeOffset.UtcNow;
Expand Down Expand Up @@ -1494,12 +1513,14 @@ public override async Task<ResponseMessage> DeleteItemStreamAsync(
if (!_items.TryGetValue(key, out var existingJson) || IsExpired(key))
{
EvictIfExpired(key);
return CreateResponseMessage(HttpStatusCode.NotFound);
return CreateResponseMessage(HttpStatusCode.NotFound,
reason: $"{MessageEntityDoesNotExist} id = {id}");
}

if (!CheckIfMatchStream(requestOptions, key))
{
return CreateResponseMessage(HttpStatusCode.PreconditionFailed);
return CreateResponseMessage(HttpStatusCode.PreconditionFailed,
reason: MessagePreconditionFailed);
}

ExecutePreTriggers(requestOptions, JsonParseHelpers.ParseJson(existingJson), "Delete");
Expand Down Expand Up @@ -1582,12 +1603,14 @@ private ResponseMessage PatchItemStreamCore(
if (!_items.TryGetValue(key, out var existingJson) || IsExpired(key))
{
EvictIfExpired(key);
return CreateResponseMessage(HttpStatusCode.NotFound);
return CreateResponseMessage(HttpStatusCode.NotFound,
reason: $"{MessageEntityDoesNotExist} id = {id}");
}

if (!CheckIfMatchStream(requestOptions, key))
{
return CreateResponseMessage(HttpStatusCode.PreconditionFailed);
return CreateResponseMessage(HttpStatusCode.PreconditionFailed,
reason: MessagePreconditionFailed);
}

var jObj = JsonParseHelpers.ParseJson(existingJson);
Expand All @@ -1602,7 +1625,8 @@ private ResponseMessage PatchItemStreamCore(
new Dictionary<string, object>(), null);
if (!matches)
{
return CreateResponseMessage(HttpStatusCode.PreconditionFailed);
return CreateResponseMessage(HttpStatusCode.PreconditionFailed,
reason: MessagePreconditionFailed);
}
}
}
Expand All @@ -1625,7 +1649,8 @@ private ResponseMessage PatchItemStreamCore(
lock (_uniqueKeyWriteLock)
{
if (!ValidateUniqueKeysStream(jObj, pk, excludeItemId: id))
return CreateResponseMessage(HttpStatusCode.Conflict);
return CreateResponseMessage(HttpStatusCode.Conflict,
reason: $"{MessageEntityAlreadyExists} id = {id}");
etag = GenerateETag();
_etags[key] = etag;
_timestamps[key] = DateTimeOffset.UtcNow;
Expand Down Expand Up @@ -3094,7 +3119,7 @@ private void CheckIfMatch(ItemRequestOptions requestOptions, (string Id, string

if (requestOptions.IfMatchEtag != currentEtag)
{
throw new InMemoryCosmosException("Precondition Failed", HttpStatusCode.PreconditionFailed, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
throw new InMemoryCosmosException(MessagePreconditionFailed, HttpStatusCode.PreconditionFailed, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}
}

Expand Down Expand Up @@ -3129,12 +3154,12 @@ private void CheckIfNoneMatchForWrite(ItemRequestOptions requestOptions, (string

if (requestOptions.IfNoneMatchEtag == "*" && _etags.ContainsKey(key))
{
throw new InMemoryCosmosException("Precondition Failed", HttpStatusCode.PreconditionFailed, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
throw new InMemoryCosmosException(MessagePreconditionFailed, HttpStatusCode.PreconditionFailed, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}

if (_etags.TryGetValue(key, out var currentEtag) && requestOptions.IfNoneMatchEtag == currentEtag)
{
throw new InMemoryCosmosException("Precondition Failed", HttpStatusCode.PreconditionFailed, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
throw new InMemoryCosmosException(MessagePreconditionFailed, HttpStatusCode.PreconditionFailed, 0, Guid.NewGuid().ToString(), SyntheticRequestCharge);
}
}

Expand Down Expand Up @@ -3245,14 +3270,23 @@ public InMemoryItemResponse(
public override string ETag => _etag;
}

private ResponseMessage CreateResponseMessage(HttpStatusCode statusCode, string json = null, string etag = null, int subStatusCode = 0)
private ResponseMessage CreateResponseMessage(HttpStatusCode statusCode, string json = null, string etag = null, int subStatusCode = 0, string reason = null)
{
var errorMessage = (int)statusCode >= 400
? $"Response status code does not indicate success: {statusCode} ({(int)statusCode})"
: null;

// When a specific error reason is provided, embed it as a JSON body so the Cosmos SDK
// can extract it into CosmosException.Message when this response flows through the
// FakeCosmosHandler HTTP pipeline (ConvertToHttpResponse → SDK parses response body).
// Ref: https://learn.microsoft.com/en-us/rest/api/cosmos-db/ — error response body format.
var contentJson = json ?? (reason is not null
? new JObject { ["message"] = reason }.ToString(Formatting.None)
: null);

var msg = new ResponseMessage(statusCode, requestMessage: null, errorMessage: errorMessage)
{
Content = json is not null ? ToStream(json) : null
Content = contentJson is not null ? ToStream(contentJson) : null
};
msg.Headers["x-ms-activity-id"] = Guid.NewGuid().ToString();
msg.Headers["x-ms-request-charge"] = SyntheticRequestCharge.ToString(CultureInfo.InvariantCulture);
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- Shared NuGet package metadata for all source (packable) projects -->
<PropertyGroup>
<IsPackable>true</IsPackable>
<Version>4.0.5</Version>
<Version>4.0.6</Version>
<Authors>lemonlion</Authors>
<Copyright>Copyright (c) 2026 lemonlion</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Loading