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

Diagnostics: Including header size details for BadRequest with large headers #1075

Merged
merged 8 commits into from
Dec 6, 2019
21 changes: 20 additions & 1 deletion Microsoft.Azure.Cosmos/src/GatewayStoreClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.Azure.Cosmos
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
Expand Down Expand Up @@ -178,9 +179,27 @@ internal static async Task<DocumentClientException> CreateDocumentClientExceptio
}
else
{
StringBuilder context = new StringBuilder();
context.Append(await responseMessage.Content.ReadAsStringAsync());
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved

HttpRequestMessage requestMessage = responseMessage.RequestMessage;
if (requestMessage != null)
{
context.Append($"RequestUri: {requestMessage.RequestUri.ToString()};");
j82w marked this conversation as resolved.
Show resolved Hide resolved
context.Append($"RequestMethod: {requestMessage.Method.Method};");

if (requestMessage.Headers != null)
{
foreach (KeyValuePair<string, IEnumerable<string>> header in requestMessage.Headers)
{
context.Append($"Header: {header.Key} Length: {string.Join(",", header.Value).Length};");
}
}
}

String message = await responseMessage.Content.ReadAsStringAsync();
return new DocumentClientException(
message: message,
message: context.ToString(),
innerException: null,
responseHeaders: responseMessage.Headers,
statusCode: responseMessage.StatusCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -300,6 +301,47 @@ await Assert.ThrowsExceptionAsync<HttpRequestException>(async () => {
DatabaseResponse databaseResponse = await cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());
});
}

[TestMethod]
[Owner("jawilley")]
public async Task TestLargeSessionTokenExceptionInGateway()
{
await TestCommon.DeleteAllDatabasesAsync();

try
{
string databaseName = "db";
string collectionName = "coll";

// Create a session token that is to large for the gateway to handle
string bigSession = "ABCDEF".PadLeft(100 * 1024, 'Z');
ItemRequestOptions options = new ItemRequestOptions()
{
SessionToken = bigSession
};

try
{
CosmosClient client = TestCommon.CreateCosmosClient(true);
Azure.Cosmos.Database db = await client.CreateDatabaseAsync(databaseName);
Container container = await db.CreateContainerAsync(collectionName, "/id");

string id = Guid.NewGuid().ToString();
ResponseMessage rm = await container.ReadItemStreamAsync(id, new Azure.Cosmos.PartitionKey(id), options);

Trace.TraceInformation(rm.StatusCode.ToString());
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved
}
catch (DocumentClientException dce)
{
string message = dce.Message;
Assert.IsTrue(message.Contains("The size of the request headers is too long."));
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved
}
}
finally
{
await TestCommon.DeleteAllDatabasesAsync();
}
}
}

internal static class StringHelper
Expand Down
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added



### Fixed

- [#1075](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1075) Including header size details for BadRequest with large headers



## 3.5.0 - TBD

Expand Down