Skip to content

Commit

Permalink
Added test to verify the TransactionId HeadDocumentHeader value.
Browse files Browse the repository at this point in the history
Moved the TransactionId header string to a constant class.

fix ArangoDB-Community#322
  • Loading branch information
DiscJockeyDJ committed Sep 13, 2021
1 parent 1922279 commit 5b73da6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
5 changes: 2 additions & 3 deletions arangodb-net-standard.Test/CursorApi/CursorApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ public async Task PostCursorAsync_ShouldUseHeaderProperties()
return Task.FromResult(mockResponse.Object);
});

string transactionHeaderKey = "x-arango-trx-id";
string dummyTransactionId = "dummy transaction Id";

// Call the method to create the cursor.
Expand All @@ -264,8 +263,8 @@ await apiClient.PostCursorAsync<MyModel>(

// Check that the header and values are there.
Assert.NotNull(requestHeader);
Assert.Contains(transactionHeaderKey, requestHeader.AllKeys);
Assert.Equal(dummyTransactionId, requestHeader.Get(transactionHeaderKey));
Assert.Contains(CustomHttpHeaders.StreamTransactionHeader, requestHeader.AllKeys);
Assert.Equal(dummyTransactionId, requestHeader.Get(CustomHttpHeaders.StreamTransactionHeader));
}

[Fact]
Expand Down
50 changes: 50 additions & 0 deletions arangodb-net-standard.Test/DocumentApi/DocumentApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ArangoDBNetStandard;
using ArangoDBNetStandard.DocumentApi;
using ArangoDBNetStandard.DocumentApi.Models;
using ArangoDBNetStandard.TransactionApi.Models;
using ArangoDBNetStandard.Transport;
using ArangoDBNetStandardTest.DocumentApi.Models;
using Moq;
Expand Down Expand Up @@ -1086,6 +1087,55 @@ public async Task ReadDocumentHeaderAsync_ShouldReturnPreconditionFailed_WhenIfM
Assert.NotEqual($"\"{docResponse._rev}\"", response.Etag.Tag);
}

[Fact]
public async Task ReadDocumentHeaderAsync_ShouldReturnOk_WhenTransactionIdIsGivenAndIsTheSame()
{
// Post a single document.
var docResponse =
await _docClient.PostDocumentAsync(_testCollection, new Dictionary<string, object> { ["key"] = "value" });

// Begin a transaction.
var beginTransaction = await _adb.Transaction.BeginTransaction(
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { _testCollection }
}
});

// Get the header fields.
var response = await _docClient.HeadDocumentAsync(
_testCollection,
docResponse._key,
new HeadDocumentHeader { TransactionId = beginTransaction.Result.Id });

// Check for the expected status.
Assert.Equal(HttpStatusCode.OK, response.Code);

// Abort the transaction.
await _adb.Transaction.AbortTransaction(beginTransaction.Result.Id);
}

[Fact]
public async Task ReadDocumentHeaderAsync_ShouldReturnNotFound_WhenTransctionIdIsGiveAndIsNotTheSame()
{
string dummyTransactionId = "Bogus transaction Id";

// Post a single document.
var docResponse =
await _docClient.PostDocumentAsync(_testCollection, new Dictionary<string, object> { ["key"] = "value" });

// Get the header fields.
var response = await _docClient.HeadDocumentAsync(
_testCollection,
docResponse._key,
new HeadDocumentHeader { TransactionId = dummyTransactionId });

// Check for the expected status.
Assert.Equal(HttpStatusCode.BadRequest, response.Code);
}

[Fact]
public async Task ReadDocumentHeaderAsync_ShouldReturnNotFound_WhenCollectionDoesNotExist()
{
Expand Down
2 changes: 1 addition & 1 deletion arangodb-net-standard/CursorApi/CursorApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected virtual WebHeaderCollection GetHeaderCollection(CursorHeaderProperties
{
if (!string.IsNullOrWhiteSpace(headerProperties.TransactionId))
{
headerCollection.Add("x-arango-trx-id", headerProperties.TransactionId);
headerCollection.Add(CustomHttpHeaders.StreamTransactionHeader, headerProperties.TransactionId);
}
}

Expand Down
13 changes: 13 additions & 0 deletions arangodb-net-standard/CustomHttpHeaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ArangoDBNetStandard
{
/// <summary>
/// The custom HttpHeaders that may be specified in a client request.
/// </summary>
public static class CustomHttpHeaders
{
/// <summary>
/// The header string used for Stream Transaction.
/// </summary>
public const string StreamTransactionHeader = "x-arango-trx-id";
}
}
2 changes: 2 additions & 0 deletions arangodb-net-standard/DocumentApi/DocumentApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ public virtual async Task<PatchDocumentResponse<U>> PatchDocumentAsync<T, U>(
/// <remarks>
/// 200: is returned if the document was found.
/// 304: is returned if the “If-None-Match” header is given and the document has the same version.
/// 400: is returned if the "TransactionId" header is given and the transactionId does not exist.
/// 404: is returned if the document or collection was not found.
/// 412: is returned if an “If-Match” header is given and the found document has a different version. The response will also contain the found document’s current revision in the Etag header.
/// </remarks>
Expand All @@ -692,6 +693,7 @@ public virtual async Task<HeadDocumentResponse> HeadDocumentAsync(
/// <remarks>
/// 200: is returned if the document was found.
/// 304: is returned if the “If-None-Match” header is given and the document has the same version.
/// 400: is returned if the "TransactionId" header is given and the transactionId does not exist.
/// 404: is returned if the document or collection was not found.
/// 412: is returned if an “If-Match” header is given and the found document has a different version. The response will also contain the found document’s current revision in the Etag header.
/// </remarks>
Expand Down
2 changes: 2 additions & 0 deletions arangodb-net-standard/DocumentApi/IDocumentApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ Task<PatchDocumentResponse<U>> PatchDocumentAsync<T, U>(
/// <remarks>
/// 200: is returned if the document was found.
/// 304: is returned if the “If-None-Match” header is given and the document has the same version.
/// 400: is returned if the "TransactionId" header is given and the transactionId does not exist.
/// 404: is returned if the document or collection was not found.
/// 412: is returned if an “If-Match” header is given and the found document has a different version. The response will also contain the found document’s current revision in the Etag header.
/// </remarks>
Expand All @@ -381,6 +382,7 @@ Task<HeadDocumentResponse> HeadDocumentAsync(
/// <remarks>
/// 200: is returned if the document was found.
/// 304: is returned if the “If-None-Match” header is given and the document has the same version.
/// 400: is returned if the "TransactionId" header is given and the transactionId does not exist.
/// 404: is returned if the document or collection was not found.
/// 412: is returned if an “If-Match” header is given and the found document has a different version. The response will also contain the found document’s current revision in the Etag header.
/// </remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public WebHeaderCollection ToWebHeaderCollection()

if (TransactionId != null)
{
collection.Add("x-arango-trx-id", TransactionId);
collection.Add(CustomHttpHeaders.StreamTransactionHeader, TransactionId);
}

return collection;
Expand Down

0 comments on commit 5b73da6

Please sign in to comment.