Skip to content

Commit

Permalink
Issue #447 Default behaviour change for batch error handling in 5.7.0…
Browse files Browse the repository at this point in the history
…-beta
  • Loading branch information
VikingsFan committed Aug 5, 2015
1 parent 2c0221a commit 9cbe805
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
Expand Up @@ -32,6 +32,8 @@ public static class HttpConfigurationExtensions

private const string ResolverSettingsKey = "System.Web.OData.ResolverSettingsKey";

private const string ContinueOnErrorKey = "System.Web.OData.ContinueOnErrorKey";

/// <summary>
/// Enables query support for actions with an <see cref="IQueryable" /> or <see cref="IQueryable{T}" /> return
/// type. To avoid processing unexpected or malicious queries, use the validation settings on
Expand Down Expand Up @@ -232,6 +234,38 @@ public static void EnableAlternateKeys(this HttpConfiguration configuration, boo
settings.AlternateKeys = alternateKeys;
}

/// <summary>
/// Enable the continue-on-error header.
/// </summary>
public static void EnableContinueOnErrorHeader(this HttpConfiguration configuration)
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}

configuration.Properties[ContinueOnErrorKey] = true;
}

/// <summary>
/// Check the continue-on-error header is enable or not.
/// </summary>
/// <returns></returns>
internal static bool HasEnabledContinueOnErrorHeader(this HttpConfiguration configuration)
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}

object value;
if (configuration.Properties.TryGetValue(ContinueOnErrorKey, out value))
{
return (bool)value;
}
return false;
}

internal static ODataUriResolverSetttings GetResolverSettings(this HttpConfiguration configuration)
{
if (configuration == null)
Expand Down
Expand Up @@ -8,6 +8,8 @@
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Batch;
using System.Web.OData.Extensions;
using System.Web.OData.Properties;
using Microsoft.OData.Core;

namespace System.Web.OData.Batch
Expand Down Expand Up @@ -42,7 +44,7 @@ public override async Task<HttpResponseMessage> ProcessBatchAsync(HttpRequestMes
IList<ODataBatchRequestItem> subRequests = await ParseBatchRequestsAsync(request, cancellationToken);

string preferHeader = RequestPreferenceHelpers.GetRequestPreferHeader(request);
if (preferHeader != null && preferHeader.Contains(PreferenceContinueOnError))
if ((preferHeader != null && preferHeader.Contains(PreferenceContinueOnError)) || (!request.GetConfiguration().HasEnabledContinueOnErrorHeader()))
{
ContinueOnError = true;
}
Expand Down
Expand Up @@ -7,6 +7,8 @@
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Batch;
using System.Web.OData.Extensions;
using System.Web.OData.Properties;
using Microsoft.OData.Core;

namespace System.Web.OData.Batch
Expand Down Expand Up @@ -49,9 +51,9 @@ public override async Task<HttpResponseMessage> ProcessBatchAsync(HttpRequestMes
List<ODataBatchResponseItem> responses = new List<ODataBatchResponseItem>();
Guid batchId = Guid.NewGuid();
List<IDisposable> resourcesToDispose = new List<IDisposable>();

string preferHeader = RequestPreferenceHelpers.GetRequestPreferHeader(request);
if (preferHeader != null && preferHeader.Contains(PreferenceContinueOnError))
if ((preferHeader != null && preferHeader.Contains(PreferenceContinueOnError)) || (!request.GetConfiguration().HasEnabledContinueOnErrorHeader()))
{
ContinueOnError = true;
}
Expand Down
Expand Up @@ -242,6 +242,19 @@ public void EnableAlternateKeys_Sets_AlternateKeyFlag()
Assert.True(resolverSetttings.AlternateKeys);
}

[Fact]
public void EnableAlternateKeys_Sets_ContinueOnErrorKeyFlag()
{
// Arrange
HttpConfiguration config = new HttpConfiguration();

// Act
config.EnableContinueOnErrorHeader();

// Assert
Assert.True(config.HasEnabledContinueOnErrorHeader());
}

private static ODataMediaTypeFormatter CreateODataFormatter()
{
return new ODataMediaTypeFormatter(new ODataPayloadKind[0]);
Expand Down
Expand Up @@ -10,6 +10,7 @@
using System.Web.Http;
using System.Web.Http.Routing;
using System.Web.OData.Batch;
using System.Web.OData.Extensions;
using Microsoft.TestCommon;

namespace System.Web.OData.Test
Expand Down Expand Up @@ -79,6 +80,7 @@ public void ProcessBatchAsync_Throws_IfRequestIsNull()
[Fact]
public void ProcessBatchAsync_CallsRegisterForDispose()
{
// Arrange
List<IDisposable> expectedResourcesForDisposal = new List<IDisposable>();
MockHttpServer server = new MockHttpServer(request =>
{
Expand All @@ -95,18 +97,23 @@ public void ProcessBatchAsync_CallsRegisterForDispose()
ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/"))
}
};
batchRequest.SetConfiguration(new HttpConfiguration());

// Act
var response = batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None).Result;
var resourcesForDisposal = batchRequest.GetResourcesForDisposal();

// Assert
foreach (var expectedResource in expectedResourcesForDisposal)
{
Assert.Contains(expectedResource, resourcesForDisposal);
}
}

[Fact]
public void ProcessBatchAsync_ContinueOnError()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ProcessBatchAsync_ContinueOnError(bool enableContinueOnError)
{
// Arrange
MockHttpServer server = new MockHttpServer(request =>
Expand Down Expand Up @@ -147,6 +154,9 @@ public void ProcessBatchAsync_ContinueOnError()
}),
}
};
var enableContinueOnErrorconfig = new HttpConfiguration();
enableContinueOnErrorconfig.EnableContinueOnErrorHeader();
batchRequest.SetConfiguration(enableContinueOnErrorconfig);
HttpRequestMessage batchRequestWithPrefContinueOnError = new HttpRequestMessage(HttpMethod.Post, "http://example.com/$batch")
{
Content = new MultipartContent("mixed")
Expand All @@ -165,7 +175,15 @@ public void ProcessBatchAsync_ContinueOnError()
}),
}
};
batchRequestWithPrefContinueOnError.Headers.Add("prefer", "odata.continue-on-error");
if (enableContinueOnError)
{
batchRequestWithPrefContinueOnError.SetConfiguration(enableContinueOnErrorconfig);
batchRequestWithPrefContinueOnError.Headers.Add("prefer", "odata.continue-on-error");
}
else
{
batchRequestWithPrefContinueOnError.SetConfiguration(new HttpConfiguration());
}

