def test_http_pass(self): """Does http authentication work correctly""" responses.add(responses.GET, "http://127.0.0.1/index.html", body='line1\nline2') _ = smart_open.HttpOpenRead(smart_open.ParseUri("http://127.0.0.1/index.html"), user='******', password='******') self.assertEquals(len(responses.calls), 1) actual_request = responses.calls[0].request self.assert_('Authorization' in actual_request.headers) self.assert_(actual_request.headers['Authorization'].startswith('Basic '))
def test_https_readline(self): """Does https readline method work correctly""" responses.add(responses.GET, "https://127.0.0.1/index.html", body='line1\nline2') smart_open_object = smart_open.HttpOpenRead( smart_open.ParseUri("https://127.0.0.1/index.html")) self.assertEqual(smart_open_object.readline().decode("utf-8"), "line1")
def test_http_gz(self): """Can open gzip via http?""" fpath = os.path.join(CURR_DIR, 'test_data/crlf_at_1k_boundary.warc.gz') data = open(fpath, 'rb').read() responses.add(responses.GET, "http://127.0.0.1/data.gz", body=data) smart_open_object = smart_open.HttpOpenRead( smart_open.ParseUri("http://127.0.0.1/data.gz")) m = hashlib.md5(smart_open_object.read()) # decompress the gzip and get the same md5 hash self.assertEqual(m.hexdigest(), '18473e60f8c7c98d29d65bf805736a0d')
def test_http_bz2(self): """Can open bz2 via http?""" test_string = b'Hello World Compressed.' test_file = tempfile.NamedTemporaryFile('wb', suffix='.bz2', delete=False).name with smart_open.smart_open(test_file, 'wb') as outfile: outfile.write(test_string) with open(test_file, 'rb') as infile: compressed_data = infile.read() if os.path.isfile(test_file): os.unlink(test_file) responses.add(responses.GET, "http://127.0.0.1/data.bz2", body=compressed_data) smart_open_object = smart_open.HttpOpenRead( smart_open.ParseUri("http://127.0.0.1/data.bz2")) # decompress the gzip and get the same md5 hash self.assertEqual(smart_open_object.read(), test_string)