Exemplo n.º 1
0
        public void GetState_AfterStoredState_ShouldRestoreSameState()
        {
            // store
            IStateStoreService       stateStoreSvc1 = new StateStoreService(new InMemoryStateStore());
            IDirectoryMapperService  dirMapperSvc   = FakeDirectoryMapperService.Create();
            MiddlewareOptionsBuilder builder        = new MiddlewareOptionsBuilder(dirMapperSvc);

            builder.BypassAllAuthenticatedUsers();

            stateStoreSvc1.SetState(new MaintenanceState(null, isMaintenanceOn: true, builder.GetOptions()));

            // restore
            IStateStoreService stateStoreSvc2 = new StateStoreService(new InMemoryStateStore());

            Func <MaintenanceState> testFunc = () => stateStoreSvc2.GetState();

            MaintenanceState state = testFunc.ShouldNotThrow()
                                     .ShouldNotBeNull();
            IMiddlewareOptionsContainer optionsContainer = state;

            optionsContainer.MiddlewareOptions
            .ShouldNotBeNull()
            .Any <BypassAllAuthenticatedUsersOption>()
            .ShouldBeTrue();
        }
Exemplo n.º 2
0
 public MaintenanceControlService(
     IDirectoryMapperService dirMapperSvc,
     IStateStoreService stateStoreService)
 {
     _dirMapperSvc      = dirMapperSvc;
     _stateStoreService = stateStoreService;
 }
Exemplo n.º 3
0
        public void Constructor_WithResponseFromFileOptionWhenFileIsMissing_ShouldThrowFileNotFoundException()
        {
            const string        testFileNameCaseNotExists = "nonexistent_response_option_file.txt";
            string              tempPath   = Path.GetTempPath();
            IWebHostEnvironment webHostEnv = Substitute.For <IWebHostEnvironment>();

            webHostEnv.ContentRootPath.Returns(tempPath);
            IDirectoryMapperService pathMappingSvc = Substitute.For <IDirectoryMapperService>();

            pathMappingSvc.GetAbsolutePath(Arg.Any <EnvDirectory>()).Returns(tempPath);


            Action <IMiddlewareOptionsBuilder> optionBuilderDelegate = (options) =>
            {
                options.UseResponseFromFile(testFileNameCaseNotExists, EnvDirectory.ContentRootPath);
                // prevent other exceptions due to missing required options
                ((MiddlewareOptionsBuilder)options).FillEmptyOptionsWithDefault();
            };

            Action testAction = () =>
                                new MaintenanceMiddleware(null,
                                                          null,
                                                          pathMappingSvc,
                                                          optionBuilderDelegate);


            testAction.ShouldThrow <FileNotFoundException>()
            .Message.ShouldStartWith("Could not find file");
        }
        public FileStateStore(IDirectoryMapperService dirMapperService)
        {
            _dirMapperSvc = dirMapperService;

            File = new FileDescriptor("maintenanceState.json",
                                      EnvDirectory.ContentRootPath);
        }
Exemplo n.º 5
0
        private string GetFileFullPath(IDirectoryMapperService dirMapperSvc)
        {
            string envDir = dirMapperSvc.GetAbsolutePath(Value.File.BaseDir.Value);

            string absPath = Path.Combine(envDir, Value.File.Path);

            return(absPath);
        }
Exemplo n.º 6
0
        public MaintenanceMiddleware(RequestDelegate next,
                                     IMaintenanceControlService maintenanceCtrlSvc,
                                     IDirectoryMapperService dirMapperSvc,
                                     Action <IMiddlewareOptionsBuilder> optionsBuilderDelegate)
        {
            _next = next;
            _maintenanceCtrlSvc = maintenanceCtrlSvc;
            _dirMapperSvc       = dirMapperSvc;

            _startupOptions = GetStartupOptions(optionsBuilderDelegate);
        }
Exemplo n.º 7
0
        public void SetState_WithValidState_ShouldNotThrow()
        {
            IStateStoreService       stateStoreSvc = new StateStoreService(new InMemoryStateStore());
            IDirectoryMapperService  dirMapperSvc  = FakeDirectoryMapperService.Create();
            MiddlewareOptionsBuilder builder       = new MiddlewareOptionsBuilder(dirMapperSvc);

            builder.BypassAllAuthenticatedUsers();

            Action testAction = () => stateStoreSvc.SetState(new MaintenanceState(null, isMaintenanceOn: true, builder.GetOptions()));

            testAction.ShouldNotThrow();
        }
