コード例 #1
0
        public async Task CacheFound()
        {
            const string inputToken = "996";
            string       t;

            _tokenGeneratorMock.Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(true);

            var link = new Link
            {
                OriginUrl = "https://996.icu"
            };

            var memoryCache = MockMemoryCacheService.GetMemoryCache(link);

            // var cachedResponse = memoryCache.Get<Link>(inputToken);

            _mockFeatureManager.Setup(p => p.IsEnabledAsync(nameof(FeatureFlags.HonorDNT))).Returns(Task.FromResult(false));

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                memoryCache,
                _linkVerifierMock.Object,
                _mockFeatureManager.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            var result = await ctl.Forward(inputToken);

            Assert.IsInstanceOf(typeof(RedirectResult), result);
        }
コード例 #2
0
        public void Test_controller_get()
        {
            var forwardController = new ForwardController(forwardSolverMock.Object);
            var response          = forwardController.Get();

            string[] array = { "Controller", "Forward" };
            Assert.AreEqual(array, response);
        }
コード例 #3
0
        public async Task TestForwardEmptyToken(string token)
        {
            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                _memoryCacheMock.Object,
                _linkVerifierMock.Object);

            var result = await ctl.Forward(token);

            Assert.IsInstanceOf(typeof(BadRequestResult), result);
        }
コード例 #4
0
        public async Task Aka_EmptyName(string akaName)
        {
            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                _memoryCacheMock.Object,
                _linkVerifierMock.Object,
                _mockFeatureManager.Object);

            var result = await ctl.Aka(akaName);

            Assert.IsInstanceOf(typeof(BadRequestResult), result);
        }
コード例 #5
0
        public async Task TestFirstTimeRequestLinkNotExistsWithInvalidDefRedir(LinkVerifyResult linkVerifyResult)
        {
            string inputToken = "996";
            string t;

            _tokenGeneratorMock
            .Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(true);

            var link        = new Link();
            var memoryCache = MockMemoryCacheService.GetMemoryCache(link, false);

            _linkForwarderServiceMock
            .Setup(p => p.GetLinkAsync(null))
            .ReturnsAsync(new SuccessResponse <Link>(null));

            _linkVerifierMock
            .Setup(p => p.Verify(It.IsAny <string>(), It.IsAny <IUrlHelper>(), It.IsAny <HttpRequest>()))
            .Returns(linkVerifyResult);

            _appSettingsMock.Setup(p => p.Value).Returns(new AppSettings
            {
                DefaultRedirectionUrl = "INVALID_VALUE"
            });

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                memoryCache,
                _linkVerifierMock.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            var result = await ctl.Forward(inputToken);

            Assert.IsInstanceOf(typeof(StatusCodeResult), result);

            var statusCode = ((StatusCodeResult)result).StatusCode;

            Assert.IsTrue(statusCode == 500);
        }
コード例 #6
0
        public void FirstTimeRequest_InvalidOriginUrl()
        {
            string inputToken = "996";
            string t;

            _tokenGeneratorMock
            .Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(true);

            var link = new Link {
                IsEnabled = true, OriginUrl = "INVALID_VALUE"
            };
            var memoryCache = MockMemoryCacheService.GetMemoryCache(link, false);

            _linkForwarderServiceMock
            .Setup(p => p.GetLinkAsync(null))
            .ReturnsAsync(link);

            _linkVerifierMock
            .Setup(p => p.Verify(It.IsAny <string>(), It.IsAny <IUrlHelper>(), It.IsAny <HttpRequest>(), false))
            .Returns(LinkVerifyResult.InvalidFormat);

            _appSettingsMock.Setup(p => p.Value).Returns(new AppSettings
            {
                DefaultRedirectionUrl = "https://edi.wang"
            });

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                memoryCache,
                _linkVerifierMock.Object,
                _mockFeatureManager.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            Assert.ThrowsAsync(typeof(UriFormatException), async() =>
            {
                await ctl.Forward(inputToken);
            });
        }
コード例 #7
0
        public async Task TestFirstTimeRequestNoDntNoTTL()
        {
            string inputToken = "996";
            string t;

            _tokenGeneratorMock
            .Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(true);

            var link = new Link {
                IsEnabled = true, OriginUrl = "https://edi.wang"
            };
            var memoryCache = MockMemoryCacheService.GetMemoryCache(link, false);

            _linkForwarderServiceMock
            .Setup(p => p.GetLinkAsync(null))
            .ReturnsAsync(new SuccessResponse <Link>(link));

            _linkVerifierMock
            .Setup(p => p.Verify(It.IsAny <string>(), It.IsAny <IUrlHelper>(), It.IsAny <HttpRequest>()))
            .Returns(LinkVerifyResult.Valid);

            _appSettingsMock.Setup(p => p.Value).Returns(new AppSettings
            {
                HonorDNT = false
            });

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                memoryCache,
                _linkVerifierMock.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            var result = await ctl.Forward(inputToken);

            Assert.IsInstanceOf(typeof(RedirectResult), result);
        }
