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_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')
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']
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
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
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