Skip to content

Commit

Permalink
Merge 4d2e10f into 508604c
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Sep 12, 2016
2 parents 508604c + 4d2e10f commit 32f7476
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions goodtables/utilities/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import os
import io
import re
import json
import inspect
import requests
Expand Down Expand Up @@ -202,6 +203,7 @@ def validate_handler(handler, argcount=1):
if not len(spec[0]) == argcount:
raise exceptions.InvalidHandlerError


def make_valid_url(url):
"""Make sure url doesn't contain unsupported characters
Expand All @@ -214,8 +216,13 @@ def make_valid_url(url):
return (glue).join(quoted)

scheme, netloc, path, query, fragment = compat.urlsplit(url)
quoted_path = compat.quote(path.encode('utf-8'))
quoted_query = compat.quote_plus(query.encode('utf-8'))
new_url_tuple = (scheme, netloc, quoted_path, quoted_query, fragment)
encoded_path = url_encode_non_ascii(path)
encoded_query = url_encode_non_ascii(query)
new_url_tuple = (scheme, netloc, encoded_path, encoded_query, fragment)
quoted_url = compat.urlunsplit(new_url_tuple)
return quoted_url


def url_encode_non_ascii(text):
# http://stackoverflow.com/questions/4389572/how-to-fetch-a-non-ascii-url-with-python-urlopen
return re.sub('[\x80-\xFF]', lambda c: '%%%02x' % ord(c.group(0)), text)
4 changes: 4 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ def test_make_valid_url(self):
'data_url=http://data.defra.gov.uk/ops/government_procurement_card/over_£500_GPC_apr_2013.csv')
assertion = '£' not in utilities.helpers.make_valid_url(url)
self.assertTrue(assertion)

def test_make_valid_url_dont_break_query(self):
url = 'http://next.openspending.org/fdp-adapter/convert?url=https%3A%2F%2Fraw.githubusercontent.com%2Fkravets-levko%2Fdata%2Fmaster%2Ftest.xlsx.csv'
self.assertEqual(utilities.helpers.make_valid_url(url), url)

0 comments on commit 32f7476

Please sign in to comment.