// Act
var response = batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None).Result;
Expand Down
Expand Up @@ -12,6 +12,7 @@
using System.Web.Http;
using System.Web.Http.Routing;
using System.Web.OData.Batch;
using System.Web.OData.Extensions;
using Microsoft.OData.Core;
using Microsoft.TestCommon;

Expand Down Expand Up @@ -58,6 +59,7 @@ public void ProcessBatchAsync_Throws_IfRequestIsNull()
[Fact]
public void ProcessBatchAsync_CallsRegisterForDispose()
{
// Arrange
List<IDisposable> expectedResourcesForDisposal = new List<IDisposable>();
MockHttpServer server = new MockHttpServer(request =>
{
Expand All @@ -78,10 +80,13 @@ public void ProcessBatchAsync_CallsRegisterForDispose()
}
}
};
batchRequest.SetConfiguration(new HttpConfiguration());

// Act
var response = batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None).Result;
var resourcesForDisposal = batchRequest.GetResourcesForDisposal();

// Assert
foreach (var expectedResource in expectedResourcesForDisposal)
{
Assert.Contains(expectedResource, resourcesForDisposal);
Expand All @@ -91,6 +96,7 @@ public void ProcessBatchAsync_CallsRegisterForDispose()
[Fact]
public void ProcessBatchAsync_CallsInvokerForEachRequest()
{
// Arrange
MockHttpServer server = new MockHttpServer(request =>
{
string responseContent = request.RequestUri.AbsoluteUri;
Expand Down Expand Up @@ -119,9 +125,12 @@ public void ProcessBatchAsync_CallsInvokerForEachRequest()
}
}
};
batchRequest.SetConfiguration(new HttpConfiguration());

// Act
var response = batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None).Result;

// Assert
var batchContent = Assert.IsType<ODataBatchContent>(response.Content);
var batchResponses = batchContent.Responses.ToArray();
Assert.Equal(2, batchResponses.Length);
Expand All @@ -132,6 +141,7 @@ public void ProcessBatchAsync_CallsInvokerForEachRequest()
[Fact]
public void ProcessBatchAsync_DisposesResponseInCaseOfException()
{
// Arrange
List<MockHttpResponseMessage> responses = new List<MockHttpResponseMessage>();
MockHttpServer server = new MockHttpServer(request =>
{
Expand All @@ -156,7 +166,9 @@ public void ProcessBatchAsync_DisposesResponseInCaseOfException()
}
}
};
batchRequest.SetConfiguration(new HttpConfiguration());

// Act & Assert
Assert.Throws<InvalidOperationException>(
() => batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None).Result);

Expand All @@ -167,8 +179,10 @@ public void ProcessBatchAsync_DisposesResponseInCaseOfException()
}
}

[Fact]
public void ProcessBatchAsync_ContinueOnError()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ProcessBatchAsync_ContinueOnError(bool enableContinueOnError)
{
// Arrange
MockHttpServer server = new MockHttpServer(request =>
Expand Down Expand Up @@ -209,6 +223,9 @@ public void ProcessBatchAsync_ContinueOnError()
}),
}
};
var enableContinueOnErrorconfig = new HttpConfiguration();
enableContinueOnErrorconfig.EnableContinueOnErrorHeader();
batchRequest.SetConfiguration(enableContinueOnErrorconfig);
HttpRequestMessage batchRequestWithPrefContinueOnError = new HttpRequestMessage(HttpMethod.Post, "http://example.com/$batch")
{
Content = new MultipartContent("mixed")
Expand All @@ -227,7 +244,15 @@ public void ProcessBatchAsync_ContinueOnError()
}),
}
};
batchRequestWithPrefContinueOnError.Headers.Add("prefer", "odata.continue-on-error");
if (enableContinueOnError)
{
batchRequestWithPrefContinueOnError.SetConfiguration(enableContinueOnErrorconfig);
batchRequestWithPrefContinueOnError.Headers.Add("prefer", "odata.continue-on-error");
}
else
{
batchRequestWithPrefContinueOnError.SetConfiguration(new HttpConfiguration());
}

// Act
var response = batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None).Result;
Expand Down

0 comments on commit 9cbe805

Please sign in to comment.