Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,37 @@ await SetupAndRunRestApiTest(
);
}

/// <summary>
/// Tests that a PATCH request with an invalid If-Match header value
/// (anything other than "*") returns a 400 Bad Request response
/// because ETags are not supported.
/// </summary>
[TestMethod]
public virtual async Task PatchOne_Update_InvalidIfMatchHeader_Returns400_Test()
{
Dictionary<string, StringValues> headerDictionary = new();
headerDictionary.Add("If-Match", "\"abc123\"");
string requestBody = @"
{
""title"": ""The Hobbit Returns to The Shire"",
""publisher_id"": 1234
}";

await SetupAndRunRestApiTest(
primaryKeyRoute: "id/1",
queryString: null,
entityNameOrPath: _integrationEntityName,
sqlQuery: string.Empty,
operationType: EntityActionOperation.UpsertIncremental,
headers: new HeaderDictionary(headerDictionary),
requestBody: requestBody,
exceptionExpected: true,
expectedErrorMessage: "Etags not supported, use '*'",
expectedStatusCode: HttpStatusCode.BadRequest,
expectedSubStatusCode: DataApiBuilderException.SubStatusCodes.BadRequest.ToString()
);
}

/// <summary>
/// Test to validate successful execution of PATCH operation which satisfies the database policy for the update operation it resolves into.
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions src/Service.Tests/SqlTests/RestApiTests/Put/PutApiTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,37 @@ await SetupAndRunRestApiTest(
);
}

/// <summary>
/// Tests that a PUT request with an invalid If-Match header value
/// (anything other than "*") returns a 400 Bad Request response
/// because ETags are not supported.
/// </summary>
[TestMethod]
public virtual async Task PutOne_Update_InvalidIfMatchHeader_Returns400_Test()
{
Dictionary<string, StringValues> headerDictionary = new();
headerDictionary.Add("If-Match", "\"abc123\"");
string requestBody = @"
{
""title"": ""The Return of the King"",
""publisher_id"": 1234
}";

await SetupAndRunRestApiTest(
primaryKeyRoute: "id/1",
queryString: null,
entityNameOrPath: _integrationEntityName,
sqlQuery: string.Empty,
operationType: EntityActionOperation.Upsert,
headers: new HeaderDictionary(headerDictionary),
requestBody: requestBody,
exceptionExpected: true,
expectedErrorMessage: "Etags not supported, use '*'",
expectedStatusCode: HttpStatusCode.BadRequest,
expectedSubStatusCode: DataApiBuilderException.SubStatusCodes.BadRequest.ToString()
);
}

/// <summary>
/// Tests that a PUT request with If-Match header (strict update semantics)
/// still requires a primary key route. When If-Match is present, the operation
Expand Down
9 changes: 7 additions & 2 deletions src/Service/Controllers/RestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public async Task<IActionResult> Upsert(
{
return await HandleOperation(
route,
DeterminePatchPutSemantics(EntityActionOperation.Upsert));
EntityActionOperation.Upsert);
}

/// <summary>
Expand All @@ -181,7 +181,7 @@ public async Task<IActionResult> UpsertIncremental(
{
return await HandleOperation(
route,
DeterminePatchPutSemantics(EntityActionOperation.UpsertIncremental));
EntityActionOperation.UpsertIncremental);
}

/// <summary>
Expand All @@ -206,6 +206,11 @@ private async Task<IActionResult> HandleOperation(
{
TelemetryMetricsHelper.IncrementActiveRequests(ApiType.REST);

if (operationType is EntityActionOperation.Upsert or EntityActionOperation.UpsertIncremental)
{
operationType = DeterminePatchPutSemantics(operationType);
}

if (activity is not null)
{
activity.TrackMainControllerActivityStarted(
Expand Down