public string GetAbsolutePath(EnvDirectory dir) { return(dir switch { EnvDirectory.ContentRootPath => _webHostEnv.ContentRootPath, EnvDirectory.WebRootPath => _webHostEnv.WebRootPath, _ => throw new InvalidOperationException($"Unknown env dir type: {dir}."), });
public void GetAbsolutePath_WithInvalidEnumValue_ShouldThrow() { EnvDirectory invalidDir = (EnvDirectory)(-1); Action testAction = () => _mapperSvc.GetAbsolutePath(invalidDir); testAction.ShouldThrow <InvalidOperationException>(); }
public void GetAbsolutePath_WithValidEnumValue_ShouldReturnPath(EnvDirectory dir) { Func <string> testFunc = () => _mapperSvc.GetAbsolutePath(dir); string path = testFunc.ShouldNotThrow(); path.ShouldNotBeNullOrEmpty(); }
public void ParametrizedConstructor_WithValidParams_ValueShouldEqualParams() { string filePath = "some.txt"; EnvDirectory baseDir = EnvDirectory.ContentRootPath; int code503RetryInterval = 2000; ResponseFromFileOption opt = new ResponseFromFileOption(filePath, baseDir, code503RetryInterval); opt.Value.ShouldNotBeNull(); opt.Value.File.Path.ShouldBe(filePath); opt.Value.File.BaseDir.ShouldBe(baseDir); opt.Value.Code503RetryInterval.ShouldBe(code503RetryInterval); }
private void SetValue(string filePath, EnvDirectory baseDir, int code503RetryInterval) { if (!TryGetContentType(filePath, out _)) { string fileName = Path.GetFileName(filePath); throw new ArgumentException($"The file, specified in {filePath} must have one of the following extensions: .txt, .html or .json."); } Value = new FileMaintenanceResponse { File = new FileDescriptor(filePath, baseDir), Code503RetryInterval = code503RetryInterval }; }
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 IMiddlewareOptionsBuilder UseResponseFromFile(string relativePath, EnvDirectory baseDir, int code503RetryInterval = DefaultValues.DEFAULT_503_RETRY_INTERVAL) { if (string.IsNullOrEmpty(relativePath)) { throw new ArgumentNullException(nameof(relativePath)); } string envDir = _dirMapperSvc.GetAbsolutePath(baseDir); string fullFilePath = Path.Combine(envDir, relativePath); if (!File.Exists(fullFilePath)) { throw new FileNotFoundException($"Could not find file {relativePath}. Expected absolute path: {fullFilePath}."); } _options.Add(new ResponseFromFileOption(relativePath, baseDir, code503RetryInterval)); return(this); }
public void LoadFromString_WithVariousEnvDirectory_EnvDirectoryInValueShouldMatchInput(string input, EnvDirectory baseDir, bool shouldBeEqual) { var option = new ResponseFromFileOption(); option.LoadFromString(input); bool isDirEqual = option.Value.File.BaseDir == baseDir; isDirEqual.ShouldBe(shouldBeEqual); }
internal ResponseFromFileOption(string filePath, EnvDirectory baseDir, int code503RetryInterval) { SetValue(filePath, baseDir, code503RetryInterval); }
public async void Invoke_WithUseResponseFile_ResponseShouldBeAppropriate(string fileName, string expectedContentType, EnvDirectory baseDir) { string tempDir = Path.GetTempPath(); string rootDir; if (baseDir == EnvDirectory.ContentRootPath) { rootDir = Path.Combine(tempDir, "contentRoot"); } else { rootDir = Path.Combine(tempDir, "wwwRoot"); } string safeTempFileName = SafeTempPath.Create(fileName); File.WriteAllText(Path.Combine(rootDir, safeTempFileName), "test"); MiddlewareTestDesk desk = GetTestDesk( (httpContext) => { }, (optionBuilder) => { optionBuilder.UseResponseFromFile(safeTempFileName, baseDir); }, null, tempDir); await desk.MiddlewareInstance .Invoke(desk.CurrentHttpContext); desk.CurrentHttpContext.Response.StatusCode .ShouldBe(503); desk.CurrentHttpContext.Response.Headers .Any(h => h.Key == "Retry-After") .ShouldBeTrue(); desk.CurrentHttpContext.Response.ContentType .ShouldBe(expectedContentType); GetResponseString(desk.CurrentHttpContext) .ShouldBe("test"); }
public void SecondConstructor_WithValidArgs_ValuesShouldEqualInput(string path, EnvDirectory envDir) { var fileDescriptor = new FileDescriptor(path, envDir); fileDescriptor.Path .ShouldBe(path); fileDescriptor.BaseDir .ShouldBe(envDir); }