Skip to content

Commit

Permalink
Expose additional Preference Headers
Browse files Browse the repository at this point in the history
Adds the odata.track-changes, odata.maxpagesize, and odata.continue-on-error preference headers.

Signed-off-by: Dong Liu <doliu@microsoft.com>
  • Loading branch information
JefWight authored and karataliu committed Mar 16, 2015
1 parent c2f996e commit a9321f3
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/Microsoft.OData.Core/ODataPreferenceHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ public sealed class ODataPreferenceHeader
/// </summary>
private const string WaitPreferenceTokenName = "wait";

/// <summary>
/// The odata.continue-on-error preference token.
/// </summary>
private const string ODataContinueOnErrorPreferenceToken = "odata.continue-on-error";

/// <summary>
/// The odata.maxpagesize=# preference token.
/// </summary>
private const string ODataMaxPageSizePreferenceToken = "odata.maxpagesize";

/// <summary>
/// The odata.track-changes preference token.
/// </summary>
private const string ODataTrackChangesPreferenceToken = "odata.track-changes";

/// <summary>
/// The Prefer header name.
/// </summary>
Expand All @@ -61,6 +76,11 @@ public sealed class ODataPreferenceHeader
/// </summary>
private static readonly KeyValuePair<string, string>[] EmptyParameters = new KeyValuePair<string, string>[0];

/// <summary>
/// The odata.continue-on-error preference.
/// </summary>
private static readonly HttpHeaderValueElement ContinueOnErrorPreference = new HttpHeaderValueElement(ODataContinueOnErrorPreferenceToken, null, EmptyParameters);

/// <summary>
/// The return=minimal preference.
/// </summary>
Expand All @@ -76,6 +96,11 @@ public sealed class ODataPreferenceHeader
/// </summary>
private static readonly HttpHeaderValueElement RespondAsyncPreference = new HttpHeaderValueElement(RespondAsyncPreferenceToken, null, EmptyParameters);

/// <summary>
/// The odata.track-changes preference.
/// </summary>
private static readonly HttpHeaderValueElement TrackChangesPreference = new HttpHeaderValueElement(ODataTrackChangesPreferenceToken, null, EmptyParameters);

/// <summary>
/// The message to set the preference header to and to get the preference header from.
/// </summary>
Expand Down Expand Up @@ -280,6 +305,98 @@ public int? Wait
}
}

/// <summary>
/// Property to get and set the "odata.continue-on-error" preference to the "Prefer" header on the underlying IODataRequestMessage or
/// the "Preference-Applied" header on the underlying IODataResponseMessage.
/// Setting true sets the "odata.continue-on-error" preference.
/// Setting false clears the "odata.continue-on-error" preference.
/// Returns true of the "odata.continue-on-error" preference is on the header. Otherwise returns false if the "odata.continue-on-error" is not on the header.
/// </summary>
public bool ContinueOnError
{
get
{
return this.Get(ODataContinueOnErrorPreferenceToken) != null;
}

set
{
if (value)
{
this.Set(ContinueOnErrorPreference);
}
else
{
this.Clear(ODataContinueOnErrorPreferenceToken);
}
}
}

/// <summary>
/// Property to get and set the "odata.maxpagesize" preference to the "Prefer" header on the underlying IODataRequestMessage or
/// the "Preference-Applied" header on the underlying IODataResponseMessage.
/// Setting N sets the "odata.maxpagesize=N" preference.
/// Setting null clears the "odata.maxpagesize" preference.
/// Returns N if the "odata.maxpagesize=N" preference is on the header.
/// Returning null indicates that "odata.maxpagesize" is not on the header.
/// </summary>
public int? MaxPageSize
{
get
{
var maxPageSizeHttpHeaderValueElement = this.Get(ODataMaxPageSizePreferenceToken);

// Should check maxPageSizeHttpHeaderValueElement.Value != null.
// Should do int.TryParse.
// If either of the above fail, should throw an ODataException for parsing, not a System.Exception (such as FormatException, etc.).
if (maxPageSizeHttpHeaderValueElement != null)
{
return int.Parse(maxPageSizeHttpHeaderValueElement.Value, CultureInfo.InvariantCulture);
}

return null;
}

set
{
if (value.HasValue)
{
this.Set(new HttpHeaderValueElement(ODataMaxPageSizePreferenceToken, string.Format(CultureInfo.InvariantCulture, "{0}", value.Value), EmptyParameters));
}
else
{
this.Clear(ODataMaxPageSizePreferenceToken);
}
}
}

