private OptionCollection GetStartupOptions(Action <MiddlewareOptionsBuilder> builderDelegate) { var optionsBuilder = new MiddlewareOptionsBuilder(_dirMapperSvc); builderDelegate?.Invoke(optionsBuilder); return(optionsBuilder.GetOptions()); }
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(); }
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); } } }
public void FillEmptyOptionsWithDefault_Default_ShouldAddOptions() { MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc); builder.GetOptions().ShouldNotBeNull() .GetAll().ShouldNotBeEmpty(); }
private OptionCollection GetMiddlewareOptions(Action <MiddlewareOptionsBuilder> middlewareOptionsBuilder) { var optionsBuilder = new MiddlewareOptionsBuilder(_dirMapperSvc); middlewareOptionsBuilder?.Invoke(optionsBuilder); return(optionsBuilder.GetOptions()); }
public void Constructor_Default_ShouldAddDefaultOptions() { MiddlewareOptionsBuilder builder = null; builder = new MiddlewareOptionsBuilder(_dirMapperSvc); builder.GetOptions() .ShouldNotBeNull(); }
public void BypassFileExtensions_WithNullOrEmptyArray_ShouldThrow(string[] extensions, Type expectedException) { MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc); Action testAction = () => { builder.BypassFileExtensions(extensions); }; testAction.ShouldThrow(expectedException); }
public void BypassUrlPath_WithEmptyPath_ShouldThrowArgumentException() { MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc); Action testAction = () => { builder.BypassUrlPath(new PathString(), StringComparison.Ordinal);; }; testAction.ShouldThrow <ArgumentException>(); }
public void BypassAllAuthenticatedUsers_Default_ShouldSucceed() { MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc); builder.BypassAllAuthenticatedUsers(); builder.GetOptions() .GetSingleOrDefault <BypassAllAuthenticatedUsersOption>() .ShouldNotBeNull(); }
public void BypassUserRole_WithNullOrEmptyRole_ShouldThrow(string userRole, Type expectedException) { MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc); Action testAction = () => { builder.BypassUserRole(userRole); }; testAction.ShouldThrow(expectedException); }
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."); }
public void MiddlewareOptionsBuilder_WhenGetOptionsIsCalledTwice_ShouldNotThrow() { MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc); Action testAction = () => { builder.GetOptions(); builder.GetOptions(); }; testAction.ShouldNotThrow(); }
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(); }
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 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(); }
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); }
public void BypassUser_WithUser_ShouldSucceed() { const string userName = "******"; MiddlewareOptionsBuilder builder = new MiddlewareOptionsBuilder(_dirMapperSvc); builder.BypassUser(userName); builder .GetOptions() .GetSingleOrDefault <BypassUserNameOption>() .ShouldNotBeNull() .Value.ShouldBe(userName); }
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); }
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); }
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); }
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); }
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"); }
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); }
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); }
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); }
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); }