Exemplo n.º 1
0
def test_new_url_already_exists_returns_short_url(client):
    original_url = 'https://www.placecage.com/gif/284/196'
    URLEntry(original_url, sequence=1).save()
    res = client.get('/new/{}'.format(original_url))
    assert res.status_code == 200
    assert res.json['original_url'] == original_url
    assert res.json['short_url'].endswith('/1')
Exemplo n.º 2
0
def test_new_url_with_query_parameters_returns_short_url(client):
    original_url = 'https://www.youtube.com/watch?v=FyYMzEplnfU'
    URLEntry(original_url, sequence=1).save()
    res = client.get('/new/{}'.format(original_url))
    assert res.status_code == 200
    assert res.json['original_url'] == original_url
    assert res.json['short_url'].endswith('/1')
def test_new_url_with_new_in_path_exists_returns_short_url(client, app):
    original_url = 'https://www.google.com/new/'
    URLEntry(_id=original_url, sequence=3).save()
    res = client.get('/new/{}'.format(original_url))
    assert res.status_code == 200
    assert res.json['original_url'] == original_url
    assert app.config['SERVER_NAME'] in res.json['short_url']
def test_new_url_with_query_params_exists_returns_short_url(client, app):
    original_url = 'https://www.youtube.com/watch?v=FyYMzEplnfU'
    URLEntry(_id=original_url, sequence=2).save()
    res = client.get('/new/{}'.format(original_url))
    assert res.status_code == 200
    assert res.json['original_url'] == original_url
    assert app.config['SERVER_NAME'] in res.json['short_url']
def test_new_url_already_exists_returns_short_url(client, app):
    original_url = 'https://www.placecage.com/200/300'
    URLEntry(_id=original_url, sequence=1).save()
    res = client.get('/new/{}'.format(original_url))
    assert res.status_code == 200
    assert res.json['original_url'] == original_url
    assert app.config['SERVER_NAME'] in res.json['short_url']
Exemplo n.º 6
0
def new_url(path):
    """
    Saves a new URL to the database and returns the short URL.
    """

    url = request.url.split('/new/')[1]

    url_entry = URLEntry.objects(_id=url).first()

    if not url_entry:
        try:
            url_entry = URLEntry(url).save()
        except ValidationError:
            return jsonify({'error': 'Invalid URL'}), 400

    if current_app.config.get('SSL'):
        app_url = request.host_url.replace('http://', 'https://')
    else:
        app_url = request.host_url

    encoded_sequence = hex(url_entry.sequence)[2:]

    return jsonify({'original_url': url, 'short_url': app_url + encoded_sequence}), 200
Exemplo n.º 7
0
def new_url(path):
    """
    Saves a new URL to the database and returns the short URL.
    """

    request_path = '{}/new/'.format(request.url_root)
    request_url_index_start = \
        request.url.find(request_path) + len(request_path)
    request_url = request.url[request_url_index_start:].strip()
    request_url_without_prefixes = \
        re.sub(r'^(http(s)?://)?(www.)?', '', request_url)

    server_name = current_app.config.get('SERVER_NAME')

    if request_url_without_prefixes.startswith(server_name):
        return jsonify({'error': 'That is already a shortened link'}), 400

    url_entry = URLEntry.objects(_id=request_url).first()

    if not url_entry:
        try:
            url_entry = URLEntry(_id=request_url).save()
        except ValidationError:
            return jsonify({'error': 'Invalid URL'}), 400

    if current_app.config.get('SSL'):
        app_url = request.host_url.replace('http://', 'https://')
    else:
        app_url = request.host_url

    hashids = Hashids(current_app.config['SECRET_KEY'])
    encoded_sequence = hashids.encode(url_entry.sequence)

    return jsonify({
        'original_url': request_url,
        'short_url': app_url + encoded_sequence
    }), 200
Exemplo n.º 8
0
def go_to_url(sequence):
    """
    Redirects to URL in database with given sequence.
    """

    try:
        decoded_sequence = int(sequence, 16)
    except ValueError:
        return abort(404)

    url_entry = URLEntry.objects(sequence=decoded_sequence).first()

    if url_entry:
        return redirect(url_entry.get_url())
    else:
        return abort(404)
Exemplo n.º 9
0
def go_to_url(hashed_id):
    """
    Redirects to URL in database with the given hashed ID.
    """

    hashids = Hashids(current_app.config['SECRET_KEY'])
    decoded_sequence_tuple = hashids.decode(hashed_id)

    if not decoded_sequence_tuple:
        return abort(404)

    decoded_sequence = decoded_sequence_tuple[0]
    url_entry = URLEntry.objects(sequence=decoded_sequence).first()

    if not url_entry:
        return abort(404)

    return redirect(url_entry.get_url())
Exemplo n.º 10
0
def test_go_to_url_redirects_to_url(client):
    URLEntry('https://www.placecage.com/g/155/300', sequence=1).save()
    res = client.get('/1')
    assert res.status_code == 302