/// <summary>
/// Property to get and set the "odata.track-changes" preference to the "Prefer" header on the underlying IODataRequestMessage or
/// the "Preference-Applied" header on the underlying IODataResponseMessage.
/// Setting true sets the "odata.track-changes" preference.
/// Setting false clears the "odata.track-changes" preference.
/// Returns true of the "odata.track-changes" preference is on the header. Otherwise returns false if the "odata.track-changes" is not on the header.
/// </summary>
public bool TrackChanges
{
get
{
return this.Get(ODataTrackChangesPreferenceToken) != null;
}

set
{
if (value)
{
this.Set(TrackChangesPreference);
}
else
{
this.Clear(ODataTrackChangesPreferenceToken);
}
}
}

/// <summary>
/// Dictionary of preferences in the header.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4765,8 +4765,11 @@ public sealed class Microsoft.OData.Core.ODataPayloadKindDetectionResult {

public sealed class Microsoft.OData.Core.ODataPreferenceHeader {
string AnnotationFilter { public get; public set; }
bool ContinueOnError { public get; public set; }
System.Nullable`1[[System.Int32]] MaxPageSize { public get; public set; }
bool RespondAsync { public get; public set; }
System.Nullable`1[[System.Boolean]] ReturnContent { public get; public set; }
bool TrackChanges { public get; public set; }
System.Nullable`1[[System.Int32]] Wait { public get; public set; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class ODataPreferenceHeaderTests
private const string RespondAyncPreference = "respond-async";
private const string RespondAsyncAndWaitPreference = "respond-async,wait=10";
private const string WaitPreference = "wait=10";
private const string MaxPageSizePreference = "odata.maxpagesize";
private const string TrackChangesPreference = "odata.track-changes";
private const string ContinueOnErrorPreference = "odata.continue-on-error";

[TestInitialize]
public void TestInit()
Expand Down Expand Up @@ -250,5 +253,64 @@ public void ReturnWaitOfBadIntergerFormatShouldThrow()
Action test = () => wait = this.preferHeader.Wait;
test.ShouldThrow<FormatException>().WithMessage("Input string was not in a correct format.");
}

[TestMethod]
public void SetContinueOnErrorToTrueShouldAppendHeader()
{
this.preferHeader.ContinueOnError = true;
this.preferHeader.ContinueOnError.Should().BeTrue();
this.requestMessage.GetHeader(PreferHeaderName).Should().Be(ContinueOnErrorPreference);
}

[TestMethod]
public void SetContinueOnErrorToFalseShouldClearHeader()
{
this.preferHeader.ContinueOnError = false;
this.preferHeader.ContinueOnError.Should().BeFalse();
this.requestMessage.GetHeader(PreferHeaderName).Should().BeNull();
}

[TestMethod]
public void SetMaxPageSizeShouldAppendHeader()
{
const int MaxPageSize = 10;
this.preferHeader.MaxPageSize = MaxPageSize;
this.preferHeader.MaxPageSize.Should().Be(MaxPageSize);
this.requestMessage.GetHeader(PreferHeaderName).Should().Be(string.Format("{0}={1}", MaxPageSizePreference, MaxPageSize));
}

[TestMethod]
public void SetMaxPageSizeToNullShouldClearHeader()
{
this.preferHeader.MaxPageSize = null;
this.preferHeader.MaxPageSize.Should().Be(null);
this.requestMessage.GetHeader(PreferHeaderName).Should().BeNull();
}

[TestMethod]
public void ReturnMaxPageSizeOfBadIntergerFormatShouldThrow()
{
this.requestMessage.SetHeader(PreferHeaderName, string.Format("{0}=abc", MaxPageSizePreference));
this.preferHeader = new ODataPreferenceHeader(this.requestMessage);
int? maxPageSize;
Action test = () => maxPageSize = this.preferHeader.MaxPageSize;
test.ShouldThrow<FormatException>().WithMessage("Input string was not in a correct format.");
}

[TestMethod]
public void SetTrackChangesToTrueShouldAppendHeader()
{
this.preferHeader.TrackChanges = true;
this.preferHeader.TrackChanges.Should().BeTrue();
this.requestMessage.GetHeader(PreferHeaderName).Should().Be(TrackChangesPreference);
}

[TestMethod]
public void SetTrackChangesToFalseShouldClearHeader()
{
this.preferHeader.TrackChanges = false;
this.preferHeader.TrackChanges.Should().BeFalse();
this.requestMessage.GetHeader(PreferHeaderName).Should().BeNull();
}
}
}

0 comments on commit a9321f3

Please sign in to comment.