def test_getConverted_toProxies_ShouldSkipInvalidProxies(self):
        # Given
        someInvalid = [
            "foo.bar.de:8080:TestUser:TestPassword", "this.is.invalid::x",
            "234.178.188.6:9274"
        ]
        sut = StringlistProxiesConverter(source=someInvalid,
                                         target=tp.List[Proxy])

        # When
        proxies = sut.getConverted()

        # Then
        self.assertEqual(2, len(proxies))

        idx = 0
        self.assertEqual("foo.bar.de", proxies[idx].endpoint)
        self.assertEqual(8080, proxies[idx].port)
        self.assertEqual("TestUser", proxies[0].username)
        self.assertEqual("TestPassword", proxies[idx].pw)
        idx = 1
        self.assertEqual("234.178.188.6", proxies[idx].endpoint)
        self.assertEqual(9274, proxies[idx].port)
        self.assertEqual("", proxies[idx].username)
        self.assertEqual("", proxies[idx].pw)
    def test_getConverted_toProxies_shouldSetObjectArray(self):
        # Given
        proxyStrings = [
            "foo.bar.de:8080:TestUser:TestPassword", "another.one.de:2846",
            "234.178.188.6:9274"
        ]

        sut = StringlistProxiesConverter(source=proxyStrings,
                                         target=tp.List[Proxy])

        # When
        proxies = sut.getConverted()

        # Then
        self.assertEqual(3, len(proxies))

        idx = 0
        self.assertEqual("foo.bar.de", proxies[idx].endpoint)
        self.assertEqual(8080, proxies[idx].port)
        self.assertEqual("TestUser", proxies[0].username)
        self.assertEqual("TestPassword", proxies[idx].pw)
        idx = 1
        self.assertEqual("another.one.de", proxies[idx].endpoint)
        self.assertEqual(2846, proxies[idx].port)
        self.assertEqual("", proxies[idx].username)
        self.assertEqual("", proxies[idx].pw)
        idx = 2
        self.assertEqual("234.178.188.6", proxies[idx].endpoint)
        self.assertEqual(9274, proxies[idx].port)
        self.assertEqual("", proxies[idx].username)
        self.assertEqual("", proxies[idx].pw)
    def test_getConverted_toProxies_shouldReturnRepoWithZeroProxiesOnEmptyList(
            self):
        # Given
        emptyList = []
        sut = StringlistProxiesConverter(source=emptyList,
                                         target=tp.List[Proxy])

        # When
        proxies = sut.getConverted()

        # Then
        self.assertIsInstance(proxies, list)
        self.assertEqual(0, len(proxies))
        self.assertTrue(all(isinstance(i, Proxy) for i in proxies))
    def test_getConverted_toStringlist_shouldReturnEmptyListOnEmptyProxyObjectList(
            self):
        # Given
        emptyList = list()
        self.assertEqual(0, len(emptyList))

        sut = StringlistProxiesConverter(source=emptyList,
                                         target=tp.List[Proxy])

        # When
        stringList = sut.getConverted()

        # Then
        self.assertEqual(0, len(stringList))
        self.assertIsInstance(stringList, list)
    def test_getConverted_toProxies_shouldReturnCorrectInstance(self):
        # Given
        proxyStrings = [
            "foo.bar.de:8080:TestUser:TestPassword", "another.one.de:2846",
            "234.178.188.6:9274"
        ]

        sut = StringlistProxiesConverter(source=proxyStrings,
                                         target=tp.List[Proxy])

        # When
        proxies = sut.getConverted()

        # Then
        self.assertIsInstance(proxies, list)
        self.assertTrue(all(isinstance(i, Proxy) for i in proxies))
    def test_getConverted_toStringlist_ShouldSkipInvalidProxies(self):
        # Given
        proxy1 = Proxy.make(endpoint="InvalidProxyMissingUsername",
                            port=2333,
                            username="",
                            pw="MyPass")
        proxy2 = Proxy.make(endpoint="186.230.153.100",
                            port=3526,
                            username="******",
                            pw="Some+-Pass")
        proxies = list((proxy1, proxy2))

        sut = StringlistProxiesConverter(source=proxies, target=tp.List[str])

        # When
        stringList = sut.getConverted()

        # Then
        self.assertEqual(1, len(stringList))
        self.assertEqual("186.230.153.100:3526:Some_User:Some+-Pass",
                         stringList[0])
Beispiel #7
0
    def loadAll(self) -> Optional[List[Proxy]]:
        """ Tries to load proxies from a text file and set a Proxy
        list from it. If no file path was given during init, we load from the
        default path. You're also free to change the path from outside via
        `self.path` after initialization.

        :return: List of Proxy objects.
        :raises: Exception
        """
        super().loadAll()  # raises
        # Note that super() method also runs/triggers hooks like _postprocess() etc.

        if self.__proxyStringList:
            converter = StringlistProxiesConverter(
                source=self.__proxyStringList, target=List[Proxy])
            return converter.getConverted()

        else:
            logger.warning(
                "No valid proxy data found in repo file. Please check if they are "
                "marked as inactive by a '#' character at line start. Make sure"
                "your proxy file is empty if you don't need proxies.")
            return None
    def test_getConverted_toStringlist_shouldSetStringList(self):
        # Given
        proxy1 = Proxy.make(endpoint="abc.bar.de",
                            port=8021,
                            username="******",
                            pw="TestPW")
        proxy2 = Proxy.make(endpoint="123.234.217.1",
                            port=3526,
                            username="",
                            pw="")
        proxies = list((proxy1, proxy2))

        sut = StringlistProxiesConverter(source=proxies, target=tp.List[str])

        # When
        stringList = sut.getConverted()

        # Then
        self.assertIsInstance(stringList, list)
        self.assertEqual(2, len(stringList))
        idx = 0
        self.assertEqual("abc.bar.de:8021:Tester:TestPW", stringList[idx])
        idx = 1
        self.assertEqual("123.234.217.1:3526", stringList[idx])
 def test_isInstanceOfBaseConverter(self):
     sut = StringlistProxiesConverter(list(), tp.List[Proxy])
     self.assertIsInstance(sut, BaseConverter)