public async Task ActionsShouldExecuteInOrder()
        {
            var contextBag = ApFrameworkHelper.GetEmptyBag();

            contextBag.Add("testObj", new TestArguments());

            var request   = new Request <string>(ProcessRequest);
            var processor = new RequestProcessor();

            processor.AddAddtionalAction(Last, AddtionalActionSequence.Last);
            processor.AddAddtionalAction(First, AddtionalActionSequence.First); // 0
            processor.AddAddtionalAction(BeforeAuth, AddtionalActionSequence.BeforeAuthorization);
            processor.AddAddtionalAction(AfterAuth, AddtionalActionSequence.AfterAuthorization);
            processor.AddAddtionalAction(BeforeVal, AddtionalActionSequence.BeforeValidation);
            processor.AddAddtionalAction(AfterVal, AddtionalActionSequence.AfterValidation);
            processor.AddAddtionalAction(BeforeProcess, AddtionalActionSequence.BeforeRequestProcessing);
            processor.AddAddtionalAction(AfterProcess, AddtionalActionSequence.AfterRequestProcessing);


            await processor.ProcessRequestAsync(request, contextBag);

            var updatedObj = contextBag["testObj"] as TestArguments;

            for (int i = 0; i < 9; i++)
            {
                Assert.True(updatedObj.Logs[i] == i.ToString());
            }
        }
        public async Task VerifyComponentsHookUpCorrectly()
        {
            IServiceCollection services = new ServiceCollection();

            services.AddTransient <IRequestProcessor, RequestProcessor>();

            var serviceProvider = services.BuildServiceProvider();

            string logKey = "TestLog";
            // Request processing
            ProcessRequestDelegate <string> process =
                async(request, contextBag, cancellationToken) =>
            {
                var log = contextBag[logKey] as StringBuilder;
                log.Append("Processed");
                return(string.Empty);
            };

            ValidateRequestDelegate validator = async(request, contextBag, cancellationToken) =>
            {
                var log = contextBag[logKey] as StringBuilder;
                log.Append("Validated>>");
                return(RequestValidationResult.Instance());
            };

            AuthorizeRequestDelegate authorizer = async(request, contextBag, cancellationToken) =>
            {
                var log = contextBag[logKey] as StringBuilder;
                log.Append("Authorized>>");
                return(new RequestAuthorizationResult());
            };

            var contextBag = ApFrameworkHelper.GetEmptyBag();

            contextBag[logKey] = new StringBuilder();

            var expected = "Authorized>>Validated>>Processed";

            var request   = new Request <string>(process, validator, authorizer);
            var processor = serviceProvider.GetService <IRequestProcessor>();

            await processor.ProcessRequestAsync(request, contextBag);

            var log = contextBag[logKey] as StringBuilder;

            Assert.True(log.ToString() == expected);
        }
Esempio n. 3
0
        public async Task HookUpPerformanceLoggingGlobally()
        {
            IServiceCollection services = new ServiceCollection();


            services.AddTransient <IRequestProcessor, RequestProcessor>();

            // add a property to request to represent if it is a child request. Thogh i dont need it.
            ApFrameworkHelper.AddApFrameworkGloablAction(StartPerformanceMonitoring, AddtionalActionSequence.First);
            ApFrameworkHelper.AddApFrameworkGloablAction(StopPerformanceMonitoring, AddtionalActionSequence.Last);

            var contextBag      = ApFrameworkHelper.GetEmptyBag();
            var serviceProvider = services.BuildServiceProvider();

            var request      = new Request <string>(null);
            var reqProcessor = serviceProvider.GetService <IRequestProcessor>();
            await reqProcessor.ProcessRequestAsync(request, contextBag);

            Assert.False(string.IsNullOrWhiteSpace(contextBag["Perf_log"].ToString()));
        }
        public async Task StopExectionIfCancellationIsRequested()
        {
            var contextBag = ApFrameworkHelper.GetEmptyBag();

            var testArgs = new TestArguments();
            CancellationTokenSource source = new CancellationTokenSource();

            testArgs.CancellationTokenSource  = source;
            testArgs.CancellationMatchStrinig = "2";


            contextBag.Add("testObj", testArgs);

            var request   = new Request <string>(ProcessRequest);
            var processor = new RequestProcessor();

            processor.AddAddtionalAction(Last, AddtionalActionSequence.Last);
            processor.AddAddtionalAction(First, AddtionalActionSequence.First); // 0
            processor.AddAddtionalAction(BeforeAuth, AddtionalActionSequence.BeforeAuthorization);
            processor.AddAddtionalAction(AfterAuth, AddtionalActionSequence.AfterAuthorization);
            processor.AddAddtionalAction(BeforeVal, AddtionalActionSequence.BeforeValidation);
            processor.AddAddtionalAction(AfterVal, AddtionalActionSequence.AfterValidation);
            processor.AddAddtionalAction(BeforeProcess, AddtionalActionSequence.BeforeRequestProcessing);
            processor.AddAddtionalAction(AfterProcess, AddtionalActionSequence.AfterRequestProcessing);


            await processor.ProcessRequestAsync(request, contextBag, source.Token);

            var updatedObj = contextBag["testObj"] as TestArguments;

            for (int i = 0; i < 3; i++)
            {
                Assert.True(updatedObj.Logs[i] == i.ToString());
            }

            for (int i = 3; i < 9; i++)
            {
                Assert.True(string.IsNullOrEmpty(updatedObj.Logs[i]));
            }
        }