|
| 1 | +/* |
| 2 | + * OpenAPI Petstore |
| 3 | + * |
| 4 | + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ |
| 5 | + * |
| 6 | + * The version of the OpenAPI document: 1.0.0 |
| 7 | + * Generated by: https://github.com/openapitools/openapi-generator.git |
| 8 | + */ |
| 9 | + |
| 10 | +using System; |
| 11 | +using Microsoft.Extensions.Hosting; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using System.Collections.Generic; |
| 14 | +using System.Security.Cryptography; |
| 15 | +using Org.OpenAPITools.Client; |
| 16 | +using Org.OpenAPITools.Api; |
| 17 | +using Org.OpenAPITools.Extensions; |
| 18 | +using Xunit; |
| 19 | + |
| 20 | +namespace Org.OpenAPITools.Test.Api |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Tests the dependency injection. |
| 24 | + /// </summary> |
| 25 | + public class DependencyInjectionTest |
| 26 | + { |
| 27 | + private readonly IHost _hostUsingConfigureWithoutAClient = |
| 28 | + Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) => |
| 29 | + { |
| 30 | + ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1)); |
| 31 | + options.AddTokens(apiKeyToken1); |
| 32 | + |
| 33 | + ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1)); |
| 34 | + options.AddTokens(apiKeyToken2); |
| 35 | + |
| 36 | + BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1)); |
| 37 | + options.AddTokens(bearerToken1); |
| 38 | + |
| 39 | + BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1)); |
| 40 | + options.AddTokens(basicToken1); |
| 41 | + |
| 42 | + HttpSigningConfiguration config1 = new("<keyId>", "<keyFilePath>", null, [], HashAlgorithmName.SHA256, "<signingAlgorithm>", 0); |
| 43 | + HttpSignatureToken httpSignatureToken1 = new(config1, timeout: TimeSpan.FromSeconds(1)); |
| 44 | + options.AddTokens(httpSignatureToken1); |
| 45 | + |
| 46 | + OAuthToken oauthToken1 = new("token", timeout: TimeSpan.FromSeconds(1)); |
| 47 | + options.AddTokens(oauthToken1); |
| 48 | + }) |
| 49 | + .Build(); |
| 50 | + |
| 51 | + private readonly IHost _hostUsingConfigureWithAClient = |
| 52 | + Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) => |
| 53 | + { |
| 54 | + ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1)); |
| 55 | + options.AddTokens(apiKeyToken1); |
| 56 | + |
| 57 | + ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1)); |
| 58 | + options.AddTokens(apiKeyToken2); |
| 59 | + |
| 60 | + BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1)); |
| 61 | + options.AddTokens(bearerToken1); |
| 62 | + |
| 63 | + BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1)); |
| 64 | + options.AddTokens(basicToken1); |
| 65 | + |
| 66 | + HttpSigningConfiguration config1 = new("<keyId>", "<keyFilePath>", null, [], HashAlgorithmName.SHA256, "<signingAlgorithm>", 0); |
| 67 | + HttpSignatureToken httpSignatureToken1 = new(config1, timeout: TimeSpan.FromSeconds(1)); |
| 68 | + options.AddTokens(httpSignatureToken1); |
| 69 | + |
| 70 | + OAuthToken oauthToken = new("token", timeout: TimeSpan.FromSeconds(1)); |
| 71 | + options.AddTokens(oauthToken); |
| 72 | + options.AddApiHttpClients(client => client.BaseAddress = new Uri(ClientUtils.BASE_ADDRESS)); |
| 73 | + }) |
| 74 | + .Build(); |
| 75 | + |
| 76 | + private readonly IHost _hostUsingAddWithoutAClient = |
| 77 | + Host.CreateDefaultBuilder([]).ConfigureServices((host, services) => |
| 78 | + { |
| 79 | + services.AddApi(options => |
| 80 | + { |
| 81 | + ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1)); |
| 82 | + options.AddTokens(apiKeyToken1); |
| 83 | + |
| 84 | + ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1)); |
| 85 | + options.AddTokens(apiKeyToken2); |
| 86 | + |
| 87 | + BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1)); |
| 88 | + options.AddTokens(bearerToken1); |
| 89 | + |
| 90 | + BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1)); |
| 91 | + options.AddTokens(basicToken1); |
| 92 | + |
| 93 | + HttpSigningConfiguration config1 = new("<keyId>", "<keyFilePath>", null, [], HashAlgorithmName.SHA256, "<signingAlgorithm>", 0); |
| 94 | + HttpSignatureToken httpSignatureToken1 = new(config1, timeout: TimeSpan.FromSeconds(1)); |
| 95 | + options.AddTokens(httpSignatureToken1); |
| 96 | + |
| 97 | + OAuthToken oauthToken1 = new("token", timeout: TimeSpan.FromSeconds(1)); |
| 98 | + options.AddTokens(oauthToken1); |
| 99 | + }); |
| 100 | + }) |
| 101 | + .Build(); |
| 102 | + |
| 103 | + private readonly IHost _hostUsingAddWithAClient = |
| 104 | + Host.CreateDefaultBuilder([]).ConfigureServices((host, services) => |
| 105 | + { |
| 106 | + services.AddApi(options => |
| 107 | + { |
| 108 | + ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1)); |
| 109 | + options.AddTokens(apiKeyToken1); |
| 110 | + |
| 111 | + ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1)); |
| 112 | + options.AddTokens(apiKeyToken2); |
| 113 | + |
| 114 | + BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1)); |
| 115 | + options.AddTokens(bearerToken1); |
| 116 | + |
| 117 | + BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1)); |
| 118 | + options.AddTokens(basicToken1); |
| 119 | + |
| 120 | + HttpSigningConfiguration config1 = new("<keyId>", "<keyFilePath>", null, [], HashAlgorithmName.SHA256, "<signingAlgorithm>", 0); |
| 121 | + HttpSignatureToken httpSignatureToken1 = new(config1, timeout: TimeSpan.FromSeconds(1)); |
| 122 | + options.AddTokens(httpSignatureToken1); |
| 123 | + |
| 124 | + OAuthToken oauthToken1 = new("token", timeout: TimeSpan.FromSeconds(1)); |
| 125 | + options.AddTokens(oauthToken1); |
| 126 | + options.AddApiHttpClients(client => client.BaseAddress = new Uri(ClientUtils.BASE_ADDRESS)); |
| 127 | + }); |
| 128 | + }) |
| 129 | + .Build(); |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Test dependency injection when using the configure method |
| 133 | + /// </summary> |
| 134 | + [Fact] |
| 135 | + public void ConfigureApiWithAClientTest() |
| 136 | + { |
| 137 | + var anotherFakeApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IAnotherFakeApi>(); |
| 138 | + Assert.True(anotherFakeApi.HttpClient.BaseAddress != null); |
| 139 | + |
| 140 | + var defaultApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IDefaultApi>(); |
| 141 | + Assert.True(defaultApi.HttpClient.BaseAddress != null); |
| 142 | + |
| 143 | + var fakeApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IFakeApi>(); |
| 144 | + Assert.True(fakeApi.HttpClient.BaseAddress != null); |
| 145 | + |
| 146 | + var fakeClassnameTags123Api = _hostUsingConfigureWithAClient.Services.GetRequiredService<IFakeClassnameTags123Api>(); |
| 147 | + Assert.True(fakeClassnameTags123Api.HttpClient.BaseAddress != null); |
| 148 | + |
| 149 | + var petApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IPetApi>(); |
| 150 | + Assert.True(petApi.HttpClient.BaseAddress != null); |
| 151 | + |
| 152 | + var storeApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IStoreApi>(); |
| 153 | + Assert.True(storeApi.HttpClient.BaseAddress != null); |
| 154 | + |
| 155 | + var userApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IUserApi>(); |
| 156 | + Assert.True(userApi.HttpClient.BaseAddress != null); |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Test dependency injection when using the configure method |
| 161 | + /// </summary> |
| 162 | + [Fact] |
| 163 | + public void ConfigureApiWithoutAClientTest() |
| 164 | + { |
| 165 | + var anotherFakeApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IAnotherFakeApi>(); |
| 166 | + Assert.True(anotherFakeApi.HttpClient.BaseAddress != null); |
| 167 | + |
| 168 | + var defaultApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IDefaultApi>(); |
| 169 | + Assert.True(defaultApi.HttpClient.BaseAddress != null); |
| 170 | + |
| 171 | + var fakeApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IFakeApi>(); |
| 172 | + Assert.True(fakeApi.HttpClient.BaseAddress != null); |
| 173 | + |
| 174 | + var fakeClassnameTags123Api = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IFakeClassnameTags123Api>(); |
| 175 | + Assert.True(fakeClassnameTags123Api.HttpClient.BaseAddress != null); |
| 176 | + |
| 177 | + var petApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IPetApi>(); |
| 178 | + Assert.True(petApi.HttpClient.BaseAddress != null); |
| 179 | + |
| 180 | + var storeApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IStoreApi>(); |
| 181 | + Assert.True(storeApi.HttpClient.BaseAddress != null); |
| 182 | + |
| 183 | + var userApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IUserApi>(); |
| 184 | + Assert.True(userApi.HttpClient.BaseAddress != null); |
| 185 | + } |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// Test dependency injection when using the add method |
| 189 | + /// </summary> |
| 190 | + [Fact] |
| 191 | + public void AddApiWithAClientTest() |
| 192 | + { |
| 193 | + var anotherFakeApi = _hostUsingAddWithAClient.Services.GetRequiredService<IAnotherFakeApi>(); |
| 194 | + Assert.True(anotherFakeApi.HttpClient.BaseAddress != null); |
| 195 | + |
| 196 | + var defaultApi = _hostUsingAddWithAClient.Services.GetRequiredService<IDefaultApi>(); |
| 197 | + Assert.True(defaultApi.HttpClient.BaseAddress != null); |
| 198 | + |
| 199 | + var fakeApi = _hostUsingAddWithAClient.Services.GetRequiredService<IFakeApi>(); |
| 200 | + Assert.True(fakeApi.HttpClient.BaseAddress != null); |
| 201 | + |
| 202 | + var fakeClassnameTags123Api = _hostUsingAddWithAClient.Services.GetRequiredService<IFakeClassnameTags123Api>(); |
| 203 | + Assert.True(fakeClassnameTags123Api.HttpClient.BaseAddress != null); |
| 204 | + |
| 205 | + var petApi = _hostUsingAddWithAClient.Services.GetRequiredService<IPetApi>(); |
| 206 | + Assert.True(petApi.HttpClient.BaseAddress != null); |
| 207 | + |
| 208 | + var storeApi = _hostUsingAddWithAClient.Services.GetRequiredService<IStoreApi>(); |
| 209 | + Assert.True(storeApi.HttpClient.BaseAddress != null); |
| 210 | + |
| 211 | + var userApi = _hostUsingAddWithAClient.Services.GetRequiredService<IUserApi>(); |
| 212 | + Assert.True(userApi.HttpClient.BaseAddress != null); |
| 213 | + } |
| 214 | + |
| 215 | + /// <summary> |
| 216 | + /// Test dependency injection when using the add method |
| 217 | + /// </summary> |
| 218 | + [Fact] |
| 219 | + public void AddApiWithoutAClientTest() |
| 220 | + { |
| 221 | + var anotherFakeApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IAnotherFakeApi>(); |
| 222 | + Assert.True(anotherFakeApi.HttpClient.BaseAddress != null); |
| 223 | + |
| 224 | + var defaultApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IDefaultApi>(); |
| 225 | + Assert.True(defaultApi.HttpClient.BaseAddress != null); |
| 226 | + |
| 227 | + var fakeApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IFakeApi>(); |
| 228 | + Assert.True(fakeApi.HttpClient.BaseAddress != null); |
| 229 | + |
| 230 | + var fakeClassnameTags123Api = _hostUsingAddWithoutAClient.Services.GetRequiredService<IFakeClassnameTags123Api>(); |
| 231 | + Assert.True(fakeClassnameTags123Api.HttpClient.BaseAddress != null); |
| 232 | + |
| 233 | + var petApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IPetApi>(); |
| 234 | + Assert.True(petApi.HttpClient.BaseAddress != null); |
| 235 | + |
| 236 | + var storeApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IStoreApi>(); |
| 237 | + Assert.True(storeApi.HttpClient.BaseAddress != null); |
| 238 | + |
| 239 | + var userApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IUserApi>(); |
| 240 | + Assert.True(userApi.HttpClient.BaseAddress != null); |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments