コード例 #1
0
        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");
            }
        }
コード例 #2
0
 public PrimeWebCheckPrimeShould()
 {
     // Arrange
     _server = new TestServer(TestServer.CreateBuilder()
         .UseStartup<Startup>());
     _client = _server.CreateClient();
 }
コード例 #3
0
        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");
            }
        }
コード例 #4
0
 protected DatabaseTestBase()
 {
     // Arrange
     Server = new TestServer(
         TestServer.CreateBuilder()
             .UseStartup<TestStartup>());
 }
コード例 #5
0
ファイル: TestServerTests.cs プロジェクト: leloulight/Hosting
 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);
 }
コード例 #6
0
        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());
            }
        }
コード例 #7
0
        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());
        }
コード例 #8
0
 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);
            }
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        public HomeTests()
        {
            var server = new TestServer(TestServer.CreateBuilder()
                .UseStartup<TestStartup>());

            _client = server.CreateClient();
        }
コード例 #12
0
        public ResourceOwnerClient()
        {
            var server = new TestServer(TestServer.CreateBuilder()
                                .UseStartup<Startup>());

            _handler = server.CreateHandler();
            _client = server.CreateClient();
        }
コード例 #13
0
ファイル: IntegrationTestBase.cs プロジェクト: geffzhang/Bolt
 protected IntegrationTestBase()
 {
     ServerUrl = new Uri("http://localhost");
     _runningServer = TestServer.Create(Configure, ConfigureServices);
     ClientConfiguration = new ClientConfiguration();
     HttpMessageHandler handler = _runningServer.CreateHandler();
     ClientConfiguration.HttpMessageHandler = handler;
 }
コード例 #14
0
        public IntrospectionTests()
        {
            var server = new TestServer(TestServer.CreateBuilder()
                                .UseStartup<Startup>());

            _handler = server.CreateHandler();
            _client = server.CreateClient();
        }
コード例 #15
0
        public ClientCredentialsClient()
        {
            var server = new TestServer(TestServer.CreateBuilder()
                                .UseStartup<Startup>());

            _handler = server.CreateHandler();
            _client = server.CreateClient();
        }
コード例 #16
0
        public UserInfoEndpointClient()
        {
            var server = new TestServer(TestServer.CreateBuilder()
                                .UseStartup<Startup>());

            _handler = server.CreateHandler();
            _client = server.CreateClient();
        }
コード例 #17
0
 public PictureControllerTest()
 {
     // Arrange
     _server = new TestServer(TestServer.CreateBuilder()
         .UseStartup<Startup>());
     _client = _server.CreateClient();
     _client.BaseAddress = new Uri("https://localhost:44371/api/Picture/");
 }
コード例 #18
0
 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);
 }
コード例 #19
0
        public BaseControllerTest()
        {
            _server = new TestServer(TestServer.CreateBuilder()
                .UseStartup<Startup>());
            _server.BaseAddress = new Uri("http://localhost:5000");
            _client = _server.CreateClient();

            _serviceProvider = new ServiceCollection().BuildServiceProvider();
        }
コード例 #20
0
ファイル: RequestBuilder.cs プロジェクト: yonglehou/Hosting
        public RequestBuilder(TestServer server, string path)
        {
            if (server == null)
            {
                throw new ArgumentNullException(nameof(server));
            }

            _server = server;
            _req = new HttpRequestMessage(HttpMethod.Get, path);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
 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());
         });
 }
コード例 #23
0
ファイル: BaseTest.cs プロジェクト: agcavaleiro/SlnFortes
        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();
        }
コード例 #24
0
        public TestClientProvider()
        {
            _fakeCalendarServer = TestServer.Create(app =>
            {
                app.UseFakeExternalApi();
            });

            _fakeCalcServer = TestServer.Create(app =>
            {
                app.UseFakeExternalApi();
            });
        }
コード例 #25
0
 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());
         });
 }
コード例 #26
0
        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"));

        }
コード例 #27
0
        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();
        }
コード例 #28
0
ファイル: MvcTestFixture.cs プロジェクト: 4myBenefits/Mvc
        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");
        }
コード例 #29
0
        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);
        }
コード例 #30
0
ファイル: TestServerTests.cs プロジェクト: leloulight/Hosting
        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);
        }