summaryrefslogtreecommitdiff
path: root/tests/write_test.py
blob: 09d3c60e134cf1d13553a8b3b3e72ad734eb249d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vi:ts=4:et

from . import localhost
try:
    import unittest2 as unittest
except ImportError:
    import unittest
import pycurl
import tempfile
import shutil
import os.path

from . import appmanager
from . import util

setup_module, teardown_module = appmanager.setup(('app', 8380))

class Acceptor(object):
    def __init__(self):
        self.buffer = ''

    def write(self, chunk):
        self.buffer += chunk.decode()

class WriteTest(unittest.TestCase):
    def setUp(self):
        self.curl = util.DefaultCurl()

    def tearDown(self):
        self.curl.close()

    def test_write_to_tempfile_via_function(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        f = tempfile.NamedTemporaryFile()
        try:
            self.curl.setopt(pycurl.WRITEFUNCTION, f.write)
            self.curl.perform()
            f.seek(0)
            body = f.read()
        finally:
            f.close()
        self.assertEqual('success', body.decode())

    def test_write_to_tempfile_via_object(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        f = tempfile.NamedTemporaryFile()
        try:
            self.curl.setopt(pycurl.WRITEDATA, f)
            self.curl.perform()
            f.seek(0)
            body = f.read()
        finally:
            f.close()
        self.assertEqual('success', body.decode())

    def test_write_to_file_via_function(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        dir = tempfile.mkdtemp()
        try:
            path = os.path.join(dir, 'pycurltest')
            f = open(path, 'wb+')
            try:
                self.curl.setopt(pycurl.WRITEFUNCTION, f.write)
                self.curl.perform()
                f.seek(0)
                body = f.read()
            finally:
                f.close()
        finally:
            shutil.rmtree(dir)
        self.assertEqual('success', body.decode())

    def test_write_to_file_via_object(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        dir = tempfile.mkdtemp()
        try:
            path = os.path.join(dir, 'pycurltest')
            f = open(path, 'wb+')
            try:
                self.curl.setopt(pycurl.WRITEDATA, f)
                self.curl.perform()
                f.seek(0)
                body = f.read()
            finally:
                f.close()
        finally:
            shutil.rmtree(dir)
        self.assertEqual('success', body.decode())

    def test_write_to_file_like(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEDATA, acceptor)
        self.curl.perform()
        self.assertEqual('success', acceptor.buffer)
    
    @util.with_real_write_file
    def test_write_to_file_like_then_real_file(self, real_f):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEDATA, acceptor)
        self.curl.perform()
        self.assertEqual('success', acceptor.buffer)

        self.curl.setopt(pycurl.WRITEDATA, real_f)
        self.curl.perform()
        real_f.seek(0)
        body = real_f.read()
        self.assertEqual('success', body.decode())

    def test_headerfunction_and_writefunction(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        header_acceptor = Acceptor()
        body_acceptor = Acceptor()
        self.curl.setopt(pycurl.HEADERFUNCTION, header_acceptor.write)
        self.curl.setopt(pycurl.WRITEFUNCTION, body_acceptor.write)
        self.curl.perform()
        self.assertEqual('success', body_acceptor.buffer)
        self.assertIn('content-type', header_acceptor.buffer.lower())

    def test_writeheader_and_writedata_file_like(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        header_acceptor = Acceptor()
        body_acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEHEADER, header_acceptor)
        self.curl.setopt(pycurl.WRITEDATA, body_acceptor)
        self.curl.perform()
        self.assertEqual('success', body_acceptor.buffer)
        self.assertIn('content-type', header_acceptor.buffer.lower())

    @util.with_real_write_file
    @util.with_real_write_file
    def test_writeheader_and_writedata_real_file(self, real_f_header, real_f_data):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        self.curl.setopt(pycurl.WRITEHEADER, real_f_header)
        self.curl.setopt(pycurl.WRITEDATA, real_f_data)
        self.curl.perform()
        real_f_header.seek(0)
        real_f_data.seek(0)
        self.assertEqual('success', real_f_data.read().decode())
        self.assertIn('content-type', real_f_header.read().decode().lower())

    def test_writedata_and_writefunction_file_like(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        data_acceptor = Acceptor()
        function_acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEDATA, data_acceptor)
        self.curl.setopt(pycurl.WRITEFUNCTION, function_acceptor.write)
        self.curl.perform()
        self.assertEqual('', data_acceptor.buffer)
        self.assertEqual('success', function_acceptor.buffer)

    @util.with_real_write_file
    def test_writedata_and_writefunction_real_file(self, real_f):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        function_acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEDATA, real_f)
        self.curl.setopt(pycurl.WRITEFUNCTION, function_acceptor.write)
        self.curl.perform()
        real_f.seek(0)
        self.assertEqual('', real_f.read().decode().lower())
        self.assertEqual('success', function_acceptor.buffer)

    def test_writefunction_and_writedata_file_like(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        data_acceptor = Acceptor()
        function_acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEFUNCTION, function_acceptor.write)
        self.curl.setopt(pycurl.WRITEDATA, data_acceptor)
        self.curl.perform()
        self.assertEqual('success', data_acceptor.buffer)
        self.assertEqual('', function_acceptor.buffer)

    @util.with_real_write_file
    def test_writefunction_and_writedata_real_file(self, real_f):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        function_acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEFUNCTION, function_acceptor.write)
        self.curl.setopt(pycurl.WRITEDATA, real_f)
        self.curl.perform()
        real_f.seek(0)
        self.assertEqual('success', real_f.read().decode().lower())
        self.assertEqual('', function_acceptor.buffer)

    def test_writeheader_and_headerfunction_file_like(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        data_acceptor = Acceptor()
        function_acceptor = Acceptor()
        body_acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEHEADER, data_acceptor)
        self.curl.setopt(pycurl.HEADERFUNCTION, function_acceptor.write)
        # silence output
        self.curl.setopt(pycurl.WRITEDATA, body_acceptor)
        self.curl.perform()
        self.assertEqual('', data_acceptor.buffer)
        self.assertIn('content-type', function_acceptor.buffer.lower())

    @util.with_real_write_file
    def test_writeheader_and_headerfunction_real_file(self, real_f):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        function_acceptor = Acceptor()
        body_acceptor = Acceptor()
        self.curl.setopt(pycurl.WRITEHEADER, real_f)
        self.curl.setopt(pycurl.HEADERFUNCTION, function_acceptor.write)
        # silence output
        self.curl.setopt(pycurl.WRITEDATA, body_acceptor)
        self.curl.perform()
        real_f.seek(0)
        self.assertEqual('', real_f.read().decode().lower())
        self.assertIn('content-type', function_acceptor.buffer.lower())

    def test_headerfunction_and_writeheader_file_like(self):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        data_acceptor = Acceptor()
        function_acceptor = Acceptor()
        body_acceptor = Acceptor()
        self.curl.setopt(pycurl.HEADERFUNCTION, function_acceptor.write)
        self.curl.setopt(pycurl.WRITEHEADER, data_acceptor)
        # silence output
        self.curl.setopt(pycurl.WRITEDATA, body_acceptor)
        self.curl.perform()
        self.assertIn('content-type', data_acceptor.buffer.lower())
        self.assertEqual('', function_acceptor.buffer)

    @util.with_real_write_file
    def test_headerfunction_and_writeheader_real_file(self, real_f):
        self.curl.setopt(pycurl.URL, 'http://%s:8380/success' % localhost)
        function_acceptor = Acceptor()
        body_acceptor = Acceptor()
        self.curl.setopt(pycurl.HEADERFUNCTION, function_acceptor.write)
        self.curl.setopt(pycurl.WRITEHEADER, real_f)
        # silence output
        self.curl.setopt(pycurl.WRITEDATA, body_acceptor)
        self.curl.perform()
        real_f.seek(0)
        self.assertIn('content-type', real_f.read().decode().lower())
        self.assertEqual('', function_acceptor.buffer)

    def test_writedata_not_file_like(self):
        not_file_like = object()
        try:
            self.curl.setopt(self.curl.WRITEDATA, not_file_like)
        except TypeError as exc:
            self.assertIn('object given without a write method', str(exc))
        else:
            self.fail('TypeError not raised')

    def test_writeheader_not_file_like(self):
        not_file_like = object()
        try:
            self.curl.setopt(self.curl.WRITEHEADER, not_file_like)
        except TypeError as exc:
            self.assertIn('object given without a write method', str(exc))
        else:
            self.fail('TypeError not raised')