33
33
@mock.patch('friends.utils.http.Soup.Message',
34
34
FakeSoupMessage('friends.tests.data', 'short.dat'))
35
35
def test_isgd(self):
38
isgd.URLShortener().shorten('http://www.python.org'),
37
Short('is.gd').make('http://www.python.org'),
41
def test_isgd_protocol(self):
42
self.assertEqual(isgd.URLShortener.name, 'is.gd')
43
self.assertEqual(isgd.URLShortener.fqdn, 'http://is.gd')
45
40
@mock.patch('friends.utils.http.Soup.Message',
46
41
FakeSoupMessage('friends.tests.data', 'short.dat'))
47
42
def test_ougd(self):
50
ougd.URLShortener().shorten('http://www.python.org'),
44
Short('ou.gd').make('http://www.python.org'),
53
def test_ougd_protocol(self):
54
self.assertEqual(ougd.URLShortener.name, 'ou.gd')
55
self.assertEqual(ougd.URLShortener.fqdn, 'http://ou.gd')
57
47
@mock.patch('friends.utils.http.Soup.Message',
58
48
FakeSoupMessage('friends.tests.data', 'short.dat'))
59
49
def test_linkeecom(self):
62
linkeecom.URLShortener().shorten('http://www.python.org'),
51
Short('linkee.com').make('http://www.python.org'),
65
def test_linkeecom_protocol(self):
66
self.assertEqual(linkeecom.URLShortener.name, 'linkee.com')
67
self.assertEqual(linkeecom.URLShortener.fqdn, 'http://linkee.com')
69
54
@mock.patch('friends.utils.http.Soup.Message',
70
55
FakeSoupMessage('friends.tests.data', 'short.dat'))
71
56
def test_tinyurlcom(self):
74
tinyurlcom.URLShortener().shorten('http://www.python.org'),
58
Short('tinyurl.com').make('http://www.python.org'),
77
def test_tinyurlcom_protocol(self):
78
self.assertEqual(tinyurlcom.URLShortener.name, 'tinyurl.com')
79
self.assertEqual(tinyurlcom.URLShortener.fqdn, 'http://tinyurl.com')
81
61
@mock.patch('friends.utils.http.Soup.Message',
82
FakeSoupMessage('friends.tests.data', 'short.dat'))
83
def test_enabled_lookup(self):
84
# Look up an enabled shortener.
85
shortener = lookup.lookup('tinyurl.com')
62
FakeSoupMessage('friends.tests.data', 'durlme.dat'))
63
def test_durlme(self):
87
shortener.shorten('http://www.python.org'),
65
Short('durl.me').make('http://www.python.org'),
90
68
def test_missing_or_disabled_lookup(self):
91
69
# Looking up a non-existent or disabled shortener gives you one that
92
70
# returns the original url back unchanged.
93
shortener = lookup.lookup('dummy')
95
shortener.shorten('http://www.python.org'),
72
Short('nonexistant').make('http://www.python.org'),
73
'http://www.python.org')
75
Short().make('http://www.python.org'),
96
76
'http://www.python.org')
98
78
def test_is_shortened(self):
99
# Test a URL that has been shortened.
100
self.assertTrue(lookup.is_shortened('http://tinyurl.com/foo'))
101
self.assertTrue(lookup.is_shortened('http://is.gd/foo'))
102
self.assertTrue(lookup.is_shortened('http://linkee.com/foo'))
103
self.assertTrue(lookup.is_shortened('http://ou.gd/foo'))
79
# Test URLs that have been shortened.
80
self.assertTrue(Short.already('http://tinyurl.com/foo'))
81
self.assertTrue(Short.already('http://is.gd/foo'))
82
self.assertTrue(Short.already('http://linkee.com/foo'))
83
self.assertTrue(Short.already('http://ou.gd/foo'))
84
self.assertTrue(Short.already('http://durl.me/foo'))
105
86
def test_is_not_shortened(self):
106
87
# Test a URL that has not been shortened.
107
self.assertFalse(lookup.is_shortened('http://www.python.org/bar'))
109
@mock.patch('friends.shorteners.base.Downloader')
110
def test_urls_quoted_properly(self, dl_mock):
111
lookup.lookup('tinyurl.com').shorten(
88
self.assertFalse(Short.already('http://www.python.org/bar'))
90
@mock.patch('friends.utils.shorteners.Downloader')
91
def test_isgd_quoted_properly(self, dl_mock):
92
Short('is.gd').make('http://example.com/~user/stuff/+things')
93
dl_mock.assert_called_once_with(
94
'http://is.gd/api.php?longurl=http%3A%2F%2Fexample.com'
95
'%2F%7Euser%2Fstuff%2F%2Bthings')
97
@mock.patch('friends.utils.shorteners.Downloader')
98
def test_ougd_quoted_properly(self, dl_mock):
99
Short('ou.gd').make('http://example.com/~user/stuff/+things')
100
dl_mock.assert_called_once_with(
101
'http://ou.gd/api.php?format=simple&action=shorturl&url='
102
'http%3A%2F%2Fexample.com%2F%7Euser%2Fstuff%2F%2Bthings')
104
@mock.patch('friends.utils.shorteners.Downloader')
105
def test_linkeecom_quoted_properly(self, dl_mock):
106
Short('linkee.com').make(
107
'http://example.com/~user/stuff/+things')
108
dl_mock.assert_called_once_with(
109
'http://api.linkee.com/1.0/shorten?format=text&input='
110
'http%3A%2F%2Fexample.com%2F%7Euser%2Fstuff%2F%2Bthings')
112
@mock.patch('friends.utils.shorteners.Downloader')
113
def test_tinyurl_quoted_properly(self, dl_mock):
114
Short('tinyurl.com').make(
112
115
'http://example.com/~user/stuff/+things')
113
116
dl_mock.assert_called_once_with(
114
117
'http://tinyurl.com/api-create.php?url=http%3A%2F%2Fexample.com'
115
118
'%2F%7Euser%2Fstuff%2F%2Bthings')
120
@mock.patch('friends.utils.shorteners.Downloader')
121
def test_durlme_quoted_properly(self, dl_mock):
122
dl_mock().get_string().strip.return_value = ''
124
Short('durl.me').make(
125
'http://example.com/~user/stuff/+things')
126
dl_mock.assert_called_once_with(
127
'http://durl.me/api/Create.do?type=json&longurl='
128
'http%3A%2F%2Fexample.com%2F%7Euser%2Fstuff%2F%2Bthings')
130
@mock.patch('friends.utils.shorteners.Downloader')
131
def test_dont_over_shorten(self, dl_mock):
132
Short('tinyurl.com').make('http://tinyurl.com/page_id')
133
Short('linkee.com').make('http://ou.gd/page_id')
134
Short('is.gd').make('http://is.gd/page_id')
135
Short('ou.gd').make('http://linkee.com/page_id')
136
self.assertEqual(dl_mock.call_count, 0)
138
def test_find_all_in_string(self):
140
shorter.make = lambda url: 'zombo.com'
142
'Welcome to zombo.com, anything is possible. '
143
'You can do anything at zombo.com!',
145
'Welcome to http://example.com/really/really/long/url, '
146
'anything is possible. You can do anything at '
147
'http://example.com!'))