Additional fixes for py3k
This commit is contained in:
parent
428ed5b59b
commit
dd57365398
@ -23,6 +23,8 @@
|
||||
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
# OTHER DEALINGS IN THE SOFTWARE.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
version = '0.5.8'
|
||||
|
||||
import re
|
||||
@ -41,7 +43,7 @@ if PY3:
|
||||
text_type = str
|
||||
byte_type = bytes
|
||||
import io
|
||||
StringIO = io.StringIO
|
||||
StringIO = io.BytesIO
|
||||
|
||||
class Py3kObject(object):
|
||||
def __repr__(self):
|
||||
@ -110,6 +112,13 @@ def utf8(s):
|
||||
return byte_type(s)
|
||||
|
||||
|
||||
def decode_utf8(s):
|
||||
if isinstance(s, byte_type):
|
||||
s = s.decode("utf-8")
|
||||
|
||||
return text_type(s)
|
||||
|
||||
|
||||
def parse_requestline(s):
|
||||
"""
|
||||
http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5
|
||||
@ -123,8 +132,8 @@ def parse_requestline(s):
|
||||
...
|
||||
ValueError: Not a Request-Line
|
||||
"""
|
||||
methods = '|'.join(HTTPretty.METHODS)
|
||||
m = re.match(r'('+methods+')\s+(.*)\s+HTTP/(1.[0|1])', s, re.I)
|
||||
methods = b'|'.join(HTTPretty.METHODS)
|
||||
m = re.match(br'(' + methods + b')\s+(.*)\s+HTTP/(1.[0|1])', s, re.I)
|
||||
if m:
|
||||
return m.group(1).upper(), m.group(2), m.group(3)
|
||||
else:
|
||||
@ -135,7 +144,9 @@ class HTTPrettyRequest(BaseHTTPRequestHandler, Py3kObject):
|
||||
def __init__(self, headers, body=''):
|
||||
self.body = utf8(body)
|
||||
self.raw_headers = utf8(headers)
|
||||
self.rfile = StringIO('\r\n\r\n'.join([headers.strip(), body]))
|
||||
self.client_address = ['10.0.0.1']
|
||||
self.rfile = StringIO(b'\r\n\r\n'.join([headers.strip(), body]))
|
||||
self.wfile = StringIO()
|
||||
self.raw_requestline = self.rfile.readline()
|
||||
self.error_code = self.error_message = None
|
||||
self.parse_request()
|
||||
@ -159,15 +170,7 @@ class HTTPrettyRequestEmpty(object):
|
||||
|
||||
|
||||
class FakeSockFile(StringIO):
|
||||
def read(self, amount=None):
|
||||
amount = amount or self.len
|
||||
new_amount = amount
|
||||
|
||||
if amount > self.len:
|
||||
new_amount = self.len - self.tell()
|
||||
|
||||
ret = StringIO.read(self, new_amount)
|
||||
return ret
|
||||
pass
|
||||
|
||||
|
||||
class FakeSSLSocket(object):
|
||||
@ -264,8 +267,7 @@ class fakesock(object):
|
||||
hostnames = [getattr(i.info, 'hostname', None) for i in HTTPretty._entries.keys()]
|
||||
self.fd.seek(0)
|
||||
try:
|
||||
|
||||
requestline, _ = data.split('\r\n', 1)
|
||||
requestline, _ = data.split(b'\r\n', 1)
|
||||
method, path, version = parse_requestline(requestline)
|
||||
is_parsing_headers = True
|
||||
except ValueError:
|
||||
@ -287,7 +289,7 @@ class fakesock(object):
|
||||
# path might come with
|
||||
s = urlsplit(path)
|
||||
|
||||
headers, body = map(utf8, data.split('\r\n\r\n', 1))
|
||||
headers, body = map(utf8, data.split(b'\r\n\r\n', 1))
|
||||
|
||||
request = HTTPretty.historify_request(headers, body)
|
||||
|
||||
@ -321,7 +323,7 @@ class fakesock(object):
|
||||
("Please open an issue at "
|
||||
"'https://github.com/gabrielfalcao/HTTPretty/issues'"),
|
||||
"And paste the following traceback:\n",
|
||||
"".join(lines),
|
||||
"".join(decode_utf8(lines)),
|
||||
]
|
||||
raise RuntimeError("\n".join(message))
|
||||
|
||||
@ -502,7 +504,7 @@ class Entry(Py3kObject):
|
||||
def normalize_headers(self, headers):
|
||||
new = {}
|
||||
for k in headers:
|
||||
new_k = '-'.join([s.title() for s in k.split('-')])
|
||||
new_k = '-'.join([s.lower() for s in k.split('-')])
|
||||
new[new_k] = headers[k]
|
||||
|
||||
return new
|
||||
@ -511,47 +513,49 @@ class Entry(Py3kObject):
|
||||
now = datetime.utcnow()
|
||||
|
||||
headers = {
|
||||
'Status': self.status,
|
||||
'Date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
|
||||
'Server': 'Python/HTTPretty',
|
||||
'Connection': 'close',
|
||||
'status': self.status,
|
||||
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
|
||||
'server': 'Python/HTTPretty',
|
||||
'connection': 'close',
|
||||
}
|
||||
|
||||
if self.forcing_headers:
|
||||
headers = self.forcing_headers
|
||||
|
||||
if self.adding_headers:
|
||||
headers.update(self.adding_headers)
|
||||
headers.update(self.normalize_headers(self.adding_headers))
|
||||
|
||||
headers = self.normalize_headers(headers)
|
||||
|
||||
status = headers.get('Status', self.status)
|
||||
status = headers.get('status', self.status)
|
||||
string_list = [
|
||||
'HTTP/1.1 %d %s' % (status, STATUSES[status]),
|
||||
]
|
||||
|
||||
if 'Date' in headers:
|
||||
string_list.append('Date: %s' % headers.pop('Date'))
|
||||
if 'date' in headers:
|
||||
string_list.append('date: %s' % headers.pop('date'))
|
||||
|
||||
if not self.forcing_headers:
|
||||
content_type = headers.pop('Content-Type',
|
||||
content_type = headers.pop('content-type',
|
||||
'text/plain; charset=utf-8')
|
||||
|
||||
content_length = headers.pop('Content-Length', self.body_length)
|
||||
content_length = headers.pop('content-length', self.body_length)
|
||||
|
||||
string_list.append('Content-Type: %s' % content_type)
|
||||
string_list.append('content-type: %s' % content_type)
|
||||
if not self.streaming:
|
||||
string_list.append('Content-Length: %s' % content_length)
|
||||
string_list.append('content-length: %s' % content_length)
|
||||
|
||||
string_list.append('Server: %s' % headers.pop('Server'))
|
||||
string_list.append('server: %s' % headers.pop('server'))
|
||||
|
||||
for k, v in headers.items():
|
||||
string_list.append(
|
||||
'%s: %s' % (k, utf8(v)),
|
||||
'{0}: {1}'.format(k, v),
|
||||
)
|
||||
|
||||
fk.write("\n".join(string_list))
|
||||
fk.write('\n\r\n')
|
||||
for item in string_list:
|
||||
fk.write(utf8(item) + b'\n')
|
||||
|
||||
fk.write(b'\r\n')
|
||||
|
||||
if self.streaming:
|
||||
self.body, body = itertools.tee(self.body)
|
||||
@ -607,7 +611,9 @@ class URIInfo(Py3kObject):
|
||||
return hash(text_type(self))
|
||||
|
||||
def __eq__(self, other):
|
||||
return text_type(self) == text_type(other)
|
||||
self_tuple = (self.port, decode_utf8(self.hostname), decode_utf8(self.path))
|
||||
other_tuple = (other.port, decode_utf8(other.hostname), decode_utf8(other.path))
|
||||
return self_tuple == other_tuple
|
||||
|
||||
def full_url(self):
|
||||
credentials = ""
|
||||
@ -617,15 +623,16 @@ class URIInfo(Py3kObject):
|
||||
|
||||
query = ""
|
||||
if self.query:
|
||||
query = "?{0}".format(self.query)
|
||||
query = "?{0}".format(decode_utf8(self.query))
|
||||
|
||||
return "{scheme}://{credentials}{host}{path}{query}".format(
|
||||
result = "{scheme}://{credentials}{host}{path}{query}".format(
|
||||
scheme=self.scheme,
|
||||
credentials=credentials,
|
||||
host=self.hostname,
|
||||
path=self.path,
|
||||
host=decode_utf8(self.hostname),
|
||||
path=decode_utf8(self.path),
|
||||
query=query
|
||||
)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def from_uri(cls, uri, entry):
|
||||
@ -690,12 +697,12 @@ class HTTPretty(Py3kObject):
|
||||
u"""The URI registration class"""
|
||||
_entries = {}
|
||||
latest_requests = []
|
||||
GET = 'GET'
|
||||
PUT = 'PUT'
|
||||
POST = 'POST'
|
||||
DELETE = 'DELETE'
|
||||
HEAD = 'HEAD'
|
||||
PATCH = 'PATCH'
|
||||
GET = b'GET'
|
||||
PUT = b'PUT'
|
||||
POST = b'POST'
|
||||
DELETE = b'DELETE'
|
||||
HEAD = b'HEAD'
|
||||
PATCH = b'PATCH'
|
||||
METHODS = (GET, PUT, POST, DELETE, HEAD, PATCH)
|
||||
last_request = HTTPrettyRequestEmpty()
|
||||
|
||||
@ -723,6 +730,9 @@ class HTTPretty(Py3kObject):
|
||||
responses=None, **headers):
|
||||
|
||||
if isinstance(responses, list) and len(responses) > 0:
|
||||
for response in responses:
|
||||
response.uri = uri
|
||||
response.method = method
|
||||
entries_for_this_uri = responses
|
||||
else:
|
||||
headers['body'] = body
|
||||
@ -734,9 +744,6 @@ class HTTPretty(Py3kObject):
|
||||
cls.Response(method=method, uri=uri, **headers),
|
||||
]
|
||||
|
||||
map(lambda e: setattr(e, 'uri', uri) or setattr(e, 'method', method),
|
||||
entries_for_this_uri)
|
||||
|
||||
matcher = URIMatcher(uri, entries_for_this_uri)
|
||||
if matcher in cls._entries:
|
||||
del cls._entries[matcher]
|
||||
|
@ -1,16 +1,13 @@
|
||||
distribute==0.6.31
|
||||
coverage==3.5.3
|
||||
httplib2==0.7.6
|
||||
ipdb==0.7
|
||||
ipython==0.13.1
|
||||
misaka==1.0.2
|
||||
multiprocessing==2.6.2.1
|
||||
nose==1.2.1
|
||||
py==1.4.12
|
||||
requests==0.14.2
|
||||
requests==1.1.0
|
||||
steadymark==0.4.5
|
||||
sure==1.1.3
|
||||
tornado==2.4
|
||||
tox==1.4.2
|
||||
virtualenv==1.8.2
|
||||
wsgiref==0.1.2
|
||||
|
@ -26,8 +26,12 @@
|
||||
# OTHER DEALINGS IN THE SOFTWARE.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import urllib2
|
||||
from testserver import Server
|
||||
try:
|
||||
import urllib.request as urllib2
|
||||
except ImportError:
|
||||
import urllib2
|
||||
|
||||
from .testserver import Server
|
||||
from sure import expect, that_with_context
|
||||
from httpretty import HTTPretty, httprettified
|
||||
|
||||
@ -59,13 +63,13 @@ def test_httpretty_bypasses_when_disabled(context):
|
||||
fd.close()
|
||||
|
||||
expect(got1).to.equal(
|
||||
'. o O 0 O o . o O 0 O o . o O 0 O o . o O 0 O o . o O 0 O o .')
|
||||
b'. o O 0 O o . o O 0 O o . o O 0 O o . o O 0 O o . o O 0 O o .')
|
||||
|
||||
fd = urllib2.urlopen('http://localhost:9999/come-again/')
|
||||
got2 = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got2).to.equal('<- HELLO WORLD ->')
|
||||
expect(got2).to.equal(b'<- HELLO WORLD ->')
|
||||
|
||||
HTTPretty.enable()
|
||||
|
||||
@ -73,7 +77,7 @@ def test_httpretty_bypasses_when_disabled(context):
|
||||
got3 = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got3).to.equal('glub glub')
|
||||
expect(got3).to.equal(b'glub glub')
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -89,10 +93,10 @@ def test_httpretty_bypasses_a_unregistered_request(context):
|
||||
got1 = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got1).to.equal('glub glub')
|
||||
expect(got1).to.equal(b'glub glub')
|
||||
|
||||
fd = urllib2.urlopen('http://localhost:9999/come-again/')
|
||||
got2 = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got2).to.equal('<- HELLO WORLD ->')
|
||||
expect(got2).to.equal(b'<- HELLO WORLD ->')
|
||||
|
@ -29,7 +29,7 @@ from __future__ import unicode_literals
|
||||
import re
|
||||
import httplib2
|
||||
from sure import expect, within, microseconds
|
||||
from httpretty import HTTPretty, httprettified
|
||||
from httpretty import HTTPretty, httprettified, decode_utf8
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -41,7 +41,7 @@ def test_httpretty_should_mock_a_simple_get_with_httplib2_read(now):
|
||||
body="Find the best daily deals")
|
||||
|
||||
_, got = httplib2.Http().request('http://yipit.com', 'GET')
|
||||
expect(got).to.equal('Find the best daily deals')
|
||||
expect(got).to.equal(b'Find the best daily deals')
|
||||
expect(HTTPretty.last_request.method).to.equal('GET')
|
||||
expect(HTTPretty.last_request.path).to.equal('/')
|
||||
|
||||
@ -178,19 +178,19 @@ def test_rotating_responses_with_httplib2(now):
|
||||
'https://api.yahoo.com/test', 'GET')
|
||||
|
||||
expect(headers1['status']).to.equal('201')
|
||||
expect(body1).to.equal('first response')
|
||||
expect(body1).to.equal(b'first response')
|
||||
|
||||
headers2, body2 = httplib2.Http().request(
|
||||
'https://api.yahoo.com/test', 'GET')
|
||||
|
||||
expect(headers2['status']).to.equal('202')
|
||||
expect(body2).to.equal('second and last response')
|
||||
expect(body2).to.equal(b'second and last response')
|
||||
|
||||
headers3, body3 = httplib2.Http().request(
|
||||
'https://api.yahoo.com/test', 'GET')
|
||||
|
||||
expect(headers3['status']).to.equal('202')
|
||||
expect(body3).to.equal('second and last response')
|
||||
expect(body3).to.equal(b'second and last response')
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -211,12 +211,12 @@ def test_can_inspect_last_request(now):
|
||||
|
||||
expect(HTTPretty.last_request.method).to.equal('POST')
|
||||
expect(HTTPretty.last_request.body).to.equal(
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
)
|
||||
expect(HTTPretty.last_request.headers['content-type']).to.equal(
|
||||
'text/json',
|
||||
)
|
||||
expect(body).to.equal('{"repositories": ["HTTPretty", "lettuce"]}')
|
||||
expect(body).to.equal(b'{"repositories": ["HTTPretty", "lettuce"]}')
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -237,12 +237,12 @@ def test_can_inspect_last_request_with_ssl(now):
|
||||
|
||||
expect(HTTPretty.last_request.method).to.equal('POST')
|
||||
expect(HTTPretty.last_request.body).to.equal(
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
)
|
||||
expect(HTTPretty.last_request.headers['content-type']).to.equal(
|
||||
'text/json',
|
||||
)
|
||||
expect(body).to.equal('{"repositories": ["HTTPretty", "lettuce"]}')
|
||||
expect(body).to.equal(b'{"repositories": ["HTTPretty", "lettuce"]}')
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -255,7 +255,7 @@ def test_httpretty_ignores_querystrings_from_registered_uri(now):
|
||||
|
||||
_, got = httplib2.Http().request('http://yipit.com/?id=123', 'GET')
|
||||
|
||||
expect(got).to.equal('Find the best daily deals')
|
||||
expect(got).to.equal(b'Find the best daily deals')
|
||||
expect(HTTPretty.last_request.method).to.equal('GET')
|
||||
expect(HTTPretty.last_request.path).to.equal('/?id=123')
|
||||
|
||||
@ -267,7 +267,7 @@ def test_callback_response(now):
|
||||
" httplib2")
|
||||
|
||||
def request_callback(method, uri, headers):
|
||||
return "The {0} response from {1}".format(method, uri)
|
||||
return "The {0} response from {1}".format(decode_utf8(method), uri)
|
||||
|
||||
HTTPretty.register_uri(
|
||||
HTTPretty.GET, "https://api.yahoo.com/test",
|
||||
@ -276,7 +276,7 @@ def test_callback_response(now):
|
||||
headers1, body1 = httplib2.Http().request(
|
||||
'https://api.yahoo.com/test', 'GET')
|
||||
|
||||
expect(body1).to.equal('The GET response from https://api.yahoo.com/test')
|
||||
expect(body1).to.equal(b"The GET response from https://api.yahoo.com/test")
|
||||
|
||||
HTTPretty.register_uri(
|
||||
HTTPretty.POST, "https://api.yahoo.com/test_post",
|
||||
@ -285,7 +285,7 @@ def test_callback_response(now):
|
||||
headers2, body2 = httplib2.Http().request(
|
||||
'https://api.yahoo.com/test_post', 'POST')
|
||||
|
||||
expect(body2).to.equal('The POST response from https://api.yahoo.com/test_post')
|
||||
expect(body2).to.equal(b"The POST response from https://api.yahoo.com/test_post")
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -299,6 +299,6 @@ def test_httpretty_should_allow_registering_regexes():
|
||||
)
|
||||
|
||||
response, body = httplib2.Http().request('https://api.yipit.com/v1/deal;brand=gap', 'GET')
|
||||
expect(body).to.equal('Found brand')
|
||||
expect(body).to.equal(b'Found brand')
|
||||
expect(HTTPretty.last_request.method).to.equal('GET')
|
||||
expect(HTTPretty.last_request.path).to.equal('/v1/deal;brand=gap')
|
||||
|
@ -31,7 +31,19 @@ from __future__ import unicode_literals
|
||||
import re
|
||||
import requests
|
||||
from sure import within, microseconds, expect
|
||||
from httpretty import HTTPretty, httprettified
|
||||
from httpretty import HTTPretty, httprettified, decode_utf8
|
||||
|
||||
try:
|
||||
xrange = xrange
|
||||
except NameError:
|
||||
xrange = range
|
||||
|
||||
try:
|
||||
advance_iterator = next
|
||||
except NameError:
|
||||
def advance_iterator(it):
|
||||
return it.next()
|
||||
next = advance_iterator
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -162,8 +174,8 @@ def test_rotating_responses_with_requests(now):
|
||||
HTTPretty.register_uri(
|
||||
HTTPretty.GET, "https://api.yahoo.com/test",
|
||||
responses=[
|
||||
HTTPretty.Response(body="first response", status=201),
|
||||
HTTPretty.Response(body='second and last response', status=202),
|
||||
HTTPretty.Response(body=b"first response", status=201),
|
||||
HTTPretty.Response(body=b'second and last response', status=202),
|
||||
])
|
||||
|
||||
response1 = requests.get(
|
||||
@ -203,12 +215,12 @@ def test_can_inspect_last_request(now):
|
||||
|
||||
expect(HTTPretty.last_request.method).to.equal('POST')
|
||||
expect(HTTPretty.last_request.body).to.equal(
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
)
|
||||
expect(HTTPretty.last_request.headers['content-type']).to.equal(
|
||||
'text/json',
|
||||
)
|
||||
expect(response.json).to.equal({"repositories": ["HTTPretty", "lettuce"]})
|
||||
expect(response.json()).to.equal({"repositories": ["HTTPretty", "lettuce"]})
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -229,12 +241,12 @@ def test_can_inspect_last_request_with_ssl(now):
|
||||
|
||||
expect(HTTPretty.last_request.method).to.equal('POST')
|
||||
expect(HTTPretty.last_request.body).to.equal(
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
)
|
||||
expect(HTTPretty.last_request.headers['content-type']).to.equal(
|
||||
'text/json',
|
||||
)
|
||||
expect(response.json).to.equal({"repositories": ["HTTPretty", "lettuce"]})
|
||||
expect(response.json()).to.equal({"repositories": ["HTTPretty", "lettuce"]})
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -292,43 +304,43 @@ def test_streaming_responses(now):
|
||||
# taken from the requests docs
|
||||
# Http://docs.python-requests.org/en/latest/user/advanced/#streaming-requests
|
||||
response = requests.post(TWITTER_STREAMING_URL, data={'track': 'requests'},
|
||||
auth=('username', 'password'), prefetch=False)
|
||||
auth=('username', 'password'), stream=True)
|
||||
|
||||
#test iterating by line
|
||||
line_iter = response.iter_lines()
|
||||
with in_time(0.01, 'Iterating by line is taking forever!'):
|
||||
for i in xrange(len(twitter_response_lines)):
|
||||
expect(line_iter.next().strip()).to.equal(
|
||||
expect(next(line_iter).strip()).to.equal(
|
||||
twitter_response_lines[i].strip())
|
||||
|
||||
#test iterating by line after a second request
|
||||
response = requests.post(TWITTER_STREAMING_URL, data={'track': 'requests'},
|
||||
auth=('username', 'password'), prefetch=False)
|
||||
auth=('username', 'password'), stream=True)
|
||||
|
||||
line_iter = response.iter_lines()
|
||||
with in_time(0.01, 'Iterating by line is taking forever the second time '
|
||||
'around!'):
|
||||
for i in xrange(len(twitter_response_lines)):
|
||||
expect(line_iter.next().strip()).to.equal(
|
||||
expect(next(line_iter).strip()).to.equal(
|
||||
twitter_response_lines[i].strip())
|
||||
|
||||
#test iterating by char
|
||||
response = requests.post(TWITTER_STREAMING_URL, data={'track': 'requests'},
|
||||
auth=('username', 'password'), prefetch=False)
|
||||
auth=('username', 'password'), stream=True)
|
||||
|
||||
twitter_expected_response_body = ''.join(twitter_response_lines)
|
||||
twitter_expected_response_body = b''.join(twitter_response_lines)
|
||||
with in_time(0.02, 'Iterating by char is taking forever!'):
|
||||
twitter_body = u''.join(c for c in response.iter_content(chunk_size=1))
|
||||
twitter_body = b''.join(c for c in response.iter_content(chunk_size=1))
|
||||
|
||||
expect(twitter_body).to.equal(twitter_expected_response_body)
|
||||
|
||||
#test iterating by chunks larger than the stream
|
||||
|
||||
response = requests.post(TWITTER_STREAMING_URL, data={'track': 'requests'},
|
||||
auth=('username', 'password'), prefetch=False)
|
||||
auth=('username', 'password'), stream=True)
|
||||
|
||||
with in_time(0.02, 'Iterating by large chunks is taking forever!'):
|
||||
twitter_body = u''.join(c for c in
|
||||
twitter_body = b''.join(c for c in
|
||||
response.iter_content(chunk_size=1024))
|
||||
|
||||
expect(twitter_body).to.equal(twitter_expected_response_body)
|
||||
@ -337,7 +349,7 @@ def test_streaming_responses(now):
|
||||
@httprettified
|
||||
def test_multiline():
|
||||
url = 'http://httpbin.org/post'
|
||||
data = 'content=Im\r\na multiline\r\n\r\nsentence\r\n'
|
||||
data = b'content=Im\r\na multiline\r\n\r\nsentence\r\n'
|
||||
headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
|
||||
'Accept': 'text/plain',
|
||||
@ -360,7 +372,7 @@ def test_multiline():
|
||||
@httprettified
|
||||
def test_multipart():
|
||||
url = 'http://httpbin.org/post'
|
||||
data = '--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="content"\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 68\r\n\r\nAction: comment\nText: Comment with attach\nAttachment: x1.txt, x2.txt\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_2"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_1"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz--\r\n'
|
||||
data = b'--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="content"\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 68\r\n\r\nAction: comment\nText: Comment with attach\nAttachment: x1.txt, x2.txt\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_2"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_1"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz--\r\n'
|
||||
headers = {'Content-Length': '495', 'Content-Type': 'multipart/form-data; boundary=xXXxXXyYYzzz', 'Accept': 'text/plain'}
|
||||
HTTPretty.register_uri(
|
||||
HTTPretty.POST,
|
||||
@ -383,7 +395,7 @@ def test_callback_response(now):
|
||||
" requests")
|
||||
|
||||
def request_callback(method, uri, headers):
|
||||
return "The {0} response from {1}".format(method, uri)
|
||||
return "The {0} response from {1}".format(decode_utf8(method), uri)
|
||||
|
||||
HTTPretty.register_uri(
|
||||
HTTPretty.GET, "https://api.yahoo.com/test",
|
||||
@ -391,7 +403,7 @@ def test_callback_response(now):
|
||||
|
||||
response = requests.get('https://api.yahoo.com/test')
|
||||
|
||||
expect(response.text).to.equal('The GET response from https://api.yahoo.com/test')
|
||||
expect(response.text).to.equal("The GET response from https://api.yahoo.com/test")
|
||||
|
||||
HTTPretty.register_uri(
|
||||
HTTPretty.POST, "https://api.yahoo.com/test_post",
|
||||
|
@ -28,13 +28,13 @@ from __future__ import unicode_literals
|
||||
|
||||
try:
|
||||
from urllib.request import urlopen
|
||||
import urllib as urllib2
|
||||
import urllib.request as urllib2
|
||||
except ImportError:
|
||||
import urllib2
|
||||
urlopen = urllib2.urlopen
|
||||
|
||||
from sure import *
|
||||
from httpretty import HTTPretty, httprettified
|
||||
from httpretty import HTTPretty, httprettified, decode_utf8
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -49,7 +49,7 @@ def test_httpretty_should_mock_a_simple_get_with_urllib2_read():
|
||||
got = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got).to.equal('Find the best daily deals')
|
||||
expect(got).to.equal(b'Find the best daily deals')
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -188,19 +188,19 @@ def test_httpretty_should_support_a_list_of_successive_responses_urllib2(now):
|
||||
request1.close()
|
||||
|
||||
expect(request1.code).to.equal(201)
|
||||
expect(body1).to.equal('first response')
|
||||
expect(body1).to.equal(b'first response')
|
||||
|
||||
request2 = urlopen('https://api.yahoo.com/test')
|
||||
body2 = request2.read()
|
||||
request2.close()
|
||||
expect(request2.code).to.equal(202)
|
||||
expect(body2).to.equal('second and last response')
|
||||
expect(body2).to.equal(b'second and last response')
|
||||
|
||||
request3 = urlopen('https://api.yahoo.com/test')
|
||||
body3 = request3.read()
|
||||
request3.close()
|
||||
expect(request3.code).to.equal(202)
|
||||
expect(body3).to.equal('second and last response')
|
||||
expect(body3).to.equal(b'second and last response')
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -213,7 +213,7 @@ def test_can_inspect_last_request(now):
|
||||
|
||||
request = urllib2.Request(
|
||||
'http://api.github.com',
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
{
|
||||
'content-type': 'text/json',
|
||||
},
|
||||
@ -224,12 +224,12 @@ def test_can_inspect_last_request(now):
|
||||
|
||||
expect(HTTPretty.last_request.method).to.equal('POST')
|
||||
expect(HTTPretty.last_request.body).to.equal(
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
)
|
||||
expect(HTTPretty.last_request.headers['content-type']).to.equal(
|
||||
'text/json',
|
||||
)
|
||||
expect(got).to.equal('{"repositories": ["HTTPretty", "lettuce"]}')
|
||||
expect(got).to.equal(b'{"repositories": ["HTTPretty", "lettuce"]}')
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -242,7 +242,7 @@ def test_can_inspect_last_request_with_ssl(now):
|
||||
|
||||
request = urllib2.Request(
|
||||
'https://secure.github.com',
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
{
|
||||
'content-type': 'text/json',
|
||||
},
|
||||
@ -253,12 +253,12 @@ def test_can_inspect_last_request_with_ssl(now):
|
||||
|
||||
expect(HTTPretty.last_request.method).to.equal('POST')
|
||||
expect(HTTPretty.last_request.body).to.equal(
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
)
|
||||
expect(HTTPretty.last_request.headers['content-type']).to.equal(
|
||||
'text/json',
|
||||
)
|
||||
expect(got).to.equal('{"repositories": ["HTTPretty", "lettuce"]}')
|
||||
expect(got).to.equal(b'{"repositories": ["HTTPretty", "lettuce"]}')
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -273,7 +273,7 @@ def test_httpretty_ignores_querystrings_from_registered_uri():
|
||||
got = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got).to.equal('Find the best daily deals')
|
||||
expect(got).to.equal(b'Find the best daily deals')
|
||||
expect(HTTPretty.last_request.method).to.equal('GET')
|
||||
expect(HTTPretty.last_request.path).to.equal('/?id=123')
|
||||
|
||||
@ -285,7 +285,7 @@ def test_callback_response(now):
|
||||
" urllib2")
|
||||
|
||||
def request_callback(method, uri, headers):
|
||||
return "The {0} response from {1}".format(method, uri)
|
||||
return "The {0} response from {1}".format(decode_utf8(method), uri)
|
||||
|
||||
HTTPretty.register_uri(
|
||||
HTTPretty.GET, "https://api.yahoo.com/test",
|
||||
@ -295,7 +295,7 @@ def test_callback_response(now):
|
||||
got = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got).to.equal('The GET response from https://api.yahoo.com/test')
|
||||
expect(got).to.equal(b"The GET response from https://api.yahoo.com/test")
|
||||
|
||||
HTTPretty.register_uri(
|
||||
HTTPretty.POST, "https://api.yahoo.com/test_post",
|
||||
@ -303,7 +303,7 @@ def test_callback_response(now):
|
||||
|
||||
request = urllib2.Request(
|
||||
"https://api.yahoo.com/test_post",
|
||||
'{"username": "gabrielfalcao"}',
|
||||
b'{"username": "gabrielfalcao"}',
|
||||
{
|
||||
'content-type': 'text/json',
|
||||
},
|
||||
@ -312,7 +312,7 @@ def test_callback_response(now):
|
||||
got = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got).to.equal("The POST response from https://api.yahoo.com/test_post")
|
||||
expect(got).to.equal(b"The POST response from https://api.yahoo.com/test_post")
|
||||
|
||||
|
||||
@httprettified
|
||||
@ -332,4 +332,4 @@ def test_httpretty_should_allow_registering_regexes():
|
||||
got = fd.read()
|
||||
fd.close()
|
||||
|
||||
expect(got).to.equal("Found brand")
|
||||
expect(got).to.equal(b"Found brand")
|
||||
|
@ -27,7 +27,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from sure import expect
|
||||
from httpretty import HTTPretty, HTTPrettyError, STATUSES
|
||||
from httpretty import HTTPretty, HTTPrettyError, STATUSES, URIInfo
|
||||
|
||||
|
||||
def test_httpretty_should_raise_proper_exception_on_inconsistent_length():
|
||||
@ -49,6 +49,7 @@ def test_httpretty_should_raise_proper_exception_on_inconsistent_length():
|
||||
|
||||
def test_does_not_have_last_request_by_default():
|
||||
u'HTTPretty.last_request is a dummy object by default'
|
||||
HTTPretty.reset()
|
||||
|
||||
expect(HTTPretty.last_request.headers).to.be.empty
|
||||
expect(HTTPretty.last_request.body).to.be.empty
|
||||
@ -134,3 +135,21 @@ def test_status_codes():
|
||||
598: "Network read timeout error",
|
||||
599: "Network connect timeout error",
|
||||
})
|
||||
|
||||
|
||||
def test_uri_info_full_url():
|
||||
uri_info = URIInfo(
|
||||
username='johhny',
|
||||
password='password',
|
||||
hostname=b'google.com',
|
||||
port=80,
|
||||
path=b'/',
|
||||
query=b'foo=bar&baz=test',
|
||||
fragment='',
|
||||
scheme='',
|
||||
)
|
||||
|
||||
expect(uri_info.full_url()).to.equal(
|
||||
"http://johhny:password@google.com/?foo=bar&baz=test"
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user