Exemplo n.º 1
0
 def test_is_tiny(self):
     untiny = Untiny()
     untiny.get_services = MagicMock(return_value=set([".tk", "1u.ro", "1url.com", "2pl.us"]))
     self.assertFalse(untiny.is_tiny("http://example.com"))
     self.assertTrue(untiny.is_tiny("http://1u.ro/123"))
     self.assertTrue(untiny.is_tiny("http://2pl.us/234"))
     self.assertEquals(untiny.get_services.call_count, 3)
Exemplo n.º 2
0
    def test_extract(self):
        untiny = Untiny()

        mock_response = MagicMock()
        mock_response_text = PropertyMock()
        type(mock_response).text = mock_response_text

        untiny.is_tiny = MagicMock()

        with patch("requests.get", MagicMock(return_value=mock_response)) as mock_get:
            # If the URL is tiny send a request to untiny.me to extract
            # full URL
            untiny.is_tiny.return_value = True
            mock_response_text.return_value = "http://foo.com"
            self.assertEquals(
                untiny.extract("http://2pl.us/234"),
                "http://foo.com",
            )
            self.assertEquals(mock_get.call_count, 1)
            self.assertEquals(mock_response_text.call_count, 1)

            # Check with another URL
            mock_response_text.return_value = "http://bar.com"
            self.assertEquals(
                untiny.extract("http://1u.ro/123"),
                "http://bar.com",
            )
            self.assertEquals(mock_get.call_count, 2)
            self.assertEquals(mock_response_text.call_count, 2)

            # If the URL is not tiny return it unchanged.
            untiny.is_tiny.return_value = False
            self.assertEquals(
                untiny.extract("http://example.com"),
                "http://example.com",
            )
            self.assertEquals(mock_get.call_count, 2)

        with patch("requests.get", MagicMock(side_effect=requests.RequestException)) as mock_get:
            # If a request to untiny.me fails return the original URL.
            untiny.is_tiny.return_value = True
            self.assertEquals(
                untiny.extract("http://1u.ro/123"),
                "http://1u.ro/123",
            )
            self.assertEquals(mock_get.call_count, 1)