Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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>
57 changes: 57 additions & 0 deletions sdk/core/Azure.Core/tests/RecordSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,63 @@ public void CanRoundtripSessionRecord(string body, string contentType)
CollectionAssert.AreEqual(bodyBytes, deserializedRecord.Response.Body);
}

[Test]
public void CanRoundTripEncodedJson()
{
var body = "{\\u0002json\\u0002:{\\u0002json\\u0002:\\u0002value\\u0002}}";
var contentType = "application/json";
var session = new RecordSession();
session.Variables["a"] = "value a";
session.Variables["b"] = "value b";
var content = new Utf8JsonRequestContent();
content.JsonWriter.WriteStringValue(body);
var request = new MockRequest()
{
Content = content
};

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

recordEntry.Response.Headers.Add("Content-Type", new[] { contentType });
recordEntry.Response.Headers.Add("Other-Response-Header", new[] { "multi", "value" });

session.Entries.Add(recordEntry);

var arrayBufferWriter = new ArrayBufferWriter<byte>();
using var jsonWriter = new Utf8JsonWriter(arrayBufferWriter, new JsonWriterOptions()
{
Indented = true
});
session.Serialize(jsonWriter);
jsonWriter.Flush();

TestContext.Out.WriteLine(Encoding.UTF8.GetString(arrayBufferWriter.WrittenMemory.ToArray()));

var document = JsonDocument.Parse(arrayBufferWriter.WrittenMemory);
var deserializedSession = RecordSession.Deserialize(document.RootElement);

Assert.AreEqual("value a", deserializedSession.Variables["a"]);
Assert.AreEqual("value b", deserializedSession.Variables["b"]);

RecordEntry deserializedRecord = deserializedSession.Entries.Single();

Assert.AreEqual(RequestMethod.Put, recordEntry.RequestMethod);
Assert.AreEqual("url", recordEntry.RequestUri);
Assert.AreEqual(200, recordEntry.StatusCode);

CollectionAssert.AreEqual(new[] { contentType }, deserializedRecord.Request.Headers["content-type"]);
CollectionAssert.AreEqual(new[] { "multi", "value" }, deserializedRecord.Request.Headers["other-header"]);

CollectionAssert.AreEqual(new[] { contentType }, deserializedRecord.Response.Headers["content-type"]);
CollectionAssert.AreEqual(new[] { "multi", "value" }, deserializedRecord.Response.Headers["other-response-header"]);

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

[Test]
public void RecordMatcherThrowsExceptionsWithDetails()
{
Expand Down