Ejemplo n.º 1
0
def test_get_urls_list(client):
    u = Url(origin_url=fake.uri(), short_url='fake_url')
    u2 = Url(origin_url=fake.uri(), short_url='fake_url_2')
    redis_cli.set(u.redis_key, u.json())
    redis_cli.set(u2.redis_key, u2.json())

    resp = client.get(f'/v1/url')

    assert resp.status_code == 200
    assert u in resp.json.get('urls')
    assert u2 in resp.json.get('urls')
Ejemplo n.º 2
0
def create():
    """
    Register the url
    """
    origin_url = request.values.get('origin_url', None)

    if not origin_url:
        raise ApiException('"origin_url" is required')

    MAX_TRY = 5

    for cnt in range(MAX_TRY):
        try:
            u = Url(short_url=Url.gen_short_url(), origin_url=origin_url)
        except ValidationError as e:
            raise ApiException(str(e))

        if redis_cli.set(u.redis_key,
                         u.json(),
                         nx=True,
                         ex=u.SHORT_URL_EXPIRE_SECONDS):
            break

        if cnt + 1 == MAX_TRY:
            raise ApiException('Collision happened. Please try again.')

    return jsonify({'short_url': u.short_url})
Ejemplo n.º 3
0
def test_get_origin_url(client):
    u = Url(origin_url=fake.uri(), short_url='fake_url')
    redis_cli.set(u.redis_key, u.json())

    resp = client.get(f'/v1/url/{u.short_url}')

    assert resp.status_code == 200
    assert resp.json.get('origin_url') == u.origin_url