-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug in StreamingUtf8JsonReader, add tests
- Loading branch information
1 parent
fe0da63
commit 3124916
Showing
2 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
sdk/test/UnitTests/Custom/Marshalling/StreamingUtf8JsonReaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using AWSSDK_DotNet.CommonTest.Utils; | ||
using AWSSDK_DotNet.UnitTests; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.IO; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Amazon.Runtime.Internal.Transform; | ||
namespace AWSSDK.UnitTests | ||
{ | ||
/// <summary> | ||
/// Protocol Tests already exists to test the marshalling and unmarhsalling of request and responses in json, but they don't test very | ||
/// large payloads, which would trigger the logic for | ||
/// This class just tests the wrapper class StreamingUtf8JsonReader. | ||
/// </summary> | ||
[TestClass] | ||
public class StreamingUtf8JsonReaderTests | ||
{ | ||
[TestMethod] | ||
public void HandlesUtf8BOM() | ||
{ | ||
// we can't use reflection to access the private fields of StreamingUtf8JsonReader since it is a ref struct so we have to test it this way. | ||
var a = Convert.ToByte('{'); | ||
var b = Convert.ToByte('x'); | ||
var c = Convert.ToByte(':'); | ||
var d = Convert.ToByte('y'); | ||
var e = Convert.ToByte('}'); | ||
|
||
byte[] payload = new byte[] { 0xEF, 0xBB, 0xBF, a, b, c ,d, e}; | ||
MemoryStream stream = new MemoryStream(payload); | ||
StreamingUtf8JsonReader reader = new StreamingUtf8JsonReader(stream); | ||
bool firstIteration = true; | ||
while (reader.Read()) | ||
{ | ||
if (firstIteration) | ||
{ | ||
// make sure the BOM was removed | ||
Assert.IsTrue(reader.Reader.TokenType == JsonTokenType.StartObject); | ||
firstIteration = false; | ||
return; | ||
} | ||
} | ||
} | ||
// This method tests that if the json token starts in one buffer but continues into the next buffer | ||
// the reader can handle parsing it correctly. | ||
[TestMethod] | ||
public void Utf8JsonReaderHandlesJsonTokenThatSpansMultipleBuffers() | ||
{ | ||
// Arrange | ||
// here we're creating a json string that is greater than 4096 bytes to test the GetMoreBytesFromStream logic | ||
var sb = new StringBuilder(); | ||
sb.Append("{ \"key\": \""); | ||
sb.Append(new string('x', 7500)); // String with 5000 'x' characters | ||
sb.Append("\" }"); | ||
string largeJson = sb.ToString(); | ||
|
||
byte[] payload = Encoding.UTF8.GetBytes(largeJson); | ||
using (var stream = new MemoryStream(payload)) | ||
{ | ||
var reader = new StreamingUtf8JsonReader(stream); | ||
string key = null, value = null; | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.Reader.TokenType == JsonTokenType.PropertyName) | ||
{ | ||
key = reader.Reader.GetString(); | ||
} | ||
else if (reader.Reader.TokenType == JsonTokenType.String) | ||
{ | ||
value = reader.Reader.GetString(); | ||
} | ||
} | ||
Assert.AreEqual<string>("key", key); | ||
Assert.AreEqual<string>(new string('x', 7500), value); | ||
} | ||
} | ||
} | ||
} |