Skip to content

Commit f2d2ed8

Browse files
author
api.jscudder
committed
URL comparisons now use atom.url.Url objects instead of strings.
1 parent 5393ffe commit f2d2ed8

5 files changed

Lines changed: 53 additions & 18 deletions

File tree

src/atom/mock_http.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
import atom.http_interface
22+
import atom.url
2223

2324

2425
class Error(Exception):
@@ -32,6 +33,8 @@ class NoRecordingFound(Error):
3233
class MockRequest(object):
3334
def __init__(self, operation, url, data=None, headers=None):
3435
self.operation = operation
36+
if isinstance(url, (str, unicode)):
37+
url = atom.url.parse_url(url)
3538
self.url = url
3639
self.data = data
3740
self.headers = headers
@@ -94,7 +97,7 @@ def add_response(self, response, operation, url, data=None, headers=None):
9497
headers: dict of strings: Currently the headers are ignored when
9598
looking for matching requests.
9699
"""
97-
request = MockRequest(operation, str(url), data=data, headers=headers)
100+
request = MockRequest(operation, url, data=data, headers=headers)
98101
self.recordings.append((request, response))
99102

100103
def request(self, operation, url, data=None, headers=None):
@@ -106,8 +109,8 @@ def request(self, operation, url, data=None, headers=None):
106109
If there is no match, a NoRecordingFound error will be raised.
107110
"""
108111
if self.real_client is None:
109-
if url:
110-
url = str(url)
112+
if isinstance(url, (str, unicode)):
113+
url = atom.url.parse_url(url)
111114
for recording in self.recordings:
112115
if recording[0].operation == operation and recording[0].url == url:
113116
return recording[1]

src/atom/service.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,28 @@ def __str__(self):
306306

307307
def valid_for_scope(self, url):
308308
"""Tells the caller if the token authorizes access to the desired URL.
309-
310-
Since the generic token doesn't add an auth header, it is not valid for
311-
any scope.
312309
"""
310+
if isinstance(url, (str, unicode)):
311+
url = atom.url.parse_url(url)
313312
for scope in self.scopes:
314-
scope = str(scope)
315-
url = str(url)
316-
if url.startswith(scope):
313+
if scope == atom.token_store.SCOPE_ALL:
314+
return True
315+
if isinstance(scope, (str, unicode)):
316+
scope = atom.url.parse_url(scope)
317+
if scope == url:
318+
return True
319+
# Check to see if the port or protocol are set, if so are they different
320+
# Port and protocol should be checked first because they have default
321+
# values.
322+
elif scope.port and url.port and scope.port != url.port:
323+
continue
324+
elif scope.protocol and url.protocol and scope.protocol != url.protocol:
325+
continue
326+
elif scope.host == url.host and not scope.path:
327+
return True
328+
elif scope.host == url.host and scope.path and not url.path:
329+
continue
330+
elif scope.host == url.host and url.path.startswith(scope.path):
317331
return True
318332
return False
319333

src/atom/token_store.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030

3131
import atom.http_interface
32+
import atom.url
3233

3334

3435
SCOPE_ALL = 'http'
@@ -77,7 +78,8 @@ def find_token(self, url):
7778
"""
7879
if url is None:
7980
return None
80-
url = str(url)
81+
if isinstance(url, (str, unicode)):
82+
url = atom.url.parse_url(url)
8183
if url in self._tokens:
8284
token = self._tokens[url]
8385
if token.valid_for_scope(url):

src/gdata/auth.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import re
1919
import urllib
2020
import atom.http_interface
21+
import atom.url
2122

2223

2324
__author__ = 'api.jscudder (Jeff Scudder)'
@@ -303,17 +304,32 @@ def set_token_string(self, token_string):
303304

304305
def valid_for_scope(self, url):
305306
"""Tells the caller if the token authorizes access to the desired URL.
306-
307-
Since the generic token doesn't add an auth header, it is not valid for
308-
any scope.
309307
"""
308+
if isinstance(url, (str, unicode)):
309+
url = atom.url.parse_url(url)
310310
for scope in self.scopes:
311-
scope = str(scope)
312-
url = str(url)
313-
if url.startswith(scope):
311+
if scope == atom.token_store.SCOPE_ALL:
312+
return True
313+
if isinstance(scope, (str, unicode)):
314+
scope = atom.url.parse_url(scope)
315+
if scope == url:
316+
return True
317+
# Check to see if the port or protocol are set, if so are they different
318+
# Port and protocol should be checked first because they have default
319+
# values.
320+
elif scope.port and url.port and scope.port != url.port:
321+
continue
322+
elif scope.protocol and url.protocol and scope.protocol != url.protocol:
323+
continue
324+
elif scope.host == url.host and not scope.path:
325+
return True
326+
elif scope.host == url.host and scope.path and not url.path:
327+
continue
328+
elif scope.host == url.host and url.path.startswith(scope.path):
314329
return True
315330
return False
316331

332+
317333
class AuthSubToken(ClientLoginToken):
318334
def get_token_string(self):
319335
"""Removes AUTHSUB_AUTH_LABEL to give just the token value."""

src/gdata/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def Get(self, url, parser):
6565
6666
Returns: The result of calling parser(http_response_body).
6767
"""
68-
return gdata.service.GDataService.Get(self, uri=str(url), converter=parser)
68+
return gdata.service.GDataService.Get(self, uri=url, converter=parser)
6969

7070
def Post(self, data, url, parser, media_source=None):
7171
"""Streamlined version of Post.
@@ -86,7 +86,7 @@ def Put(self, data, url, parser, media_source=None):
8686
media_source=media_source, converter=parser)
8787

8888
def Delete(self, url):
89-
return gdata.service.GDataService.Delete(self, uri=str(url))
89+
return gdata.service.GDataService.Delete(self, uri=url)
9090

9191

9292
ExtractToken = gdata.service.ExtractToken

0 commit comments

Comments
 (0)