public void IsType(string userAgent, HttpUserAgentType expectedType, bool isMobile)
        {
            HttpUserAgentInformation info = HttpUserAgentInformation.Parse(userAgent);

            if (expectedType == HttpUserAgentType.Browser)
            {
                info.IsType(HttpUserAgentType.Browser).Should().Be(true);
                info.IsType(HttpUserAgentType.Robot).Should().Be(false);
                info.IsType(HttpUserAgentType.Unknown).Should().Be(false);

                info.IsBrowser().Should().Be(true);
                info.IsRobot().Should().Be(false);
            }
            else if (expectedType == HttpUserAgentType.Robot)
            {
                info.IsType(HttpUserAgentType.Browser).Should().Be(false);
                info.IsType(HttpUserAgentType.Robot).Should().Be(true);
                info.IsType(HttpUserAgentType.Unknown).Should().Be(false);

                info.IsBrowser().Should().Be(false);
                info.IsRobot().Should().Be(true);
            }
            else if (expectedType == HttpUserAgentType.Unknown)
            {
                info.IsType(HttpUserAgentType.Browser).Should().Be(false);
                info.IsType(HttpUserAgentType.Robot).Should().Be(false);
                info.IsType(HttpUserAgentType.Unknown).Should().Be(true);

                info.IsBrowser().Should().Be(false);
                info.IsRobot().Should().Be(false);
            }

            info.IsMobile().Should().Be(isMobile);
        }
Ejemplo n.º 2
0
        public void Parse()
        {
            HttpUserAgentParserMemoryCachedProviderOptions cachedProviderOptions = new();

            HttpUserAgentParserMemoryCachedProvider provider = new(cachedProviderOptions);

            // create first
            string userAgentOne =
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62";

            HttpUserAgentInformation infoOne = provider.Parse(userAgentOne);

            infoOne.Name.Should().Be("Edge");
            infoOne.Version.Should().Be("90.0.818.62");

            // check duplicate

            HttpUserAgentInformation infoDuplicate = provider.Parse(userAgentOne);

            infoDuplicate.Name.Should().Be("Edge");
            infoDuplicate.Version.Should().Be("90.0.818.62");

            // create second

            string userAgentTwo = "Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0";

            HttpUserAgentInformation infoTwo = provider.Parse(userAgentTwo);

            infoTwo.Name.Should().Be("Firefox");
            infoTwo.Version.Should().Be("41.0");
        }
        public void Parse(string userAgent)
        {
            HttpUserAgentInformation ua1 = HttpUserAgentParser.Parse(userAgent);
            HttpUserAgentInformation ua2 = HttpUserAgentInformation.Parse(userAgent);

            ua1.Should().BeEquivalentTo(ua2);
        }
        public void Parse(string userAgent)
        {
            HttpUserAgentParserDefaultProvider provider = new();

            HttpUserAgentInformation providerUserAgentInfo = provider.Parse(userAgent);
            HttpUserAgentInformation userAgentInfo         = HttpUserAgentInformation.Parse(userAgent);

            providerUserAgentInfo.Should().BeEquivalentTo(userAgentInfo);
        }
        public void CreateForRobot(string userAgent)
        {
            HttpUserAgentInformation ua = HttpUserAgentInformation.CreateForRobot(userAgent, "Chrome");

            ua.UserAgent.Should().Be(userAgent);
            ua.Type.Should().Be(HttpUserAgentType.Robot);
            ua.Platform.Should().Be(null);
            ua.Name.Should().Be("Chrome");
            ua.Version.Should().Be(null);
            ua.MobileDeviceType.Should().Be(null);
        }
        public void CreateForUnknown(string userAgent)
        {
            HttpUserAgentPlatformInformation platformInformation =
                new(new Regex(""), "Batman", HttpUserAgentPlatformType.Linux);

            HttpUserAgentInformation ua =
                HttpUserAgentInformation.CreateForUnknown(userAgent, platformInformation, null);

            ua.UserAgent.Should().Be(userAgent);
            ua.Type.Should().Be(HttpUserAgentType.Unknown);
            ua.Platform.Should().Be(platformInformation);
            ua.Name.Should().Be(null);
            ua.Version.Should().Be(null);
            ua.MobileDeviceType.Should().Be(null);
        }
        public void CreateForBrowser(string userAgent)
        {
            HttpUserAgentPlatformInformation platformInformation =
                new HttpUserAgentPlatformInformation(new Regex(""), "Android", HttpUserAgentPlatformType.Android);

            HttpUserAgentInformation ua = HttpUserAgentInformation.CreateForBrowser(userAgent,
                                                                                    platformInformation, "Edge", "46.3.4.5155", "Android");

            ua.UserAgent.Should().Be(userAgent);
            ua.Type.Should().Be(HttpUserAgentType.Browser);
            ua.Platform.Should().Be(platformInformation);
            ua.Name.Should().Be("Edge");
            ua.Version.Should().Be("46.3.4.5155");
            ua.MobileDeviceType.Should().Be("Android");
        }
        public void BotTests(string ua, string name)
        {
            HttpUserAgentInformation uaInfo = HttpUserAgentInformation.Parse(ua);

            uaInfo.Name.Should().Be(name);
            uaInfo.Version.Should().Be(null);
            uaInfo.UserAgent.Should().Be(ua);

            uaInfo.Type.Should().Be(HttpUserAgentType.Robot);

            uaInfo.Platform.Should().Be(null);
            uaInfo.MobileDeviceType.Should().Be(null);

            uaInfo.IsBrowser().Should().Be(false);
            uaInfo.IsMobile().Should().Be(false);
            uaInfo.IsRobot().Should().Be(true);
        }
        public void Get(string userAgent)
        {
            HttpUserAgentInformation userAgentInformation = HttpUserAgentInformation.Parse(userAgent);

            Mock <IHttpContextAccessor> httpMock = new();
            {
                DefaultHttpContext context = new DefaultHttpContext();
                context.Request.Headers["User-Agent"] = userAgent;
                httpMock.Setup(_ => _.HttpContext).Returns(context);
            }
            Mock <IHttpUserAgentParserProvider> parserMock = new();
            {
                parserMock.Setup(x => x.Parse(userAgent)).Returns(userAgentInformation);
            }

            HttpUserAgentParserAccessor accessor = new HttpUserAgentParserAccessor(httpMock.Object, parserMock.Object);
            HttpUserAgentInformation    info     = accessor.Get();

            info.Should().Be(userAgentInformation);
            parserMock.Verify(x => x.Parse(userAgent), Times.Once);
        }
        public void BrowserTests(string ua, string name, string version, string platformName, HttpUserAgentPlatformType platformType, string mobileDeviceType)
        {
            HttpUserAgentInformation uaInfo = HttpUserAgentInformation.Parse(ua);

            uaInfo.Name.Should().Be(name);
            uaInfo.Version.Should().Be(version);
            uaInfo.UserAgent.Should().Be(ua);

            uaInfo.Type.Should().Be(HttpUserAgentType.Browser);

            HttpUserAgentPlatformInformation platform = uaInfo.Platform.GetValueOrDefault();

            platform.PlatformType.Should().Be(platformType);
            platform.Name.Should().Be(platformName);

            uaInfo.MobileDeviceType.Should().Be(mobileDeviceType);

            uaInfo.IsBrowser().Should().Be(true);
            uaInfo.IsMobile().Should().Be(mobileDeviceType is not null);
            uaInfo.IsRobot().Should().Be(false);
        }