summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfsbs <fsbs@users.noreply.github.com>2021-11-03 08:25:32 +0000
committerfsbs <fsbs@users.noreply.github.com>2021-11-03 09:25:32 +0100
commitf84bc67cb6e8be6674cd0eeea1bc7c09561f12c8 (patch)
tree2d110cbb1b34a6143ceb6ffee082558f0de8acb0
parent9fd19c97f4be2ca0ca8910b44bea74d593e65ecb (diff)
downloadpycurl-f84bc67cb6e8be6674cd0eeea1bc7c09561f12c8.tar.gz
duphandle: add tests
-rw-r--r--tests/duphandle_test.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/tests/duphandle_test.py b/tests/duphandle_test.py
new file mode 100644
index 0000000..103e2fa
--- /dev/null
+++ b/tests/duphandle_test.py
@@ -0,0 +1,94 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+# vi:ts=4:et
+
+from . import localhost
+import pycurl
+import unittest
+try:
+ import json
+except ImportError:
+ import simplejson as json
+
+from . import appmanager
+from . import util
+
+setup_module, teardown_module = appmanager.setup(('app', 8380))
+
+class DuphandleTest(unittest.TestCase):
+ def setUp(self):
+ self.curl = util.DefaultCurl()
+ self.dup = None
+
+ def tearDown(self):
+ if self.dup:
+ self.dup.close()
+
+ def test_duphandle_attribute_dict(self):
+ self.curl.original_attr = 'original-value'
+ # attribute dict should be copied - the *object*, not the reference
+ self.dup = self.curl.duphandle()
+ assert self.dup.original_attr == 'original-value'
+ # cloned dict should be a separate object
+ self.dup.clone_attr = 'clone-value'
+ try:
+ self.curl.clone_attr == 'does not exist'
+ except AttributeError as error:
+ assert 'trying to obtain a non-existing attribute: clone_attr' in str(error.args)
+ else:
+ self.fail('should have raised AttributeError')
+ # decref - original dict is freed from memory
+ self.curl.close()
+ del self.curl
+ # cloned dict should still exist
+ assert self.dup.original_attr == 'original-value'
+ assert self.dup.clone_attr == 'clone-value'
+
+ def test_duphandle_slist(self):
+ self.curl.setopt(pycurl.HTTPHEADER, ['x-test-header: original-slist'])
+ # slist *reference* should be copied and incremented
+ self.dup = self.curl.duphandle()
+ # decref
+ self.curl.close()
+ del self.curl
+ # slist object should still exist
+ body = util.BytesIO()
+ self.dup.setopt(pycurl.WRITEFUNCTION, body.write)
+ self.dup.setopt(pycurl.URL, 'http://%s:8380/header_utf8?h=x-test-header' % localhost)
+ self.dup.perform()
+ result = body.getvalue().decode('utf-8')
+ assert result == 'original-slist'
+
+ def test_duphandle_httppost(self):
+ self.curl.setopt(pycurl.HTTPPOST, [
+ ('field', (pycurl.FORM_CONTENTS, 'original-httppost')),
+ ])
+ # httppost *reference* should be copied and incremented
+ self.dup = self.curl.duphandle()
+ # decref
+ self.curl.close()
+ del self.curl
+ # httppost object should still exist
+ body = util.BytesIO()
+ self.dup.setopt(pycurl.WRITEFUNCTION, body.write)
+ self.dup.setopt(pycurl.URL, 'http://%s:8380/postfields' % localhost)
+ self.dup.perform()
+ result = json.loads(body.getvalue())
+ assert result == {'field': 'original-httppost'}
+
+ def test_duphandle_callback(self):
+ body = util.BytesIO()
+ def callback(data):
+ body.write(data)
+ self.curl.setopt(pycurl.WRITEFUNCTION, callback)
+ # callback *reference* should be copied and incremented
+ self.dup = self.curl.duphandle()
+ # decref
+ self.curl.close()
+ del self.curl
+ del callback
+ # callback object should still exist
+ self.dup.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
+ self.dup.perform()
+ result = body.getvalue().decode('utf-8')
+ assert result == 'success'