-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FormRecognizer] Fix: StartRecognizeCustomFormsFromUri works when URI…
… has blank space (#12192)
- Loading branch information
Showing
5 changed files
with
225 additions
and
11 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
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
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
133 changes: 133 additions & 0 deletions
133
...nizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientMockTests.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,133 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.AI.FormRecognizer.Tests | ||
{ | ||
/// <summary> | ||
/// The suite of mock tests for the <see cref="FormRecognizerClient"/> class. | ||
/// </summary> | ||
public class FormRecognizerClientMockTests : ClientTestBase | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FormRecognizerClientMockTests"/> class. | ||
/// </summary> | ||
/// <param name="isAsync">A flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods.</param> | ||
public FormRecognizerClientMockTests(bool isAsync) : base(isAsync) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a fake <see cref="FormRecognizerClient" /> and instruments it to make use of the Azure Core | ||
/// Test Framework functionalities. | ||
/// </summary> | ||
/// <param name="options">A set of options to apply when configuring the client.</param> | ||
/// <returns>The instrumented <see cref="FormRecognizerClient" />.</returns> | ||
private FormRecognizerClient CreateInstrumentedClient(FormRecognizerClientOptions options = default) | ||
{ | ||
var fakeEndpoint = new Uri("http://localhost"); | ||
var fakeCredential = new AzureKeyCredential("fakeKey"); | ||
options ??= new FormRecognizerClientOptions(); | ||
|
||
var client = new FormRecognizerClient(fakeEndpoint, fakeCredential, options); | ||
return InstrumentClient(client); | ||
} | ||
|
||
[Test] | ||
public async Task StartRecognizeContentFromUriEncodesBlankSpaces() | ||
{ | ||
var mockResponse = new MockResponse(202); | ||
mockResponse.AddHeader(new HttpHeader("Operation-Location", "host/layout/analyzeResults/00000000000000000000000000000000")); | ||
|
||
var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); | ||
var options = new FormRecognizerClientOptions() { Transport = mockTransport }; | ||
var client = CreateInstrumentedClient(options); | ||
|
||
var encodedUriString = "https://fakeuri.com/blank%20space"; | ||
var decodedUriString = "https://fakeuri.com/blank space"; | ||
|
||
await client.StartRecognizeContentFromUriAsync(new Uri(encodedUriString)); | ||
await client.StartRecognizeContentFromUriAsync(new Uri(decodedUriString)); | ||
|
||
Assert.AreEqual(2, mockTransport.Requests.Count); | ||
|
||
foreach (var request in mockTransport.Requests) | ||
{ | ||
var requestBody = GetString(request.Content); | ||
|
||
Assert.True(requestBody.Contains(encodedUriString)); | ||
Assert.False(requestBody.Contains(decodedUriString)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task StartRecognizeReceiptsFromUriEncodesBlankSpaces() | ||
{ | ||
var mockResponse = new MockResponse(202); | ||
mockResponse.AddHeader(new HttpHeader("Operation-Location", "host/prebuilt/receipt/analyzeResults/00000000000000000000000000000000")); | ||
|
||
var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); | ||
var options = new FormRecognizerClientOptions() { Transport = mockTransport }; | ||
var client = CreateInstrumentedClient(options); | ||
|
||
var encodedUriString = "https://fakeuri.com/blank%20space"; | ||
var decodedUriString = "https://fakeuri.com/blank space"; | ||
|
||
await client.StartRecognizeReceiptsFromUriAsync(new Uri(encodedUriString)); | ||
await client.StartRecognizeReceiptsFromUriAsync(new Uri(decodedUriString)); | ||
|
||
Assert.AreEqual(2, mockTransport.Requests.Count); | ||
|
||
foreach (var request in mockTransport.Requests) | ||
{ | ||
var requestBody = GetString(request.Content); | ||
|
||
Assert.True(requestBody.Contains(encodedUriString)); | ||
Assert.False(requestBody.Contains(decodedUriString)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task StartRecognizeCustomFormsFromUriEncodesBlankSpaces() | ||
{ | ||
var mockResponse = new MockResponse(202); | ||
mockResponse.AddHeader(new HttpHeader("Operation-Location", "host/custom/models/00000000000000000000000000000000/analyzeResults/00000000000000000000000000000000")); | ||
|
||
var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); | ||
var options = new FormRecognizerClientOptions() { Transport = mockTransport }; | ||
var client = CreateInstrumentedClient(options); | ||
|
||
var encodedUriString = "https://fakeuri.com/blank%20space"; | ||
var decodedUriString = "https://fakeuri.com/blank space"; | ||
|
||
await client.StartRecognizeCustomFormsFromUriAsync("00000000000000000000000000000000", new Uri(encodedUriString)); | ||
await client.StartRecognizeCustomFormsFromUriAsync("00000000000000000000000000000000", new Uri(decodedUriString)); | ||
|
||
Assert.AreEqual(2, mockTransport.Requests.Count); | ||
|
||
foreach (var request in mockTransport.Requests) | ||
{ | ||
var requestBody = GetString(request.Content); | ||
|
||
Assert.True(requestBody.Contains(encodedUriString)); | ||
Assert.False(requestBody.Contains(decodedUriString)); | ||
} | ||
} | ||
|
||
private static string GetString(RequestContent content) | ||
{ | ||
using var stream = new MemoryStream(); | ||
content.WriteTo(stream, CancellationToken.None); | ||
|
||
return Encoding.UTF8.GetString(stream.ToArray()); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...ecognizer/Azure.AI.FormRecognizer/tests/FormTrainingClient/FormTrainingClientMockTests.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,80 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.AI.FormRecognizer.Training; | ||
using Azure.Core; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.AI.FormRecognizer.Tests | ||
{ | ||
/// <summary> | ||
/// The suite of mock tests for the <see cref="FormTrainingClient"/> class. | ||
/// </summary> | ||
public class FormTrainingClientMockTests : ClientTestBase | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FormTrainingClientMockTests"/> class. | ||
/// </summary> | ||
/// <param name="isAsync">A flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods.</param> | ||
public FormTrainingClientMockTests(bool isAsync) : base(isAsync) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a fake <see cref="FormTrainingClient" /> and instruments it to make use of the Azure Core | ||
/// Test Framework functionalities. | ||
/// </summary> | ||
/// <param name="options">A set of options to apply when configuring the client.</param> | ||
/// <returns>The instrumented <see cref="FormTrainingClient" />.</returns> | ||
private FormTrainingClient CreateInstrumentedClient(FormRecognizerClientOptions options = default) | ||
{ | ||
var fakeEndpoint = new Uri("http://localhost"); | ||
var fakeCredential = new AzureKeyCredential("fakeKey"); | ||
options ??= new FormRecognizerClientOptions(); | ||
|
||
var client = new FormTrainingClient(fakeEndpoint, fakeCredential, options); | ||
return InstrumentClient(client); | ||
} | ||
|
||
[Test] | ||
public async Task StartTrainingEncodesBlankSpaces() | ||
{ | ||
var mockResponse = new MockResponse(201); | ||
mockResponse.AddHeader(new HttpHeader("Location", "host/custom/models/00000000000000000000000000000000")); | ||
|
||
var mockTransport = new MockTransport(new[] { mockResponse, mockResponse }); | ||
var options = new FormRecognizerClientOptions() { Transport = mockTransport }; | ||
var client = CreateInstrumentedClient(options); | ||
|
||
var encodedUriString = "https://fakeuri.com/blank%20space"; | ||
var decodedUriString = "https://fakeuri.com/blank space"; | ||
|
||
await client.StartTrainingAsync(new Uri(encodedUriString)); | ||
await client.StartTrainingAsync(new Uri(decodedUriString)); | ||
|
||
Assert.AreEqual(2, mockTransport.Requests.Count); | ||
|
||
foreach (var request in mockTransport.Requests) | ||
{ | ||
var requestBody = GetString(request.Content); | ||
|
||
Assert.True(requestBody.Contains(encodedUriString)); | ||
Assert.False(requestBody.Contains(decodedUriString)); | ||
} | ||
} | ||
|
||
private static string GetString(RequestContent content) | ||
{ | ||
using var stream = new MemoryStream(); | ||
content.WriteTo(stream, CancellationToken.None); | ||
|
||
return Encoding.UTF8.GetString(stream.ToArray()); | ||
} | ||
} | ||
} |