示例#1
0
def shorten_url():
    """Shortens a URL, returning a URL which will redirect to :url:

    :url: a valid URL which should be shortened

    """
    try:
        url = request.form['url']
    except KeyError:
        abort(400, "The required 'url' form value argument was not provided.")

    if not valid_url(url):
        abort(400, "The URL you have entered is malformed!")

    if OUR_HOSTNAME == urlparse(url).hostname:
        abort(400, "This is already a shortcats URL!")

    short = BASE_URL + shorten(url)

    return render_template("shortened.html", short=short, original=url)
示例#2
0
 def test_shorten_doesnt_exist_creates_new_next(self):
     rdb.set('url_counter', 51)
     shorten(TEST_URL)
     self.assertEqual(rdb.get('urls|' + TEST_URL), '1g')
     self.assertEqual(rdb.get('shorts|1g'), TEST_URL)
示例#3
0
 def test_shorten_doesnt_exist_creates_new(self):
     with self.assertRaises(KeyError):
         rdb['urls|' + TEST_URL]
     shorten(TEST_URL)
     self.assertEqual(rdb.get('urls|' + TEST_URL), '1')
     self.assertEqual(rdb.get('shorts|1'), TEST_URL)
示例#4
0
 def test_shorten_doesnt_exist_returns_next(self):
     rdb.set('url_counter', 51)
     self.assertEqual(shorten(TEST_URL), '1g')
示例#5
0
 def test_shorten_doesnt_exist_returns_valid(self):
     self.assertIsNone(rdb.get('urls|' + TEST_URL))
     self.assertEqual(shorten(TEST_URL), '1')
示例#6
0
 def test_shorten_already_exists(self):
     rdb.set('urls|' + TEST_URL, 'a1f')
     self.assertEqual(shorten(TEST_URL), 'a1f')