Last active
December 16, 2016 16:03
-
-
Save efim-a-efim/fd47bbf76d48494ae4c6a136970f99f4 to your computer and use it in GitHub Desktop.
[python] [test] Mock HTTP response with pre-defined string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
from StringIO import StringIO | |
SERVER_LIST="""test1.pvt | |
test2.pvt | |
test5.pvt | |
test8.pvt | |
test7.pvt | |
""" | |
ADD_HEADERS="""X-Header-Mock-1: asd""" | |
class MockHTTPHandler(urllib2.HTTPHandler): | |
def http_open(self, req): | |
resp = urllib2.addinfourl(StringIO(SERVER_LIST), ADD_HEADERS, req.get_full_url()) | |
resp.code = 200 | |
resp.msg = "OK" | |
return resp | |
mock_url_opener = urllib2.build_opener(MockHTTPHandler) | |
urllib2.install_opener(mock_url_opener) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment