public async Task StartBuildModelEncodesBlankSpaces()
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader("operation-location", "host/operations/00000000000000000000000000000000?api-version=2021-07-30-preview"));

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new DocumentAnalysisClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateInstrumentedClient(options);

            var encodedUriString = "https://fakeuri.com/blank%20space";
            var decodedUriString = "https://fakeuri.com/blank space";

            await client.StartBuildModelAsync(new Uri(encodedUriString), DocumentBuildMode.Template);

            await client.StartBuildModelAsync(new Uri(decodedUriString), DocumentBuildMode.Template);

            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));
            }
        }
예제 #2
0
        public async Task StartAnalyzeDocumentFromUriEncodesBlankSpaces()
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader(Constants.OperationLocationHeader, OperationId));

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new DocumentAnalysisClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateInstrumentedClient(options);

            var encodedUriString = "https://fakeuri.com/blank%20space";
            var decodedUriString = "https://fakeuri.com/blank space";

            await client.StartAnalyzeDocumentFromUriAsync(FakeGuid, new Uri(encodedUriString));

            await client.StartAnalyzeDocumentFromUriAsync(FakeGuid, 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));
            }
        }
예제 #3
0
        public async Task AnalyzeDocumentOperationCreatesDiagnosticScopeOnUpdate()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticNamespace);
            using var stream       = new MemoryStream(Encoding.UTF8.GetBytes("{}"));

            var mockResponse = new MockResponse(200);

            mockResponse.ContentStream = stream;

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new DocumentAnalysisClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateDocumentAnalysisClient(options);

            var operation = new AnalyzeDocumentOperation(AnalyzeOperationId, client);

            if (IsAsync)
            {
                await operation.UpdateStatusAsync();
            }
            else
            {
                operation.UpdateStatus();
            }

            testListener.AssertScope($"{nameof(AnalyzeDocumentOperation)}.{nameof(AnalyzeDocumentOperation.UpdateStatus)}");
        }
예제 #4
0
        /// <summary>
        /// Creates a fake <see cref="DocumentAnalysisClient" /> with the specified set of options.
        /// </summary>
        /// <param name="options">A set of options to apply when configuring the client.</param>
        /// <returns>The fake <see cref="DocumentAnalysisClient" />.</returns>
        private DocumentAnalysisClient CreateDocumentAnalysisClient(DocumentAnalysisClientOptions options = default)
        {
            var fakeEndpoint   = new Uri("http://localhost");
            var fakeCredential = new AzureKeyCredential("fakeKey");

            options ??= new DocumentAnalysisClientOptions();

            return(new DocumentAnalysisClient(fakeEndpoint, fakeCredential, options));
        }
        /// <summary>
        /// Creates a fake <see cref="DocumentModelAdministrationClient" /> and instruments it to make use of the Azure Core
        /// Test Framework functionalities.
        /// </summary>
        /// <returns>The instrumented <see cref="DocumentModelAdministrationClient" />.</returns>
        private DocumentModelAdministrationClient CreateInstrumentedClient(DocumentAnalysisClientOptions options = default)
        {
            var fakeEndpoint   = new Uri("http://notreal.azure.com/");
            var fakeCredential = new AzureKeyCredential("fakeKey");

            options ??= new DocumentAnalysisClientOptions();
            var client = new DocumentModelAdministrationClient(fakeEndpoint, fakeCredential, options);

            return(client);
        }
        public async Task StartBuildModelGeneratesModelID()
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader("operation-location", "host/operations/00000000000000000000000000000000?api-version=2021-07-30-preview"));

            var mockTransport = new MockTransport(new[] { mockResponse });
            var options       = new DocumentAnalysisClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateInstrumentedClient(options);

            await client.StartBuildModelAsync(new Uri("http://localhost"), DocumentBuildMode.Template);

            var    contentString = GetString(mockTransport.Requests.Single().Content);
            string modelId       = contentString.Substring(contentString.IndexOf("modelId") + 10, 36);

            ClientCommon.ValidateModelId(modelId, "test");
        }
예제 #7
0
        public async Task StartAnalyzeDocumentSendsContentType()
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader(Constants.OperationLocationHeader, OperationId));

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new DocumentAnalysisClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateInstrumentedClient(options);

            using var stream = DocumentAnalysisTestEnvironment.CreateStream(TestFile.InvoiceLeTiff);
            await client.StartAnalyzeDocumentAsync(FakeGuid, stream);

            var request = mockTransport.Requests.Single();

            Assert.True(request.Headers.TryGetValue("Content-Type", out var contentType));
            Assert.AreEqual("application/octet-stream", contentType);
        }
예제 #8
0
        public async Task StartAnalyzeDocumentSendsUserSpecifiedLocale(string locale)
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader(Constants.OperationLocationHeader, OperationId));

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new DocumentAnalysisClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateInstrumentedClient(options);

            using var stream = DocumentAnalysisTestEnvironment.CreateStream(TestFile.ReceiptJpg);
            var analyzeOptions = new AnalyzeDocumentOptions {
                Locale = locale
            };
            await client.StartAnalyzeDocumentAsync(FakeGuid, stream, analyzeOptions);

            var requestUriQuery   = mockTransport.Requests.Single().Uri.Query;
            var expectedSubstring = $"locale={locale}";

            Assert.True(requestUriQuery.Contains(expectedSubstring));
        }
예제 #9
0
        public async Task StartAnalyzeDocumentFromUriSendsMultiplePageArgument(string page1, string page2)
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader(Constants.OperationLocationHeader, OperationId));

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new DocumentAnalysisClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateInstrumentedClient(options);

            var uri            = new Uri("https://fakeuri.com/");
            var analyzeOptions = new AnalyzeDocumentOptions {
                Pages = { page1, page2 }
            };
            await client.StartAnalyzeDocumentFromUriAsync(FakeGuid, uri, analyzeOptions);

            var requestUriQuery   = mockTransport.Requests.Single().Uri.Query;
            var expectedSubstring = $"pages={page1}%2C{page2}";

            Assert.True(requestUriQuery.Contains(expectedSubstring));
        }