Exemplo n.º 1
0
        public static async Task SignOut(StartupServer server, string cookies)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, $"{TestServerHelper.BaseUrl}/Auth/Signout");
            if (!string.IsNullOrEmpty(cookies))
            {
                request.Headers.Add("Cookie", cookies);
            }

            await server.CreateClient().SendAsync(request);
        }
Exemplo n.º 2
0
        public static async Task<string> SignIn(StartupServer server, AccountingUser user, string password = null)
        {
            if (password == null)
            {
                password = testPassword;
            }

            var signinResult = await Signin(server, user.UserName, password);
            return signinResult.GenerateCookieHeader();
        }
Exemplo n.º 3
0
        public static StartupServer Create(Uri baseAddress = null)
        {
            var appEnv = CallContextServiceLocator.Locator.ServiceProvider.GetRequiredService<IApplicationEnvironment>();

            var config = new ConfigurationBuilder();
            var configDict = new Dictionary<string, string>();
            configDict["Hosting:Environment"] = "Test";
            config.AddInMemoryCollection(configDict.AsEnumerable());
            var conf = config.Build();

            var env = new HostingEnvironment();
            env.Initialize($"{appEnv.ApplicationBasePath}/../../src/Accounting", conf);
            var startup = new Accounting.Startup(appEnv, env);

            var setup = new SetupObject();

            Action<IApplicationBuilder> buildApp = (app) =>
            {
                app.Use(async (context, next) =>
                {
                    var req = context.Request;
                        
                    if (req.Path == testSetupPathString && setup.Setup != null)
                    {
                        await setup.Setup(context);
                    }
                    else
                    {
                        await next();
                    }
                });
                startup.Configure(app);
            };

            Action<IServiceCollection> buildServices = (services) =>
            {
                startup.ConfigureServices(services);
                services.Remove(services.Where(sp => sp.ServiceType == typeof(IConfigureOptions<AntiforgeryOptions>)).First());
                services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<AntiforgeryOptions>, TestAntiforgeryOptionsSetup>());
                services.AddSingleton<IAntiforgeryTokenSerializer, TestAntiforgeryTokenSerializer>();
                services.AddSingleton<IAntiforgeryTokenGenerator, TestAntiforgeryTokenGenerator>();
            };

            StartupServer server = new StartupServer(CreateBuilder(conf, app => buildApp(app), services => buildServices(services)));
            server.setupObject = setup;
            server.BaseAddress = baseAddress;
            return server;
        }