Example #1
0
        private OptionCollection GetStartupOptions(Action <MiddlewareOptionsBuilder> builderDelegate)
        {
            var optionsBuilder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builderDelegate?.Invoke(optionsBuilder);
            return(optionsBuilder.GetOptions());
        }
Example #2
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();
        }
Example #3
0
        public void BypassUrlPaths(PathString[] pathStrings, Type expectedException)
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);
            Func <IEnumerable <BypassUrlPathOption> > testFunc = () =>
            {
                builder.BypassUrlPaths(pathStrings, StringComparison.Ordinal);
                return(builder.GetOptions().GetAll <BypassUrlPathOption>());
            };

            if (expectedException != null)
            {
                testFunc.ShouldThrow(expectedException);
            }
            else
            {
                IEnumerable <BypassUrlPathOption> options = testFunc
                                                            .ShouldNotThrow()
                                                            .ShouldNotBeNull();

                if (pathStrings != null)
                {
                    options
                    .Count()
                    .ShouldBe(pathStrings.Length);
                }
            }
        }
Example #4
0
        public void FillEmptyOptionsWithDefault_Default_ShouldAddOptions()
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.GetOptions().ShouldNotBeNull()
            .GetAll().ShouldNotBeEmpty();
        }
Example #5
0
        private OptionCollection GetMiddlewareOptions(Action <MiddlewareOptionsBuilder> middlewareOptionsBuilder)
        {
            var optionsBuilder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            middlewareOptionsBuilder?.Invoke(optionsBuilder);

            return(optionsBuilder.GetOptions());
        }
Example #6
0
        public void Constructor_Default_ShouldAddDefaultOptions()
        {
            MiddlewareOptionsBuilder builder = null;

            builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.GetOptions()
            .ShouldNotBeNull();
        }
Example #7
0
        public void BypassFileExtensions_WithNullOrEmptyArray_ShouldThrow(string[] extensions, Type expectedException)
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);
            Action testAction = () =>
            {
                builder.BypassFileExtensions(extensions);
            };

            testAction.ShouldThrow(expectedException);
        }
Example #8
0
        public void BypassUrlPath_WithEmptyPath_ShouldThrowArgumentException()
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);
            Action testAction = () =>
            {
                builder.BypassUrlPath(new PathString(), StringComparison.Ordinal);;
            };

            testAction.ShouldThrow <ArgumentException>();
        }
Example #9
0
        public void BypassAllAuthenticatedUsers_Default_ShouldSucceed()
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.BypassAllAuthenticatedUsers();

            builder.GetOptions()
            .GetSingleOrDefault <BypassAllAuthenticatedUsersOption>()
            .ShouldNotBeNull();
        }
Example #10
0
        public void BypassUserRole_WithNullOrEmptyRole_ShouldThrow(string userRole, Type expectedException)
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);
            Action testAction = () =>
            {
                builder.BypassUserRole(userRole);
            };

            testAction.ShouldThrow(expectedException);
        }
Example #11
0
        public void UseNoDefaultValues_Default_GetOptionsShouldThrow()
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.UseNoDefaultValues();
            Action assertAction = () => builder.GetOptions();

            assertAction.ShouldThrow <ArgumentException>()
            .Message.ShouldStartWith("No response was specified.");
        }
Example #12
0
        public void MiddlewareOptionsBuilder_WhenGetOptionsIsCalledTwice_ShouldNotThrow()
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);
            Action testAction = () =>
            {
                builder.GetOptions();
                builder.GetOptions();
            };

            testAction.ShouldNotThrow();
        }
Example #13
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();
        }
Example #14
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
            });
        }
Example #15
0
        public void UseDefaultResponse_WhenCalled_ShouldSucceed()
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            // ensure that the UseDefaultResponse option is used
            builder.UseNoDefaultValues();

            builder.UseDefaultResponse();

            builder.GetOptions()
            .GetSingleOrDefault <UseDefaultResponseOption>()
            .ShouldNotBeNull();
        }
Example #16
0
        public void BypassUrlPath_WithNonEmptyPath_ValueShouldEqualInput()
        {
            const string             urlPath = "/path";
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.BypassUrlPath(new PathString(urlPath), StringComparison.Ordinal);

            builder.GetOptions()
            .GetSingleOrDefault <BypassUrlPathOption>()
            .ShouldNotBeNull()
            .Value.PathString.Value
            .ShouldBe(urlPath);
        }
Example #17
0
        public void BypassUser_WithUser_ShouldSucceed()
        {
            const string             userName = "******";
            MiddlewareOptionsBuilder builder  = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.BypassUser(userName);

            builder
            .GetOptions()
            .GetSingleOrDefault <BypassUserNameOption>()
            .ShouldNotBeNull()
            .Value.ShouldBe(userName);
        }
