1#! /usr/bin/env python
2# -*- coding: utf-8 -*-
3# vi:ts=4:et
4
5import os.path
6import pycurl
7import sys
8import unittest
9
10class WriteAbortTest(unittest.TestCase):
11    def setUp(self):
12        self.curl = pycurl.Curl()
13
14    def tearDown(self):
15        self.curl.close()
16
17    def write_cb_returning_string(self, data):
18        return 'foo'
19
20    def write_cb_returning_float(self, data):
21        return 0.5
22
23    def test_write_cb_returning_string(self):
24        self.check(self.write_cb_returning_string)
25
26    def test_write_cb_returning_float(self):
27        self.check(self.write_cb_returning_float)
28
29    def check(self, write_cb):
30        # download the script itself through the file:// protocol into write_cb
31        self.curl.setopt(pycurl.URL, 'file://' + os.path.abspath(__file__).replace('\\', '/'))
32        self.curl.setopt(pycurl.WRITEFUNCTION, write_cb)
33        try:
34            self.curl.perform()
35
36            self.fail('Should not get here')
37        except pycurl.error:
38            err, msg = sys.exc_info()[1].args
39            # we expect pycurl.E_WRITE_ERROR as the response
40            assert pycurl.E_WRITE_ERROR == err
41
42        # actual error
43        assert hasattr(sys, 'last_type')
44        self.assertEqual(pycurl.error, sys.last_type)
45        assert hasattr(sys, 'last_value')
46        self.assertEqual('write callback must return int or None', str(sys.last_value))
47