Skip to content

Commit

Permalink
Adjustment to breaking docker manifest change (#8430)
Browse files Browse the repository at this point in the history
* docker manifest (v1 and v2) and index request/response bodies are no longer encoded as text -- preventing normalization of bodies that MUST not change 
* add an additional test to verify the wonky json normalization that we DONT want to see is not happening
  • Loading branch information
scbedd authored Jun 19, 2024
1 parent 61ad076 commit bd02d74
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Sdk.Tools.TestProxy.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualBasic;
using Moq;
using NuGet.ContentModel;
using Microsoft.AspNetCore.Http.Features;
using Xunit;

namespace Azure.Sdk.Tools.TestProxy.Tests
Expand Down Expand Up @@ -175,6 +172,71 @@ public async Task CanRoundTripDockerDigest()
Assert.Equal(sampleExpected, content);
}

[Fact]
public async Task CheckDigestNotNormalized()
{
var session = TestHelpers.LoadRecordSession("Test.RecordEntries/response_with_content_digest.json");

DefaultHttpContext ctx = new DefaultHttpContext();
DefaultHttpContext requestCtx = new DefaultHttpContext();

var handler = new RecordingHandler(Directory.GetCurrentDirectory());
var guid = Guid.NewGuid().ToString();

handler.PlaybackSessions.AddOrUpdate(
guid,
session,
(key, oldValue) => session
);

// we know that on disk and when loaded from memory these bytes are totally untouached
// we need to make certain this is the case during matching as well
var untouchedBytes = session.Session.Entries[1].Request.Body;

// define all this stuff where it's easy to observe it
var testEntry = new RecordEntry()
{
RequestUri = "\"https://Sanitized.azurecr.io/v2/hello-world/manifests/test",
RequestMethod = RequestMethod.Put,
Request = new RequestOrResponse()
{
Headers = new SortedDictionary<string, string[]>()
{
{ "Accept", new string[]{ "application/json" } },
{ "Accept-Encoding", new string[]{ "gzip" } },
{ "Authorization", new string[]{ "Sanitized" } },
{ "Content-Length", new string[] { "11387" } },
{ "User-Agent", new string[]{ "azsdk-go-azcontainerregistry/v0.2.2 (go1.22.2; Windows_NT)" } },
{ "Content-Type", new string[] { "application/vnd.oci.image.index.v1+json" } },
{ "x-recording-upstream-base-uri", new string[] { "https://Sanitized.azurecr.io" } }
},
Body = untouchedBytes,
}
};

// now pull it into where it HAS to be, but is a fairly awkward preparation
var httpRequest = requestCtx.Request;
var httpResponse = requestCtx.Response;
httpRequest.Method = testEntry.RequestMethod.ToString();
httpRequest.Scheme = "https";
httpRequest.Host = new HostString("Sanitized.azurecr.io");
httpRequest.Path = "/v2/hello-world/manifests/test";
foreach (var header in testEntry.Request.Headers)
{
httpRequest.Headers[header.Key] = header.Value;
}
httpRequest.Body = new MemoryStream(testEntry.Request.Body);

var requestFeature = requestCtx.Features.Get<IHttpRequestFeature>();
if (requestFeature != null)
{
requestFeature.RawTarget = httpRequest.Path;
}

// if we successfully match, the test is working as expected
await handler.HandlePlaybackRequest(guid, httpRequest, httpResponse);
}

[Fact]
public void EnsureJsonEscaping()
{
Expand Down
Loading

0 comments on commit bd02d74

Please sign in to comment.