Exemplo n.º 1
0
        public void GetAbsolutePath_WithValidEnumValue_ShouldReturnPath(EnvDirectory dir)
        {
            Func <string> testFunc = () => _mapperSvc.GetAbsolutePath(dir);

            string path = testFunc.ShouldNotThrow();

            path.ShouldNotBeNullOrEmpty();
        }
Exemplo n.º 2
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");
        }
Exemplo n.º 3
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.º 4
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);
        }
        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);
        }
Exemplo n.º 6
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));
            }
        }
        private string GetFileFullPath()
        {
            string envPath = _dirMapperSvc.GetAbsolutePath(File.BaseDir.Value);

            return(IO.Path.Combine(envPath, File.Path));
        }