Skip to content

Commit

Permalink
migrate form recognizer (#25879)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored Dec 13, 2021
1 parent 24aab72 commit ac7e23a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
32 changes: 28 additions & 4 deletions sdk/core/Azure.Core.TestFramework/src/ProxyTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class ProxyTransport : HttpPipelineTransport
private static readonly RemoteCertificateValidationCallback ServerCertificateCustomValidationCallback =
(_, certificate, _, _) => certificate.Issuer == TestProxy.DevCertIssuer;

public ProxyTransport(TestProxy proxy, HttpPipelineTransport transport, TestRecording recording)
private readonly Func<EntryRecordModel> _filter;

public ProxyTransport(TestProxy proxy, HttpPipelineTransport transport, TestRecording recording, Func<EntryRecordModel> filter)
{
if (transport is HttpClientTransport)
{
Expand All @@ -39,6 +41,7 @@ public ProxyTransport(TestProxy proxy, HttpPipelineTransport transport, TestReco
}
_recording = recording;
_proxy = proxy;
_filter = filter;
}

public override void Process(HttpMessage message)
Expand All @@ -59,15 +62,15 @@ private async Task ProcessResponseAsync(HttpMessage message, bool async)
{
if (message.Response.Headers.Contains("x-request-mismatch"))
{
var streamreader = new StreamReader(message.Response.ContentStream);
var streamReader = new StreamReader(message.Response.ContentStream);
string response;
if (async)
{
response = await streamreader.ReadToEndAsync();
response = await streamReader.ReadToEndAsync();
}
else
{
response = streamreader.ReadToEnd();
response = streamReader.ReadToEnd();
}
throw new TestRecordingMismatchException(response);
}
Expand Down Expand Up @@ -106,6 +109,27 @@ public override Request CreateRequest()
// copied from https://github.com/Azure/azure-sdk-for-net/blob/main/common/Perf/Azure.Test.Perf/TestProxyPolicy.cs
private void RedirectToTestProxy(HttpMessage message)
{
if (_recording.Mode == RecordedTestMode.Record)
{
switch (_filter())
{
case EntryRecordModel.Record:
break;
case EntryRecordModel.RecordWithoutRequestBody:
message.Request.Content = null;
break;
case EntryRecordModel.DoNotRecord:
return;
}
}
else if (_recording.Mode == RecordedTestMode.Playback)
{
if (_filter() == EntryRecordModel.RecordWithoutRequestBody)
{
message.Request.Content = null;
}
}

var request = message.Request;
request.Headers.SetValue("x-recording-id", _recording.RecordingId);
request.Headers.SetValue("x-recording-mode", _recording.Mode.ToString().ToLower());
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core.TestFramework/src/TestRecording.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public HttpPipelineTransport CreateTransport(HttpPipelineTransport currentTransp
{
if (!_useLegacyTransport && Mode != RecordedTestMode.Live)
{
return new ProxyTransport(_proxy, currentTransport, this);
return new ProxyTransport(_proxy, currentTransport, this, () => _disableRecording.Value);
}
return Mode switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DocumentAnalysisLiveTestBase : RecordedTestBase<DocumentAnalysisTes
private readonly DocumentAnalysisClientOptions.ServiceVersion _serviceVersion;

public DocumentAnalysisLiveTestBase(bool isAsync, DocumentAnalysisClientOptions.ServiceVersion serviceVersion)
: base(isAsync, useLegacyTransport: true)
: base(isAsync)
{
_serviceVersion = serviceVersion;
Sanitizer = new DocumentAnalysisRecordedTestSanitizer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class FormRecognizerLiveTestBase : RecordedTestBase<FormRecognizerTestEnv
private readonly FormRecognizerClientOptions.ServiceVersion _serviceVersion;

public FormRecognizerLiveTestBase(bool isAsync, FormRecognizerClientOptions.ServiceVersion serviceVersion)
: base(isAsync, useLegacyTransport: true)
: base(isAsync)
{
_serviceVersion = serviceVersion;
Sanitizer = new FormRecognizerRecordedTestSanitizer();
Expand Down

0 comments on commit ac7e23a

Please sign in to comment.