Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@within(two=microseconds)
def test_httpretty_should_allow_forcing_headers_httplib2(now):
"HTTPretty should allow forcing headers with httplib2"
HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo",
body="this is supposed to be the response",
forcing_headers={
'Content-Type': 'application/xml',
})
headers, _ = httplib2.Http().request('http://github.com/foo', 'GET')
expect(dict(headers)).to.equal({
'content-location': 'http://github.com/foo', # httplib2 FORCES
# content-location
# even if the
# server does not
@within(two=microseconds)
def test_can_inspect_last_request(now):
"HTTPretty.last_request is a mimetools.Message request from last match"
HTTPretty.register_uri(HTTPretty.POST, "http://api.github.com/",
body='{"repositories": ["HTTPretty", "lettuce"]}')
headers, body = httplib2.Http().request(
'http://api.github.com', 'POST',
body='{"username": "gabrielfalcao"}',
headers={
'content-type': 'text/json',
},
)
expect(HTTPretty.last_request.method).to.equal('POST')
expect(HTTPretty.last_request.body).to.equal(
@within(two=microseconds)
def test_httpretty_ignores_querystrings_from_registered_uri(now):
"HTTPretty should ignore querystrings from the registered uri (requests library)"
HTTPretty.register_uri(HTTPretty.GET, "http://yipit.com/?id=123",
body=b"Find the best daily deals")
response = requests.get('http://yipit.com/', params={'id': 123})
expect(response.text).to.equal('Find the best daily deals')
expect(HTTPretty.last_request.method).to.equal('GET')
expect(HTTPretty.last_request.path).to.equal('/?id=123')
@within(two=microseconds)
def test_rotating_responses_with_requests(now):
"HTTPretty should support rotating responses with requests"
HTTPretty.register_uri(
HTTPretty.GET, "https://api.yahoo.com/test",
responses=[
HTTPretty.Response(body=b"first response", status=201),
HTTPretty.Response(body=b'second and last response', status=202),
])
response1 = requests.get(
'https://api.yahoo.com/test')
expect(response1.status_code).to.equal(201)
expect(response1.text).to.equal('first response')
@within(two=microseconds)
def test_callback_response(now):
("HTTPretty should all a callback function to be set as the body with"
" httplib2")
def request_callback(request, uri, headers):
return [200, headers, "The {} response from {}".format(decode_utf8(request.method), uri)]
HTTPretty.register_uri(
HTTPretty.GET, "https://api.yahoo.com/test",
body=request_callback)
headers1, body1 = httplib2.Http().request(
'https://api.yahoo.com/test', 'GET')
expect(body1).to.equal(b"The GET response from https://api.yahoo.com/test")
@within(two=microseconds)
def test_httpretty_provides_easy_access_to_querystrings(now):
"HTTPretty should provide an easy access to the querystring"
HTTPretty.register_uri(HTTPretty.GET, "http://yipit.com/",
body="Find the best daily deals")
requests.get('http://yipit.com/?foo=bar&foo=baz&chuck=norris')
expect(HTTPretty.last_request.querystring).to.equal({
'foo': ['bar', 'baz'],
'chuck': ['norris'],
})
def test_within_fail():
"within(five=miliseconds) will fail"
import time
from sure import within, miliseconds
def sleepy(*a):
time.sleep(0.7)
failed = False
try:
within(five=miliseconds)(sleepy)()
except AssertionError as e:
failed = True
assert_equals('sleepy did not run within five miliseconds', str(e))
assert failed, 'within(five=miliseconds)(sleepy) did not fail'
@within(two=microseconds)
def test_httpretty_should_allow_forcing_headers_urllib2():
"HTTPretty should allow forcing headers with urllib2"
HTTPretty.register_uri(HTTPretty.GET, "http://github.com/",
body="this is supposed to be the response",
forcing_headers={
'Content-Type': 'application/xml',
'Content-Length': '35a',
})
request = urlopen('http://github.com')
headers = dict(request.headers)
request.close()
headers.should.equal({
'content-type': 'application/xml',
@within(two=microseconds)
def test_httpretty_ignores_querystrings_from_registered_uri():
"HTTPretty should mock a simple GET with urllib2.read()"
HTTPretty.register_uri(HTTPretty.GET, "http://yipit.com/?id=123",
body="Find the best daily deals")
fd = urlopen('http://yipit.com/?id=123')
got = fd.read()
fd.close()
got.should.equal(b'Find the best daily deals')
HTTPretty.last_request.method.should.equal('GET')
HTTPretty.last_request.path.should.equal('/?id=123')