File size: 6,580 Bytes
7b715bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | // Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if NET6_0_OR_GREATER
using Google.Apis.Http;
using Google.Apis.Services;
using Google.Apis.Translate.v2;
using Google.Apis.Translate.v2.Data;
using IntegrationTests.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace IntegrationTests
{
public class HttpClientFromMessageHandlerFactoryTests
{
public static IHostBuilder ConfigureHostBuilder(bool transientServiceClient) =>
Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
// Configure one HttpClient that performs automatic decompression.
services.AddHttpClient("allowsDecompression").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
// We don't need to check for SupportsRedirectConfiguration
// or SupportsAutomaticDecompression because they are true by default
// for System.Net.HttpClientHandler
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
});
// Configure one HttpClient that doesn't perform automatic decompression.
services.AddHttpClient("disallowsDecompression").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
// We don't need to check for SupportsRedirectConfiguration
// or SupportsAutomaticDecompression because they are true by default
// for System.Net.HttpClientHandler
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.None
});
// Register Google.Apis.Http.IHttpClientFactory using the registered
// System.Net.Http.IHttpMessageHandlerFactory to obtain inner message handlers.
services.AddSingleton<Google.Apis.Http.IHttpClientFactory>(sp => new HttpClientFromMessageHandlerFactory(
options => new HttpClientFromMessageHandlerFactory.ConfiguredHttpMessageHandler(
messageHandler: sp.GetRequiredService<IHttpMessageHandlerFactory>().CreateHandler(options.MayPerformDecompression ? "allowsDecompression" : "disallowsDecompression"),
// We know we have configured them correctly.
handlesRedirect: false,
performsAutomaticDecompression: options.MayPerformDecompression
)));
// Configure a TransalateService that uses the HttpClientFromMessageHandlerFactory
// Transient or not for testing different reuse scenarios.
if (transientServiceClient)
{
services.AddTransient(CreateTranslateService);
}
else
{
services.AddScoped(CreateTranslateService);
}
static TranslateService CreateTranslateService(IServiceProvider sp) => new TranslateService(new BaseClientService.Initializer
{
HttpClientInitializer = Helper.GetServiceCredential().CreateScoped(TranslateService.Scope.CloudTranslation),
ApplicationName = "IntegrationTest",
HttpClientFactory = sp.GetRequiredService<Google.Apis.Http.IHttpClientFactory>()
});
});
[Theory]
[CombinatorialData]
public async Task SingleCall(bool transientServiceClient)
{
using var host = await ConfigureHostBuilder(transientServiceClient).StartAsync();
var translateService = host.Services.GetRequiredService<TranslateService>();
var translateRequest = translateService.Translations.Translate(new TranslateTextRequest
{
Format = "text",
Q = new List<string> { "The cold weather will soon be over" },
Source = "en",
Target = "fr",
});
var response = await translateRequest.ExecuteAsync();
var translation = Assert.Single(response.Translations);
// We sometimes get:
// Le temps froid sera bientôt fini
// Le froid sera bientôt terminé
Assert.Contains("froid sera bientôt", translation.TranslatedText);
await host.StopAsync();
}
[Theory]
[CombinatorialData]
public async Task ConcurrentCalls(bool transientServiceClient)
{
using var host = await ConfigureHostBuilder(transientServiceClient).StartAsync();
var translationTasks = Enumerable.Range(0, 100).Select(i =>
{
var translateService = host.Services.GetRequiredService<TranslateService>();
var translateRequest = translateService.Translations.Translate(new TranslateTextRequest
{
Format = "text",
Q = new List<string> { $"Task {i}" },
Source = "en",
Target = "fr",
});
return translateRequest.ExecuteAsync();
});
var responses = await Task.WhenAll(translationTasks);
for (int i = 0; i < responses.Length; i++)
{
var translation = Assert.Single(responses[i].Translations);
Assert.Contains(i.ToString(), translation.TranslatedText);
}
await host.StopAsync();
}
}
}
#endif
|