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)
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)
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)
def test_shorten_doesnt_exist_returns_next(self): rdb.set('url_counter', 51) self.assertEqual(shorten(TEST_URL), '1g')
def test_shorten_doesnt_exist_returns_valid(self): self.assertIsNone(rdb.get('urls|' + TEST_URL)) self.assertEqual(shorten(TEST_URL), '1')
def test_shorten_already_exists(self): rdb.set('urls|' + TEST_URL, 'a1f') self.assertEqual(shorten(TEST_URL), 'a1f')