Exemplo n.º 8
0
        private MiddlewareTestDesk GetTestDesk(
            Action <HttpContext> contextSetup,
            Action <IMiddlewareOptionsBuilder> optionsSetup,
            Action <IMiddlewareOptionsBuilder> optionsOverrideSetup = null,
            string tempDir = null)
        {
            DefaultHttpContext httpContext = new DefaultHttpContext();

            httpContext.Response.Body = new MemoryStream();

            contextSetup(httpContext);

            bool            isNextDelegateCalled = false;
            RequestDelegate nextDelegate         = (HttpContext hc) =>
            {
                isNextDelegateCalled = true;
                return(Task.CompletedTask);
            };

            if (tempDir == null)
            {
                tempDir = Path.GetTempPath();
            }

            IDirectoryMapperService dirMapperSvc = FakeDirectoryMapperService.Create(tempDir);

            OptionCollection middlewareOptions = null;

            if (optionsOverrideSetup != null)
            {
                MiddlewareOptionsBuilder optionOverrideBuilder = new MiddlewareOptionsBuilder(dirMapperSvc);
                optionsOverrideSetup.Invoke(optionOverrideBuilder);
                middlewareOptions = optionOverrideBuilder.GetOptions();
            }

            IMaintenanceControlService svc = Substitute.For <IMaintenanceControlService>();

            svc.GetState().Returns(new MaintenanceState(null, isMaintenanceOn: true, middlewareOptions));

            MaintenanceMiddleware middleware = new MaintenanceMiddleware(
                nextDelegate,
                svc,
                dirMapperSvc,
                optionsSetup);

            return(new MiddlewareTestDesk
            {
                CurrentHttpContext = httpContext,
                IsNextDelegateCalled = isNextDelegateCalled,
                MiddlewareInstance = middleware
            });
        }
 public MaintenanceResponse GetResponse(IDirectoryMapperService dirMapperSvc)
 {
     using (Stream resStream = GetType()
                               .Assembly
                               .GetManifestResourceStream($"{nameof(MaintenanceModeMiddleware)}.Resources.DefaultResponse.html"))
     {
         using var resSr = new StreamReader(resStream, Encoding.UTF8);
         return(new MaintenanceResponse
         {
             ContentBytes = resSr.CurrentEncoding.GetBytes(resSr.ReadToEnd()),
             ContentEncoding = resSr.CurrentEncoding,
             ContentType = ResponseContentType.Html,
             Code503RetryInterval = DEFAULT_503_RETRY_INTERVAL
         });
     }
 }
Exemplo n.º 10
0
        public MaintenanceResponse GetResponse(IDirectoryMapperService dirMapperSvc)
        {
            string fullPath = GetFileFullPath(dirMapperSvc);

            using (StreamReader sr = new StreamReader(fullPath,
                                                      detectEncodingFromByteOrderMarks: true))
            {
                TryGetContentType(fullPath, out ResponseContentType? contentType);

                return(new MaintenanceResponse
                {
                    ContentBytes = sr.CurrentEncoding.GetBytes(sr.ReadToEnd()),
                    ContentEncoding = sr.CurrentEncoding,
                    ContentType = contentType.Value,
                    Code503RetryInterval = Value.Code503RetryInterval
                });
            }
        }
Exemplo n.º 11
0
        public void Constructor_WithResponseFromFileOptionWhenFileExists_ShouldNotThrow()
        {
            const string        testFileNameCaseExists = "test_response_option_file_exists.txt";
            string              tempPath   = Path.GetTempPath();
            IWebHostEnvironment webHostEnv = Substitute.For <IWebHostEnvironment>();

            webHostEnv.ContentRootPath.Returns(tempPath);
            IDirectoryMapperService pathMappingSvc = Substitute.For <IDirectoryMapperService>();

            pathMappingSvc.GetAbsolutePath(Arg.Any <EnvDirectory>()).Returns(tempPath);

            File.Create(Path.Combine(webHostEnv.ContentRootPath, testFileNameCaseExists))
            .Dispose();

            try
            {
                Action <IMiddlewareOptionsBuilder> optionBuilderDelegate = (options) =>
                {
                    options.UseResponseFromFile(testFileNameCaseExists, EnvDirectory.ContentRootPath);
                    // prevent other exceptions due to missing required options
                    ((MiddlewareOptionsBuilder)options).FillEmptyOptionsWithDefault();
                };

                Action testAction = () =>
                                    new MaintenanceMiddleware(null,
                                                              null,
                                                              pathMappingSvc,
                                                              optionBuilderDelegate);

                testAction.ShouldNotThrow();
            }
            finally
            {
                File.Delete(Path.Combine(webHostEnv.ContentRootPath, testFileNameCaseExists));
            }
        }
 public MaintenanceResponse GetResponse(IDirectoryMapperService dirMapperSvc)
 {
     return(Value);
 }
 internal MiddlewareOptionsBuilder(IDirectoryMapperService dirMapperSvc)
 {
     _options      = new OptionCollection();
     _dirMapperSvc = dirMapperSvc;
 }
Exemplo n.º 14
0
        public DirectoryMapperServiceTest()
        {
            IWebHostEnvironment webHostEnv = FakeWebHostEnvironment.Create();

            _mapperSvc = new DirectoryMapperService(webHostEnv);
        }