コード例 #8
0
        public async Task FirstTimeRequest_LinkNotExists_ValidDefRedir()
        {
            string inputToken = "996";
            string t;

            _tokenGeneratorMock
            .Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(true);

            var link        = new Link();
            var memoryCache = MockMemoryCacheService.GetMemoryCache(link, false);

            _linkForwarderServiceMock
            .Setup(p => p.GetLinkAsync(null))
            .ReturnsAsync(() => null);

            _linkVerifierMock
            .Setup(p => p.Verify(It.IsAny <string>(), It.IsAny <IUrlHelper>(), It.IsAny <HttpRequest>(), false))
            .Returns(LinkVerifyResult.Valid);

            _appSettingsMock.Setup(p => p.Value).Returns(new AppSettings
            {
                DefaultRedirectionUrl = "https://edi.wang"
            });

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                memoryCache,
                _linkVerifierMock.Object,
                _mockFeatureManager.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            var result = await ctl.Forward(inputToken);

            Assert.IsInstanceOf(typeof(RedirectResult), result);
        }
コード例 #9
0
        public async Task TestForwardEmptyUA(string ua)
        {
            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                _memoryCacheMock.Object,
                _linkVerifierMock.Object)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext()
                }
            };

            ctl.ControllerContext.HttpContext.Request.Headers["User-Agent"] = ua;

            var result = await ctl.Forward("996");

            Assert.IsInstanceOf(typeof(BadRequestResult), result);
        }
コード例 #10
0
        public async Task TestForwardInvalidToken()
        {
            string inputToken = "996";
            string t;

            _tokenGeneratorMock.Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(false);

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                _memoryCacheMock.Object,
                _linkVerifierMock.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            var result = await ctl.Forward(inputToken);

            Assert.IsInstanceOf(typeof(BadRequestResult), result);
        }
コード例 #11
0
        public async Task TestFirstTimeRequestServerError()
        {
            string inputToken = "996";
            string t;

            _tokenGeneratorMock
            .Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(true);

            var link = new Link {
                OriginUrl = "https://996.icu"
            };
            var memoryCache = MockMemoryCacheService.GetMemoryCache(link, false);

            _linkForwarderServiceMock
            .Setup(p => p.GetLinkAsync(null))
            .ReturnsAsync(new FailedResponse <Link>(0));

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                memoryCache,
                _linkVerifierMock.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            var result = await ctl.Forward(inputToken);

            Assert.IsInstanceOf(typeof(StatusCodeResult), result);

            var statusCode = ((StatusCodeResult)result).StatusCode;

            Assert.IsTrue(statusCode == 500);
        }
コード例 #12
0
        public async Task TestCacheFound()
        {
            string inputToken = "996";
            string t;

            _tokenGeneratorMock.Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(true);

            var link = new Link
            {
                OriginUrl = "https://996.icu"
            };

            var memoryCache = MockMemoryCacheService.GetMemoryCache(link);

            // var cachedResponse = memoryCache.Get<Link>(inputToken);

            _appSettingsMock.Setup(p => p.Value).Returns(new AppSettings()
            {
                HonorDNT = false
            });

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                memoryCache,
                _linkVerifierMock.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            var result = await ctl.Forward(inputToken);

            Assert.IsInstanceOf(typeof(RedirectResult), result);
        }
コード例 #13
0
        public async Task TestFirstTimeRequestLinkNotExistsNoDefRedir()
        {
            string inputToken = "996";
            string t;

            _tokenGeneratorMock
            .Setup(p => p.TryParseToken(inputToken, out t))
            .Returns(true);

            var link        = new Link();
            var memoryCache = MockMemoryCacheService.GetMemoryCache(link, false);

            _linkForwarderServiceMock
            .Setup(p => p.GetLinkAsync(null))
            .ReturnsAsync(new SuccessResponse <Link>(null));

            _appSettingsMock.Setup(p => p.Value).Returns(new AppSettings
            {
                DefaultRedirectionUrl = string.Empty
            });

            var ctl = new ForwardController(
                _appSettingsMock.Object,
                _loggerMock.Object,
                _linkForwarderServiceMock.Object,
                _tokenGeneratorMock.Object,
                memoryCache,
                _linkVerifierMock.Object)
            {
                ControllerContext = GetHappyPathHttpContext()
            };

            var result = await ctl.Forward(inputToken);

            Assert.IsInstanceOf(typeof(NotFoundResult), result);
        }
コード例 #14
0
 // Use this for initialization
 void Start()
 {
     gc = GameObject.Find("[CameraRig]").GetComponent <GameController>();
     fc = GameObject.Find("ForwardHolder").GetComponent <ForwardController>();
 }
コード例 #15
0
 void Awake()
 {
     instance = this;
 }