Exemple #1
0
 def test_url_exists(self):
     Url.create(url=self._test_url)
     with app.test_client() as c:
         resp = c.post("/", data={"url": self._test_url})
     self.assertEqual(resp.status_code, 200)
     # Test url created in db
     self.assertEqual(Url.select().where(Url.url == self._test_url).count(),
                      1)
Exemple #2
0
 def test_redirect(self):
     url = Url.create(url=self._test_url)
     with app.test_client() as c:
         resp = c.get(f"/{Base62.encode(url.id)}")
         self.assertEqual(resp.status_code, 302)
     url = Url.select().where(Url.url == self._test_url).first()
     self.assertEqual(url.views, 1)
Exemple #3
0
def index():
    if request.method == 'POST':
        original_url = request.form.get('url')
        if urlparse(original_url).scheme == '':
            original_url = f'http://{original_url}'
        url = Url.select().where(Url.url == original_url).first()
        if url:
            return render_template(
                "index.html",
                short_url=
                f"{request.scheme}://{app.config['SERVER_NAME']}/{Base62.encode(url.id)}",
                views=url.views)
        with database.atomic():
            url = Url.create(url=original_url)
        return render_template(
            "index.html",
            short_url=
            f"{request.scheme}://{app.config['SERVER_NAME']}/{Base62.encode(url.id)}"
        )
    return render_template('index.html')