Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FeedResponse<T> status code now return OK for success instead of the invalid status code 0 or Accepted #626

Merged
merged 8 commits into from
Aug 7, 2019
Merged
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 @@ -21,31 +21,6 @@ protected FeedResponse()
{
}

/// <summary>
/// Create a FeedResponse object with the default properties set
/// </summary>
/// <param name="httpStatusCode">The status code of the response</param>
/// <param name="headers">The headers of the response</param>
/// <param name="resource">The object from the response</param>
internal FeedResponse(
HttpStatusCode httpStatusCode,
Headers headers,
IEnumerable<T> resource)
{
this.StatusCode = httpStatusCode;
this.Headers = headers;
this.Resource = resource;
}

/// <inheritdoc/>
public override Headers Headers { get; }

/// <inheritdoc/>
public override IEnumerable<T> Resource { get; }

/// <inheritdoc/>
public override HttpStatusCode StatusCode { get; }

/// <inheritdoc/>
public override double RequestCharge => this.Headers?.RequestCharge ?? 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,8 @@ internal class QueryResponse<T> : FeedResponse<T>
private readonly CosmosSerializationOptions serializationOptions;
private IEnumerable<T> resources;

/// <summary>
/// Create a <see cref="QueryResponse{T}"/>
/// </summary>
private QueryResponse(
HttpStatusCode httpStatusCode,
IEnumerable<CosmosElement> cosmosElements,
CosmosQueryResponseMessageHeaders responseMessageHeaders,
CosmosSerializer jsonSerializer,
Expand All @@ -153,46 +151,26 @@ private QueryResponse(
this.QueryHeaders = responseMessageHeaders;
this.jsonSerializer = jsonSerializer;
this.serializationOptions = serializationOptions;
this.StatusCode = httpStatusCode;
}

/// <summary>
/// Gets the continuation token
/// </summary>
public override string ContinuationToken => this.Headers.ContinuationToken;

/// <summary>
/// Gets the request charge for this request from the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The request charge measured in request units.
/// </value>
public override double RequestCharge => this.Headers.RequestCharge;

/// <summary>
/// The headers of the response
/// </summary>
public override Headers Headers => this.QueryHeaders;

/// <summary>
/// The number of items in the stream.
/// </summary>
public override HttpStatusCode StatusCode { get; }

public override int Count => this.cosmosElements?.Count() ?? 0;

internal CosmosQueryResponseMessageHeaders QueryHeaders { get; }

/// <summary>
/// Get the enumerators to iterate through the results
/// </summary>
/// <returns>An enumerator of the response objects</returns>
public override IEnumerator<T> GetEnumerator()
{
return this.Resource.GetEnumerator();
}

/// <summary>
/// Get the enumerators to iterate through the results
/// </summary>
/// <returns>An enumerator of the response objects</returns>
public override IEnumerable<T> Resource
{
get
Expand Down Expand Up @@ -235,6 +213,7 @@ internal static QueryResponse<TInput> CreateResponse<TInput>(
{
cosmosQueryResponse.EnsureSuccessStatusCode();
queryResponse = new QueryResponse<TInput>(
httpStatusCode: cosmosQueryResponse.StatusCode,
cosmosElements: cosmosQueryResponse.CosmosElements,
responseMessageHeaders: cosmosQueryResponse.QueryHeaders,
jsonSerializer: jsonSerializer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ namespace Microsoft.Azure.Cosmos
internal class ReadFeedResponse<T> : FeedResponse<T>
{
protected ReadFeedResponse(
HttpStatusCode httpStatusCode,
ICollection<T> resource,
Headers responseMessageHeaders)
: base(
httpStatusCode: HttpStatusCode.Accepted,
headers: responseMessageHeaders,
resource: resource)
{
this.Count = resource.Count;
this.Headers = responseMessageHeaders;
this.Resource = resource;
this.StatusCode = httpStatusCode;
}

public override int Count { get; }

public override string ContinuationToken => this.Headers?.ContinuationToken;

public override Headers Headers { get; }

public override IEnumerable<T> Resource { get; }

public override HttpStatusCode StatusCode { get; }

public override IEnumerator<T> GetEnumerator()
{
return this.Resource.GetEnumerator();
Expand All @@ -43,23 +49,12 @@ internal static ReadFeedResponse<TInput> CreateResponse<TInput>(
}

ReadFeedResponse<TInput> readFeedResponse = new ReadFeedResponse<TInput>(
httpStatusCode: responseMessage.StatusCode,
resource: resources,
responseMessageHeaders: responseMessage.Headers);

return readFeedResponse;
}
}

internal static ReadFeedResponse<TInput> CreateResponse<TInput>(
Headers responseMessageHeaders,
ICollection<TInput> resources,
bool hasMoreResults)
{
ReadFeedResponse<TInput> readFeedResponse = new ReadFeedResponse<TInput>(
resource: resources,
responseMessageHeaders: responseMessageHeaders);

return readFeedResponse;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Cosmos.Scripts;
using Microsoft.Azure.Cosmos.Linq;
Expand Down Expand Up @@ -409,12 +410,14 @@ private async Task<List<T>> ToListAsync<T>(
string queryText,
QueryRequestOptions requestOptions)
{
HttpStatusCode expectedStatus = HttpStatusCode.OK;
FeedIterator feedStreamIterator = createStreamQuery(queryText, null, requestOptions);
List<T> streamResults = new List<T>();
while (feedStreamIterator.HasMoreResults)
{
ResponseMessage response = await feedStreamIterator.ReadNextAsync();
response.EnsureSuccessStatusCode();
Assert.AreEqual(expectedStatus, response.StatusCode);

StreamReader sr = new StreamReader(response.Content);
string result = await sr.ReadToEndAsync();
Expand All @@ -431,6 +434,7 @@ private async Task<List<T>> ToListAsync<T>(
FeedIterator pagedFeedIterator = createStreamQuery(queryText, continuationToken, requestOptions);
ResponseMessage response = await pagedFeedIterator.ReadNextAsync();
response.EnsureSuccessStatusCode();
Assert.AreEqual(expectedStatus, response.StatusCode);

ICollection<T> responseResults = TestCommon.Serializer.FromStream<CosmosFeedResponseUtil<T>>(response.Content).Data;
Assert.IsTrue(responseResults.Count <= 1);
Expand All @@ -451,23 +455,25 @@ private async Task<List<T>> ToListAsync<T>(
List<T> results = new List<T>();
while (feedIterator.HasMoreResults)
{
FeedResponse<T> iterator = await feedIterator.ReadNextAsync();
Assert.IsTrue(iterator.Count <= 1);
Assert.IsTrue(iterator.Resource.Count() <= 1);
FeedResponse<T> response = await feedIterator.ReadNextAsync();
Assert.AreEqual(expectedStatus, response.StatusCode);
Assert.IsTrue(response.Count <= 1);
Assert.IsTrue(response.Resource.Count() <= 1);

results.AddRange(iterator);
results.AddRange(response);
}

continuationToken = null;
List<T> pagedResults = new List<T>();
do
{
FeedIterator<T> pagedFeedIterator = createQuery(queryText, continuationToken, requestOptions);
FeedResponse<T> iterator = await pagedFeedIterator.ReadNextAsync();
Assert.IsTrue(iterator.Count <= 1);
Assert.IsTrue(iterator.Resource.Count() <= 1);
pagedResults.AddRange(iterator);
continuationToken = iterator.ContinuationToken;
FeedResponse<T> response = await pagedFeedIterator.ReadNextAsync();
Assert.AreEqual(expectedStatus, response.StatusCode);
Assert.IsTrue(response.Count <= 1);
Assert.IsTrue(response.Resource.Count() <= 1);
pagedResults.AddRange(response);
continuationToken = response.ContinuationToken;
} while (continuationToken != null);

Assert.AreEqual(pagedResults.Count, results.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ private static Container GetMockedContainer(string containerName = "myColl")
Headers headers = new Headers();
headers.ContinuationToken = string.Empty;

Mock<FeedResponse<DocumentServiceLeaseCore>> mockFeedResponse = new Mock<FeedResponse<DocumentServiceLeaseCore>>();
mockFeedResponse.Setup(x => x.ContinuationToken).Returns(string.Empty);
mockFeedResponse.Setup(x => x.Headers).Returns(headers);
mockFeedResponse.Setup(x => x.Resource).Returns(DocumentServiceLeaseContainerCosmosTests.allLeases);
mockFeedResponse.Setup(x => x.Headers).Returns(headers);
mockFeedResponse.Setup(x => x.GetEnumerator()).Returns(DocumentServiceLeaseContainerCosmosTests.allLeases.GetEnumerator());

Mock<FeedIterator<DocumentServiceLeaseCore>> mockedQuery = new Mock<FeedIterator<DocumentServiceLeaseCore>>();
mockedQuery.Setup(q => q.ReadNextAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(() => ReadFeedResponse<DocumentServiceLeaseCore>.CreateResponse(
responseMessageHeaders: headers,
resources: DocumentServiceLeaseContainerCosmosTests.allLeases,
hasMoreResults: false));
.ReturnsAsync(() => mockFeedResponse.Object);
mockedQuery.SetupSequence(q => q.HasMoreResults)
.Returns(true)
.Returns(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1968,47 +1968,11 @@
"Attributes": [],
"MethodInfo": "Int32 get_Count()"
},
"Microsoft.Azure.Cosmos.Headers get_Headers()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "Microsoft.Azure.Cosmos.Headers get_Headers()"
},
"Microsoft.Azure.Cosmos.Headers Headers": {
"Type": "Property",
"Attributes": [],
"MethodInfo": null
},
"System.Collections.Generic.IEnumerable`1[T] get_Resource()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "System.Collections.Generic.IEnumerable`1[T] get_Resource()"
},
"System.Collections.Generic.IEnumerable`1[T] Resource": {
"Type": "Property",
"Attributes": [],
"MethodInfo": null
},
"System.Collections.Generic.IEnumerator`1[T] GetEnumerator()": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Collections.Generic.IEnumerator`1[T] GetEnumerator()"
},
"System.Net.HttpStatusCode get_StatusCode()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "System.Net.HttpStatusCode get_StatusCode()"
},
"System.Net.HttpStatusCode StatusCode": {
"Type": "Property",
"Attributes": [],
"MethodInfo": null
},
"System.String ActivityId": {
"Type": "Property",
"Attributes": [],
Expand Down
Loading