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

Test Framework - Don't serialize string Json element directly to session #15007

Merged
merged 5 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions sdk/core/Azure.Core.TestFramework/src/RecordEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,11 @@ private void SerializeBody(Utf8JsonWriter jsonWriter, string name, byte[] reques

// We use array as a wrapper for string based serialization
// so if the root is an array we can't write it directly
// fallback to generic string writing
if (document.RootElement.ValueKind != JsonValueKind.Array)
// fallback to generic string writing. Also, if the root is a string
// we don't want to write it directly, as this would make matching
// not won't work in libraries that allow passing JSON as a string.
if (document.RootElement.ValueKind != JsonValueKind.Array &&
document.RootElement.ValueKind != JsonValueKind.String)
{
// Make sure we can replay JSON is exactly the same as the source
// for the case where service response was pre-formatted
Expand Down
1 change: 1 addition & 0 deletions sdk/core/Azure.Core/tests/Azure.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="..\src\Shared\RetriableStream.cs" />
<Compile Include="..\src\Shared\RequestRequestContent.cs" />
<Compile Include="..\src\Shared\ValueStopwatch.cs" />
<Compile Include="..\src\Shared\AutoRest\Utf8JsonRequestContent.cs" />
</ItemGroup>

</Project>
13 changes: 10 additions & 3 deletions sdk/core/Azure.Core/tests/RecordSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class RecordSessionTests
[TestCase("{\"json\" :\"value\"}", "application/json")]
[TestCase("[\"json\", \"value\"]", "application/json")]
[TestCase("[{\"json\":\"value\"}, {\"json\":\"value\"}]", "application/json")]
[TestCase("{\\u0002json\\u0002:{\\u0002json\\u0002:\\u0002value\\u0002}", "application/json")]
[TestCase("invalid json", "application/json")]
[TestCase("{ \"json\": \"value\" }", "unknown")]
[TestCase("multi\rline", "application/xml")]
Expand All @@ -37,10 +38,16 @@ public void CanRoundtripSessionRecord(string body, string contentType)
session.Variables["a"] = "value a";
session.Variables["b"] = "value b";

RecordEntry recordEntry = new RecordEntry();
var content = new Utf8JsonRequestContent();
content.JsonWriter.WriteStringValue(body);
var request = new MockRequest()
{
Content = content
};

RecordEntry recordEntry = RecordTransport.CreateEntry(request, new MockResponse(202));
recordEntry.Request.Headers.Add("Content-Type", new[] { contentType });
recordEntry.Request.Headers.Add("Other-Header", new[] { "multi", "value" });
recordEntry.Request.Body = bodyBytes;
recordEntry.RequestUri = "url";
recordEntry.RequestMethod = RequestMethod.Delete;

Expand Down Expand Up @@ -80,7 +87,7 @@ public void CanRoundtripSessionRecord(string body, string contentType)
CollectionAssert.AreEqual(new[] { contentType }, deserializedRecord.Response.Headers["content-type"]);
CollectionAssert.AreEqual(new[] { "multi", "value" }, deserializedRecord.Response.Headers["other-response-header"]);

CollectionAssert.AreEqual(bodyBytes, deserializedRecord.Request.Body);
CollectionAssert.AreEqual(recordEntry.Request.Body, deserializedRecord.Request.Body);
CollectionAssert.AreEqual(bodyBytes, deserializedRecord.Response.Body);
}

Expand Down