public async void GetDefaultCultureInfoIfCultureKeysAreMissing() { var builder = new WebApplicationBuilder() .Configure(app => { app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en-US") }); app.Run(context => { var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCulture = requestCultureFeature.RequestCulture; Assert.Equal("en-US", requestCulture.Culture.Name); Assert.Equal("en-US", requestCulture.UICulture.Name); return Task.FromResult(0); }); }); using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync("/page"); } }
public PrimeWebCheckPrimeShould() { // Arrange _server = new TestServer(TestServer.CreateBuilder() .UseStartup<Startup>()); _client = _server.CreateClient(); }
public async void GetCultureInfoFromQueryString() { var builder = new WebApplicationBuilder() .Configure(app => { app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en-US"), SupportedCultures = new List<CultureInfo> { new CultureInfo("ar-SA") }, SupportedUICultures = new List<CultureInfo> { new CultureInfo("ar-YE") } }); app.Run(context => { var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCulture = requestCultureFeature.RequestCulture; Assert.Equal("ar-SA", requestCulture.Culture.Name); Assert.Equal("ar-YE", requestCulture.UICulture.Name); return Task.FromResult(0); }); }); using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync("/page?culture=ar-SA&ui-culture=ar-YE"); } }
protected DatabaseTestBase() { // Arrange Server = new TestServer( TestServer.CreateBuilder() .UseStartup<TestStartup>()); }
public async Task CustomServiceProviderSetsApplicationServices() { var builder = new WebApplicationBuilder().UseStartup<CustomContainerStartup>(); var server = new TestServer(builder); string result = await server.CreateClient().GetStringAsync("/path"); Assert.Equal("ApplicationServicesEqual:True", result); }
public async Task CorsRequest_MatchPolicy_SetsResponseHeaders() { // Arrange var appBuilder = new WebApplicationBuilder() .Configure(app => { app.UseCors(builder => builder.WithOrigins("http://localhost:5001") .WithMethods("PUT") .WithHeaders("Header1") .WithExposedHeaders("AllowedHeader")); app.Run(async context => { await context.Response.WriteAsync("Cross origin response"); }); }) .ConfigureServices(services => services.AddCors()); using (var server = new TestServer(appBuilder)) { // Act // Actual request. var response = await server.CreateRequest("/") .AddHeader(CorsConstants.Origin, "http://localhost:5001") .SendAsync("PUT"); // Assert response.EnsureSuccessStatusCode(); Assert.Equal(2, response.Headers.Count()); Assert.Equal("Cross origin response", await response.Content.ReadAsStringAsync()); Assert.Equal("http://localhost:5001", response.Headers.GetValues(CorsConstants.AccessControlAllowOrigin).FirstOrDefault()); Assert.Equal("AllowedHeader", response.Headers.GetValues(CorsConstants.AccessControlExposeHeaders).FirstOrDefault()); } }
public async Task BufferResponseWithManualContentLength_NotReplaced() { var builder = new WebApplicationBuilder() .Configure(app => { app.UseResponseBuffering(); app.Run(async context => { context.Response.ContentLength = 12; Assert.False(context.Response.HasStarted); Assert.True(context.Response.Body.CanSeek); await context.Response.WriteAsync("Hello World"); Assert.False(context.Response.HasStarted); Assert.True(context.Response.Body.CanSeek); }); }); var server = new TestServer(builder); var response = await server.CreateClient().GetAsync(""); response.EnsureSuccessStatusCode(); Assert.Equal("Hello World", await response.Content.ReadAsStringAsync()); IEnumerable<string> values; Assert.True(response.Content.Headers.TryGetValues("Content-Length", out values)); Assert.Equal("12", values.FirstOrDefault()); }
public AuthorizeEndpointTestBase() { _mockPipeline = new MockAuthorizationPipeline(Clients, Scopes, Users); _server = TestServer.Create(null, _mockPipeline.Configure, _mockPipeline.ConfigureServices); _browser = new Browser(_server.CreateHandler()); _client = new HttpClient(_browser); }
public async void GetFallbackLanguage_ReturnsFromSupportedCulture_AcceptLanguageListContainsSupportedCultures() { var builder = new WebApplicationBuilder() .Configure(app => { app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("fr-FR"), SupportedCultures = new List<CultureInfo> { new CultureInfo("ar-SA"), new CultureInfo("en-US") } }); app.Run(context => { var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCulture = requestCultureFeature.RequestCulture; Assert.Equal("ar-SA", requestCulture.Culture.Name); return Task.FromResult(0); }); }); using (var server = new TestServer(builder)) { var client = server.CreateClient(); client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-GB,ar-SA,en-US"); var count = client.DefaultRequestHeaders.AcceptLanguage.Count; var response = await client.GetAsync(string.Empty); } }
public async Task XForwardedForOverrideBadIpDoesntChangeRemoteIp() { var assertsExecuted = false; var builder = new WebApplicationBuilder() .Configure(app => { app.UseOverrideHeaders(new OverrideHeaderOptions { ForwardedOptions = ForwardedHeaders.XForwardedFor }); app.Run(context => { Assert.Null(context.Connection.RemoteIpAddress); assertsExecuted = true; return Task.FromResult(0); }); }); var server = new TestServer(builder); var req = new HttpRequestMessage(HttpMethod.Get, ""); req.Headers.Add("X-Forwarded-For", "BAD-IP"); await server.CreateClient().SendAsync(req); Assert.True(assertsExecuted); }
public HomeTests() { var server = new TestServer(TestServer.CreateBuilder() .UseStartup<TestStartup>()); _client = server.CreateClient(); }
public ResourceOwnerClient() { var server = new TestServer(TestServer.CreateBuilder() .UseStartup<Startup>()); _handler = server.CreateHandler(); _client = server.CreateClient(); }
protected IntegrationTestBase() { ServerUrl = new Uri("http://localhost"); _runningServer = TestServer.Create(Configure, ConfigureServices); ClientConfiguration = new ClientConfiguration(); HttpMessageHandler handler = _runningServer.CreateHandler(); ClientConfiguration.HttpMessageHandler = handler; }
public IntrospectionTests() { var server = new TestServer(TestServer.CreateBuilder() .UseStartup<Startup>()); _handler = server.CreateHandler(); _client = server.CreateClient(); }
public ClientCredentialsClient() { var server = new TestServer(TestServer.CreateBuilder() .UseStartup<Startup>()); _handler = server.CreateHandler(); _client = server.CreateClient(); }
public UserInfoEndpointClient() { var server = new TestServer(TestServer.CreateBuilder() .UseStartup<Startup>()); _handler = server.CreateHandler(); _client = server.CreateClient(); }
public PictureControllerTest() { // Arrange _server = new TestServer(TestServer.CreateBuilder() .UseStartup<Startup>()); _client = _server.CreateClient(); _client.BaseAddress = new Uri("https://localhost:44371/api/Picture/"); }
public async Task CanHostMvc() { var server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>()); var client = server.CreateClient(); var res = await client.GetAsync("/api/Resources"); Assert.Equal(HttpStatusCode.OK, res.StatusCode); var body = await res.Content.ReadAsStringAsync(); Assert.Equal("Hello Web!", body); }
public BaseControllerTest() { _server = new TestServer(TestServer.CreateBuilder() .UseStartup<Startup>()); _server.BaseAddress = new Uri("http://localhost:5000"); _client = _server.CreateClient(); _serviceProvider = new ServiceCollection().BuildServiceProvider(); }
public RequestBuilder(TestServer server, string path) { if (server == null) { throw new ArgumentNullException(nameof(server)); } _server = server; _req = new HttpRequestMessage(HttpMethod.Get, path); }
public async Task ReturnsNotFoundWithoutWwwroot() { var builder = new WebApplicationBuilder() .Configure(app => app.UseStaticFiles()); var server = new TestServer(builder); var response = await server.CreateClient().GetAsync("/ranges.txt"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }
public void AddRequestHeader() { var builder = new WebApplicationBuilder().Configure(app => { }); var server = new TestServer(builder); server.CreateRequest("/") .AddHeader("Host", "MyHost:90") .And(request => { Assert.Equal("MyHost:90", request.Headers.Host.ToString()); }); }
public BaseTest() { var environment = CallContextServiceLocator.Locator.ServiceProvider.GetRequiredService<IApplicationEnvironment>(); var startup = new Startup(environment); _applicationBuilder = startup.Configure; _serviceColletion = startup.ConfigureServices; _testServer = TestServer.Create(_applicationBuilder, _serviceColletion); _httpClient = _testServer.CreateClient(); }
public TestClientProvider() { _fakeCalendarServer = TestServer.Create(app => { app.UseFakeExternalApi(); }); _fakeCalcServer = TestServer.Create(app => { app.UseFakeExternalApi(); }); }
public void AddContentHeaders() { var builder = new WebApplicationBuilder().Configure(app => { }); var server = new TestServer(builder); server.CreateRequest("/") .AddHeader("Content-Type", "Test/Value") .And(request => { Assert.NotNull(request.Content); Assert.Equal("Test/Value", request.Content.Headers.ContentType.ToString()); }); }
public ApiIdeasControllerTests() { var server = new TestServer(TestServer.CreateBuilder() .UseEnvironment("Development") .UseStartup<Startup>()); _client = server.CreateClient(); // client always expects json results _client.DefaultRequestHeaders.Clear(); _client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); }
public BoardControllerTests() { // Arrange var db = new DbContextOptionsBuilder(); db.UseInMemoryDatabase(); m_Context = new ScrumToolDB(db.Options); CreateTestData(m_Context); m_BoardController= new BoardController(m_Context); m_Server = new TestServer(TestServer.CreateBuilder() .UseStartup<Startup>()); m_Client = m_Server.CreateClient(); }
public MvcTestFixture(object startupInstance) { var startupTypeInfo = startupInstance.GetType().GetTypeInfo(); var configureApplication = (Action<IApplicationBuilder>)startupTypeInfo .DeclaredMethods .FirstOrDefault(m => m.Name == "Configure" && m.GetParameters().Length == 1) ?.CreateDelegate(typeof(Action<IApplicationBuilder>), startupInstance); if (configureApplication == null) { var configureWithLogger = (Action<IApplicationBuilder, ILoggerFactory>)startupTypeInfo .DeclaredMethods .FirstOrDefault(m => m.Name == "Configure" && m.GetParameters().Length == 2) ?.CreateDelegate(typeof(Action<IApplicationBuilder, ILoggerFactory>), startupInstance); Debug.Assert(configureWithLogger != null); configureApplication = application => configureWithLogger(application, NullLoggerFactory.Instance); } var buildServices = (Func<IServiceCollection, IServiceProvider>)startupTypeInfo .DeclaredMethods .FirstOrDefault(m => m.Name == "ConfigureServices" && m.ReturnType == typeof(IServiceProvider)) ?.CreateDelegate(typeof(Func<IServiceCollection, IServiceProvider>), startupInstance); if (buildServices == null) { var configureServices = (Action<IServiceCollection>)startupTypeInfo .DeclaredMethods .FirstOrDefault(m => m.Name == "ConfigureServices" && m.ReturnType == typeof(void)) ?.CreateDelegate(typeof(Action<IServiceCollection>), startupInstance); Debug.Assert(configureServices != null); buildServices = services => { configureServices(services); return services.BuildServiceProvider(); }; } // RequestLocalizationOptions saves the current culture when constructed, potentially changing response // localization i.e. RequestLocalizationMiddleware behavior. Ensure the saved culture // (DefaultRequestCulture) is consistent regardless of system configuration or personal preferences. using (new CultureReplacer()) { _server = TestServer.Create( CallContextServiceLocator.Locator.ServiceProvider, configureApplication, configureServices: InitializeServices(startupTypeInfo.Assembly, buildServices)); } Client = _server.CreateClient(); Client.BaseAddress = new Uri("http://localhost"); }
public async Task CustomRouting_NameSpaceRouting() { // Arrange var builder = new WebApplicationBuilder().UseStartup(typeof(NamespaceRouting.Startup)); var client = new TestServer(builder).CreateClient(); // Act var result = await client.GetAsync("MySite/Inventory/Products/List"); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); var contentString = await result.Content.ReadAsStringAsync(); Assert.Contains("Hello from ProductsController", contentString); }
public async Task RequestServicesAutoCreated() { var builder = new WebApplicationBuilder().Configure(app => { app.Run(context => { return context.Response.WriteAsync("RequestServices:" + (context.RequestServices != null)); }); }); var server = new TestServer(builder); string result = await server.CreateClient().GetStringAsync("/path"); Assert.Equal("RequestServices:True", result); }