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

Add Stream payload to ExecuteStoredProcedureStreamAsync #1061

Merged
merged 12 commits into from
Dec 12, 2019
Merged
Prev Previous commit
Next Next commit
Additional edge cases covered in unit tests
  • Loading branch information
josh committed Dec 11, 2019
commit 627b0118499be3efc80dec926ed0201a3c39c50e
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,12 @@ public async Task ExecuteTestWithArraylikeParameters()
MemoryStream[] streamPayloads = new MemoryStream[]
{
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 1}")),
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 65535}")), // The max allowed by javascript function.apply
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": ""1""}")), // Javascript treats length string as a numebr
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 10000}")), // Tons of extra arguments which aren't actually there
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""1"": ""test"", ""length"": 1}")), // function.apply will not see the [1] parameter because length == 1
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""1"": ""test"", ""length"": 1.5}")), // function.apply will not see the [1] parameter because length rounds down to 1
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""one"", ""1"": ""twothree"", ""length"": 2}")),
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""one"", ""2"": ""twothree"", ""length"": 3}")), // Skipping a parameter is okay
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""one"", ""2"": ""three"", ""1"": ""two"", ""length"": 3}")) // out of order is okay because arrays are just numeric indexers in javascript
};

Expand All @@ -730,6 +733,49 @@ public async Task ExecuteTestWithArraylikeParameters()
Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
}

[TestMethod]
public async Task ExecuteTestWithNonAppliableArraylikeParameters()
{
string sprocId = Guid.NewGuid().ToString();
string sprocBody = @"function() {
var context = getContext();
var response = context.getResponse();
response.setBody(true);
}";

StoredProcedureResponse storedProcedureResponse =
await this.scripts.CreateStoredProcedureAsync(new StoredProcedureProperties(sprocId, sprocBody));
Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode);
StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse);

// Insert document and then query
string testPartitionId = Guid.NewGuid().ToString();
var payload = new { id = testPartitionId, user = testPartitionId };
ItemResponse<dynamic> createItemResponse = await this.container.CreateItemAsync<dynamic>(payload);
Assert.AreEqual(HttpStatusCode.Created, createItemResponse.StatusCode);

MemoryStream[] streamPayloads = new MemoryStream[]
{
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 600000}")), // 600000 parameters -> "out of stack space"
new MemoryStream(Encoding.UTF8.GetBytes(@"{""0"":""onetwothree"", ""length"": 1e9}")), // 1 billion parameters -> exceeds javascript .apply maximum
};

foreach (MemoryStream streamPayload in streamPayloads)
{
ResponseMessage response = await this.scripts.ExecuteStoredProcedureStreamAsync(
storedProcedureId: sprocId,
streamPayload: streamPayload,
partitionKey: new Cosmos.PartitionKey(testPartitionId),
requestOptions: null,
cancellationToken: default(CancellationToken));

Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
}

StoredProcedureResponse deleteResponse = await this.scripts.DeleteStoredProcedureAsync(sprocId);
Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);
}

private static void ValidateStoredProcedureSettings(string id, string body, StoredProcedureResponse cosmosResponse)
{
StoredProcedureProperties settings = cosmosResponse.Resource;
Expand Down