Compare commits

..

2 Commits

Author SHA1 Message Date
sqozz eda9cae667 Fix handling of empty requests 2018-06-09 21:57:17 +02:00
sqozz 810f922927 Fix typo 2018-06-09 21:56:47 +02:00
2 changed files with 4 additions and 14 deletions

View File

@ -5,11 +5,10 @@ from urllib.parse import urlparse
app = Flask(__name__)
# HEAD is implicit with flask
@app.route('/', methods=['GET', 'POST'])
@app.route('/<shortLink>', methods=['GET', 'POST'])
def short(shortLink=""):
if request.method == "GET" or request.method == "HEAD":
if request.method == "GET":
if shortLink:
noauto = shortLink[-1] == "+"
if noauto: shortLink = shortLink[:-1]
@ -37,10 +36,11 @@ def short(shortLink=""):
else:
return render_template("index.html", name=shortLink) # Landing page
elif request.method == "POST": # Someone submitted a new link to short
longUrl = request.form["url"] # required, accept the exception if the key does not exist
longUrl = request.form.get("url", "")
wishId = request.form.get("wishId")
if len(longUrl) <= 0:
abort(400)
databaseId = insertIdUnique(longUrl, idToCheck=wishId)
return request.url_root + databaseId # Short link in plain text

View File

@ -17,11 +17,6 @@ class WebTestCase(object):
self.assertEqual(req.status_code, 200, msg="Get request unsuccessful")
return req
def assertHeadReq(self, url):
req = requests.head(url)
self.assertEqual(req.status_code, 301, msg="Head request unsuccessful")
return req
def assertGetStatusReq(self, expected_status, url, params = {}):
req = requests.get(url, params=params, allow_redirects=False)
self.assertEqual(req.status_code, expected_status, msg="Returned status code does not match the expected one")
@ -56,7 +51,7 @@ class SchortRegressionTests(unittest.TestCase, WebTestCase):
req = requests.post(BASE_URL + "/", data={"url": ""})
self.assertEqual(req.status_code, 400, msg="Could not handle a request with empty url")
req = requests.post(BASE_URL + "/", data={})
self.assertEqual(req.status_code, 400, msg="Could not handle a request with url at all")
self.assertEqual(req.status_code, 400, msg="Could not handle a request without a url at all")
class SchortShortLinkCase(object):
@ -82,11 +77,6 @@ class SchortShortLinkCase(object):
req = self.assertGetReq(BASE_URL + "/" + self.shortID, params = {"resolve" : ""})
self.assertEqual(req.text, self.shortDest)
def test_head_resolve(self):
"""Test resolving by using a HEAD request"""
req = self.assertHeadReq(BASE_URL + "/" + self.shortID)
self.assertEqual(req.headers.get("Location", ""), self.shortDest)
def test_HTMLresolve(self):
"""Test HTML displaying of the shortened URL"""
HTML_keyword = "+"