Example #18
0
        public void BypassUserRole_WithRole_ValueShouldBeEqualToTheInput()
        {
            const string             userRole = "role1";
            MiddlewareOptionsBuilder builder  = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.BypassUserRole(userRole);

            BypassUserRoleOption option = builder.GetOptions()
                                          .GetSingleOrDefault <BypassUserRoleOption>();

            option
            .ShouldNotBeNull()
            .Value.ShouldBe(userRole);
        }
Example #19
0
        public void BypassFileExtension_WithValidExtension_ValueShouldEqualInput(string extension)
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.BypassFileExtension(extension);


            builder.GetOptions()
            .GetSingleOrDefault <BypassFileExtensionOption>()
            .ShouldNotBeNull()
            // the method BypassFileExtension removes the dots of the extensions
            .Value.ShouldBe(extension.StartsWith('.')
                    ? extension.Substring(1)
                    : extension);
        }
Example #20
0
        public void UseResponseStringOverload_WithInvalidData_ShouldThrow(string response,
                                                                          ResponseContentType contentType,
                                                                          int codePage,
                                                                          Type expectedException)
        {
            Encoding encoding   = Encoding.GetEncoding(codePage);
            var      builder    = new MiddlewareOptionsBuilder(_dirMapperSvc);
            Action   testAction = () =>
            {
                builder.UseResponse(response, contentType, encoding);
            };


            testAction.ShouldThrow(expectedException);
        }
Example #21
0
        public void BypassFileExtensions_WithValidExtensions_ShouldSucceed()
        {
            string[] extensions = new string[] { "txt", "html" };
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.BypassFileExtensions(extensions);

            IEnumerable <BypassFileExtensionOption> options =
                builder.GetOptions().GetAll <BypassFileExtensionOption>();

            options.ShouldNotBeEmpty();
            options.Count()
            .ShouldBe(extensions.Length);
            extensions
            .ShouldContain(options.First().Value);
        }
Example #22
0
        public void MiddlewareOptionsBuilder_WhenDuplicateResponseOptionIsSet_GetOptionShouldThrow()
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.UseNoDefaultValues();
            builder.UseDefaultResponse();
            builder.UseResponse(Encoding.UTF8.GetBytes("test"), ResponseContentType.Text, Encoding.UTF8);

            Action testAction = () =>
            {
                builder.GetOptions();
            };

            testAction.ShouldThrow <ArgumentException>()
            .Message.ShouldStartWith("More than one response");
        }
Example #23
0
        public void BypassUserRoles_WithRoles_ShouldSucceed()
        {
            string[] userRoles = new string[] { "role1", "role2" };
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            builder.BypassUserRoles(userRoles);

            IEnumerable <BypassUserRoleOption> options = builder.GetOptions()
                                                         .GetAll <BypassUserRoleOption>();

            options
            .ShouldNotBeNull()
            .Count()
            .ShouldBe(userRoles.Count());
            userRoles
            .ShouldContain(options.First().Value);
        }
Example #24
0
        public void UseResponseFile_WithValidData_GetOptionsValueShouldEqualInput(string relativePath,
                                                                                  EnvDirectory baseDir)
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);
            string filePath = Path.Combine(_dirMapperSvc.GetAbsolutePath(baseDir), relativePath);

            File.Create(filePath);

            builder.UseResponseFromFile(relativePath, baseDir);

            ResponseFromFileOption option = builder
                                            .GetOptions()
                                            .GetSingleOrDefault <ResponseFromFileOption>();

            option.Value.ShouldNotBeNull();
            option.Value.File.Path.ShouldBe(relativePath);
            option.Value.File.BaseDir.ShouldBe(baseDir);
        }
Example #25
0
        public void UseResponseFile_WithInvalidData_ShouldThrow(string relativePath,
                                                                EnvDirectory baseDir,
                                                                bool createFile,
                                                                Type expectedException)
        {
            MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc);

            if (createFile)
            {
                string filePath = Path.Combine(_dirMapperSvc.GetAbsolutePath(baseDir), relativePath);
                File.Create(filePath);
            }
            Action testAction = () =>
            {
                builder.UseResponseFromFile(relativePath, baseDir);
            };

            testAction.ShouldThrow(expectedException);
        }
Example #26
0
        public void UseResponseStringOverload_WithValidData_ValueShouldEqualInput(string response,
                                                                                  ResponseContentType contentType,
                                                                                  int codePage)
        {
            Encoding encoding = Encoding.GetEncoding(codePage);
            var      builder  = new MiddlewareOptionsBuilder(_dirMapperSvc);


            builder.UseResponse(response, contentType, encoding);


            var option = builder
                         .GetOptions()
                         .GetSingleOrDefault <ResponseOption>();

            option.Value.ShouldNotBeNull();
            option.Value.ContentBytes.ShouldBe(encoding.GetBytes(response));
            option.Value.ContentType.ShouldBe(contentType);
            option.Value.ContentEncoding.ShouldBe(encoding);
        }