Ejemplo n.º 1
0
 def test_int_to_int_conversion(self):
     converter = BaseConverter(FULL_ALPHABET)
     results = []
     for i in range(0, 10000):
         result = converter.int_to_str(i)
         self.assertFalse(result in results)  # check for uniqueness
         self.assertEqual(converter.str_to_int(result), i)
         results.append(result)
Ejemplo n.º 2
0
def catch_all(path):
    url_id = BaseConverter().string_to_int(path)
    url = Url.query.get(url_id) if url_id > 0 else None

    if url:
        url.clicks = Url.clicks + 1
        db.session.commit()
        return redirect(url.address, code=302)
    else:
        return render_template('404.html'), 404
Ejemplo n.º 3
0
 def test_str_to_int(self):
     converter = BaseConverter(FULL_ALPHABET)
     self.assertEqual(converter.str_to_int('0'), 1)
     self.assertEqual(converter.str_to_int('00'), 66)
     self.assertEqual(converter.str_to_int('a'), 11)
     self.assertEqual(converter.str_to_int('A'), 37)
     self.assertEqual(converter.str_to_int('0_'), 129)
Ejemplo n.º 4
0
 def test_int_to_str(self):
     converter = BaseConverter(FULL_ALPHABET)
     self.assertEqual(converter.int_to_str(1), '0')
     self.assertEqual(converter.int_to_str(66), '00')
     self.assertEqual(converter.int_to_str(11), 'a')
     self.assertEqual(converter.int_to_str(37), 'A')
     self.assertEqual(converter.int_to_str(129), '0_')
Ejemplo n.º 5
0
 def test_int_to_int_conversion_with_bytes(self):
     converter = BaseConverter(FULL_ALPHABET)
     for i in range(0, 10000):
         self.assertEqual(
             converter.str_to_int(
                 converter.bytes_to_str(
                     converter.str_to_bytes(converter.int_to_str(i)))), i)
Ejemplo n.º 6
0
def urls():
    if request.method == "POST":
        valid_url = validators.url(request.form['address'])
        if valid_url == True:
            new_url = Url(address=request.form['address'], user_id=session.get("user"))
            db.session.add(new_url)
            db.session.commit()
            flash("New Url added", category="success")
        else:
            flash("That URL is not valid", category="danger")
        return redirect("/urls")
    else:
        urls = []
        for url in Url.query.filter_by(user_id=session.get("user")).all():
            url.shortcode = request.url_root + BaseConverter().int_to_string(url.id)
            urls.append(url)
        return render_template("urls.html", urls=urls)
 def test_link(self):
     bc = BaseConverter()
     self.assertEqual(bc.convert(10, 10, 16), "A")
     self.assertEqual(bc.convert(256, 10, 16), "100")
     self.assertEqual(bc.convert(256, 10, 8), "400")
     self.assertEqual(bc.convert(255, 10, 2), "11111111")
Ejemplo n.º 8
0
from shortened_url_data import *
from url_utils import validate_url

# Constants.
BASE_SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
SERVICE_URL = "http://127.0.0.1:8000"

# Initializing flask app with database configs.
app = Flask(__name__)
app.config.from_pyfile('config.py')

# This one is created on shortened_url_data.py.
shortenedUrlDb.init_app(app)

# Initializing local helper classes.
baseConverter = BaseConverter(BASE_SYMBOLS)
log = Logger("url_shorter")


def create_error_response(msg, code):
    """
    It creates a custom JSON response for exceptional cases.
    """
    return make_response(jsonify({'error': msg}), code)


@app.errorhandler(400)
def bad_request(error):
    """
    Default handler for bad request cases.
    """
Ejemplo n.º 9
0
 def test_str_to_bytes(self):
     converter = BaseConverter(FULL_ALPHABET)
     with self.assertRaises(ValueError):
         converter.int_to_str(-1)