def test_callback_response(now):
("HTTPretty should all a callback function to be set as the body with"
" urllib2")
def request_callback(request, uri, headers):
return [200, headers, "The {0} response from {1}".format(decode_utf8(request.method), uri)]
HTTPretty.register_uri(
HTTPretty.GET, "https://api.yahoo.com/test",
body=request_callback)
fd = urllib2.urlopen('https://api.yahoo.com/test')
got = fd.read()
fd.close()
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",
body=request_callback)
request = urllib2.Request(
"https://api.yahoo.com/test_post",
b'{"username": "gabrielfalcao"}',
{
'content-type': 'text/json',
},
)
fd = urllib2.urlopen(request)
got = fd.read()
fd.close()
expect(got).to.equal(b"The POST response from https://api.yahoo.com/test_post")
评论列表
文章目录