コード例 #1
0
 public string GetAbsolutePath(EnvDirectory dir)
 {
     return(dir switch
     {
         EnvDirectory.ContentRootPath => _webHostEnv.ContentRootPath,
         EnvDirectory.WebRootPath => _webHostEnv.WebRootPath,
         _ => throw new InvalidOperationException($"Unknown env dir type: {dir}."),
     });
コード例 #2
0
        public void GetAbsolutePath_WithInvalidEnumValue_ShouldThrow()
        {
            EnvDirectory invalidDir = (EnvDirectory)(-1);

            Action testAction = () => _mapperSvc.GetAbsolutePath(invalidDir);

            testAction.ShouldThrow <InvalidOperationException>();
        }
コード例 #3
0
        public void GetAbsolutePath_WithValidEnumValue_ShouldReturnPath(EnvDirectory dir)
        {
            Func <string> testFunc = () => _mapperSvc.GetAbsolutePath(dir);

            string path = testFunc.ShouldNotThrow();

            path.ShouldNotBeNullOrEmpty();
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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
            };
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
 internal ResponseFromFileOption(string filePath, EnvDirectory baseDir, int code503RetryInterval)
 {
     SetValue(filePath, baseDir, code503RetryInterval);
 }
コード例 #11
0
        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");
        }
コード例 #12
0
        public void SecondConstructor_WithValidArgs_ValuesShouldEqualInput(string path, EnvDirectory envDir)
        {
            var fileDescriptor = new FileDescriptor(path, envDir);

            fileDescriptor.Path
            .ShouldBe(path);
            fileDescriptor.BaseDir
            .ShouldBe(envDir);
        }