def test_getconnection(self): """Test HTTPClient._getconnection.""" # http http = HTTPClient("127.0.0.1", 80) con = http._getconnection() self.assertTrue(isinstance(con, httplib.HTTPConnection)) # https http = HTTPClient("127.0.0.1", 80, protocol="https") con = http._getconnection() self.assertTrue(isinstance(con, httplib.HTTPSConnection)) http = HTTPClient("127.0.0.1", timeout=300, source_address="here.loc") # Python2.5 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=False, PYTHON2_7=False, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] self.assertFalse(call_log["strict"]) self.assertEqual(call_log.get("timeout"), None) self.assertEqual(call_log.get("source_address"), None) # Python2.6 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=True, PYTHON2_7=False, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] self.assertFalse(call_log["strict"]) self.assertEqual(call_log["timeout"], 300) self.assertEqual(call_log.get("source_address"), None) # Python2.7 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=True, PYTHON2_7=True, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] self.assertFalse(call_log["strict"]) self.assertEqual(call_log["timeout"], 300) self.assertEqual(call_log.get("source_address"), "here.loc")
def test_getconnection(self): """Test HTTPClient._getconnection.""" # http http = HTTPClient("127.0.0.1", 80) con = http._getconnection() assert isinstance(con, httplib.HTTPConnection) # https http = HTTPClient("127.0.0.1", 80, protocol="https") con = http._getconnection() assert isinstance(con, httplib.HTTPSConnection) http = HTTPClient("127.0.0.1", timeout=300, source_address="here.loc") # Python2.5 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=False, PYTHON2_7=False, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] assert not call_log.get("strict") assert call_log.get("timeout") == None assert call_log.get("source_address") == None # Python2.6 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=True, PYTHON2_7=False, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] assert not call_log.get("strict") assert call_log["timeout"] == 300 assert call_log.get("source_address") == None # Python2.7 mockhttplib = Mock.Omnivore(HTTPConnection=[None]) context = dict( PYTHON2_6=True, PYTHON2_7=True, httplib=mockhttplib, ) with injected(http._getconnection, **context): http._getconnection() call_log = mockhttplib.called["HTTPConnection"][0][1] assert not call_log.get("strict") assert call_log["timeout"] == 300 assert call_log.get("source_address") == "here.loc"
def test_make_multipart_iso_explicit(self): # form-data with explicit iso-8859-1 context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b=("äöüß", "iso-8859-1")) (headers, multi) = util.make_multipart(content) assert headers["Content-Type"] == \ 'multipart/form-data; boundary="foobar"' assert multi.strip() == MIME_ISO_EXPLICIT
def test_post_py25(self): """Test HTTPClient.post with Python 2.5.""" data = StringIO("Test data") # prepare mock connection self.con.response.status = 200 with injected(self.http.post, PYTHON2_6=False): assert self.http.post("/index", data) == 200 assert self.con.method == "POST" assert self.con.path == "/index" assert self.con.closed
def test_make_multipart_file_explicit(self): # post one file with explicit content-type sio = StringIO("This is a test file.") context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b=(sio, "text/plain")) (headers, multi) = util.make_multipart(content) assert headers["Content-Type"] == \ 'multipart/form-data; boundary="foobar"' assert multi.strip() == MIME_FILE_EXPLICIT.format(newline_if_py3)
def test_make_multipart(self): """Test util.make_multipart.""" # form-data context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b="bar") (headers, multi) = util.make_multipart(content) assert headers["Content-Type"] == \ 'multipart/form-data; boundary="foobar"' assert multi.strip() == MULTI
def test_post_py25(self): """Test HTTPClient.post with Python 2.5.""" data = StringIO("Test data") # prepare mock connection self.con.response.status = 200 query = {"path": "/foo/bar"} with injected(self.http.post, PYTHON2_6=False): self.assertEqual(self.http.post("/index", data), 200) self.assertEqual(self.con.method, "POST") self.assertEqual(self.con.path, "/index") self.assertTrue(self.con.closed)
def test_make_multipart_file_name(self): # post one file with filename sio = StringIO("This is a test file.") sio.name = "test.txt" context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b=sio) (headers, multi) = util.make_multipart(content, with_filenames=True) assert headers["Content-Type"] == \ 'multipart/form-data; boundary="foobar"' assert multi.strip() == MIME_FILE_NAME.format(newline_if_py3)
def test_post_form_data(self): """Test HTTPClient.post form-data.""" data = dict(a="foo", b="bar") def urlencode(data): urlencode.count += 1 return urllib.urlencode(data) urlencode.count = 0 # prepare mock connection mockurllib = Mock.Omnivore() mockurllib.quote = urllib.quote mockurllib.urlencode = urlencode context = dict( urllib_quote=mockurllib.quote, urllib_urlencode=mockurllib.urlencode, ) with injected(self.http.post, **context): resp = self.http.post("/index", data) self.assertEqual(urlencode.count, 1) self.assertEqual(resp, 200)
def test_post_form_data(self): """Test HTTPClient.post form-data.""" data = dict(a="foo", b="bar") def urlencode(data): urlencode.count += 1 return urllib.urlencode(data) urlencode.count = 0 # prepare mock connection mockurllib = Mock.Omnivore() mockurllib.quote = urllib.quote mockurllib.urlencode = urlencode context = dict( urllib_quote=mockurllib.quote, urllib_urlencode=mockurllib.urlencode, ) with injected(self.http.post, **context): resp = self.http.post("/index", data) assert urlencode.count == 1 assert resp == 200
def test_make_multipart(self): """Test util.make_multipart.""" # form-data context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b="bar") (headers, multi) = util.make_multipart(content) self.assertEqual( headers["Content-Type"], 'multipart/form-data; boundary="foobar"' ) self.assertEqual(multi, MULTI) # form-data with iso-8859-1 context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b="äöüß") (headers, multi) = util.make_multipart(content, "iso-8859-1") self.assertEqual( headers["Content-Type"], 'multipart/form-data; boundary="foobar"' ) self.assertEqual(multi, MULTI_ISO) # form-data with explicit iso-8859-1 context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b=("äöüß", "iso-8859-1")) (headers, multi) = util.make_multipart(content) self.assertEqual( headers["Content-Type"], 'multipart/form-data; boundary="foobar"' ) self.assertEqual(multi, MIME_ISO_EXPLICIT) # post one file sio = StringIO("This is a test file.") context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b=sio) (headers, multi) = util.make_multipart(content) self.assertEqual( headers["Content-Type"], 'multipart/form-data; boundary="foobar"' ) self.assertEqual(multi, MIME_FILE) # post one file with filename sio = StringIO("This is a test file.") sio.name = "test.txt" context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b=sio) (headers, multi) = util.make_multipart(content, with_filenames=True) self.assertEqual( headers["Content-Type"], 'multipart/form-data; boundary="foobar"' ) self.assertEqual(multi, MIME_FILE_NAME) # post one file with explicit content-type sio = StringIO("This is a test file.") context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b=(sio, "text/plain")) (headers, multi) = util.make_multipart(content) self.assertEqual( headers["Content-Type"], 'multipart/form-data; boundary="foobar"' ) self.assertEqual(multi, MIME_FILE_EXPLICIT) # post two files, one with filename sio = StringIO("This is a test file.") sio2 = StringIO("This is another test file.") sio2.name = "test2.txt" context = dict(MIMEMultipart=Mock.FakeMIMEMultipart()) with injected(util.make_multipart, **context): content = dict(a="foo", b=sio, c=sio2) (headers, multi) = util.make_multipart(content, with_filenames=True) self.assertEqual( headers["Content-Type"], 'multipart/form-data; boundary="foobar"' ) self.assertEqual(multi, MIME_FILES)