Esempio n. 1
0
    def test_save_as(self):
        ''' save_as stores data in a file keeping the file position. '''
        def tmp_file_name():
            # create a temporary file name (on Python 2.6+ NamedTemporaryFile
            # with delete=False could be used)
            fd, fname = tempfile.mkstemp()
            f = os.fdopen(fd)
            f.close()
            return fname

        test_file = 'abc' * 1024
        boundary = '---------------------------186454651713519341951581030105'
        request = BytesIO(
            to_bytes('\r\n').join(
                map(to_bytes, [
                    '--' + boundary,
                    'Content-Disposition: form-data; name="file1"; filename="random.png"',
                    'Content-Type: image/png', '', test_file,
                    '--' + boundary + '--', ''
                ])))
        p = mp.MultipartParser(request, boundary)
        self.assertEqual(
            p.get('file1').file.read(1024),
            to_bytes(test_file)[:1024])
        tfn = tmp_file_name()
        p.get('file1').save_as(tfn)
        tf = open(tfn, 'rb')
        self.assertEqual(tf.read(), to_bytes(test_file))
        tf.close()
        self.assertEqual(
            p.get('file1').file.read(),
            to_bytes(test_file)[1024:])
Esempio n. 2
0
 def test_big_file(self):
     ''' If the size of an uploaded part exceeds memfile_limit,
         it is written to disk. '''
     test_file = 'abc' * 1024
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(
         to_bytes('\r\n').join(
             map(to_bytes, [
                 '--' + boundary,
                 'Content-Disposition: form-data; name="file1"; filename="random.png"',
                 'Content-Type: image/png', '', test_file, '--' + boundary,
                 'Content-Disposition: form-data; name="file2"; filename="random.png"',
                 'Content-Type: image/png', '', test_file + 'a',
                 '--' + boundary,
                 'Content-Disposition: form-data; name="file3"; filename="random.png"',
                 'Content-Type: image/png', '', test_file * 2,
                 '--' + boundary + '--', ''
             ])))
     p = mp.MultipartParser(request, boundary, memfile_limit=len(test_file))
     self.assertEqual(p.get('file1').file.read(), to_bytes(test_file))
     self.assertTrue(p.get('file1').is_buffered())
     self.assertEqual(p.get('file2').file.read(), to_bytes(test_file + 'a'))
     self.assertFalse(p.get('file2').is_buffered())
     self.assertEqual(p.get('file3').file.read(), to_bytes(test_file * 2))
     self.assertFalse(p.get('file3').is_buffered())
Esempio n. 3
0
 def test_iterlines_maxbuf(self):
     data, limit = 'abcdefgh\nijklmnop\r\nq', 9
     result = [(to_bytes('abcdefgh'),to_bytes('\n')),(to_bytes('ijklmnop'),to_bytes('')),(to_bytes(''),to_bytes('\r\n')),(to_bytes('q'),to_bytes(''))]
     i = mp.MultipartParser(BytesIO(to_bytes(data)), 'foo', buffer_size=limit)._lineiter()
     self.assertEqual(list(i), result)
     data, limit = ('X'*3*1024)+'x\n', 1024
     result = [(to_bytes('X'*1024),to_bytes('')),(to_bytes('X'*1024),to_bytes('')),(to_bytes('X'*1024),to_bytes('')),(to_bytes('x'),to_bytes('\n'))]
     i = mp.MultipartParser(BytesIO(to_bytes(data)), 'foo', buffer_size=limit)._lineiter()
     self.assertEqual(list(i), result)
Esempio n. 4
0
 def test_iterlines_limit(self):
     data, limit = 'abc\ndef\r\nghi', 10
     result = [(to_bytes('abc'),to_bytes('\n')),(to_bytes('def'),to_bytes('\r\n')),(to_bytes('g'),to_bytes(''))]
     i = mp.MultipartParser(BytesIO(to_bytes(data)), 'foo', limit)._lineiter()
     self.assertEqual(list(i), result)
     data, limit = 'abc\ndef\r\nghi', 8
     result = [(to_bytes('abc'),to_bytes('\n')),(to_bytes('def'),to_bytes('\r'))]
     i = mp.MultipartParser(BytesIO(to_bytes(data)), 'foo', limit)._lineiter()
     self.assertEqual(list(i), result)
Esempio n. 5
0
 def test_file_seek(self):
     ''' The file object should be readable withoud a seek(0). '''
     test_file = 'abc'*1024
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(to_bytes('\r\n').join(map(to_bytes,[
     '--' + boundary,
     'Content-Disposition: form-data; name="file1"; filename="random.png"',
     'Content-Type: image/png', '', test_file, '--' + boundary + '--',''])))
     p = mp.MultipartParser(request, boundary)
     self.assertEqual(p.get('file1').file.read(), to_bytes(test_file))
     self.assertEqual(p.get('file1').value, test_file)
Esempio n. 6
0
 def test_unicode_value(self):
     ''' The .value property always returns unicode '''
     test_file = 'abc'*1024
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(to_bytes('\r\n').join(map(to_bytes,[
     '--' + boundary,
     'Content-Disposition: form-data; name="file1"; filename="random.png"',
     'Content-Type: image/png', '', test_file, '--' + boundary + '--',''])))
     p = mp.MultipartParser(request, boundary)
     self.assertEqual(p.get('file1').file.read(), to_bytes(test_file))
     self.assertEqual(p.get('file1').value, test_file)
     self.assertTrue(hasattr(p.get('file1').value, 'encode'))
Esempio n. 7
0
 def test_get_all(self):
     ''' Test the get() and get_all() methods. '''
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(to_bytes('\r\n').join(map(to_bytes,[
     '--' + boundary,
     'Content-Disposition: form-data; name="file1"; filename="random.png"',
     'Content-Type: image/png', '', 'abc'*1024, '--' + boundary,
     'Content-Disposition: form-data; name="file1"; filename="random.png"',
     'Content-Type: image/png', '', 'def'*1024, '--' + boundary + '--',''])))
     p = mp.MultipartParser(request, boundary)
     self.assertEqual(p.get('file1').file.read(), to_bytes('abc'*1024))
     self.assertEqual(p.get('file2'), None)
     self.assertEqual(len(p.get_all('file1')), 2)
     self.assertEqual(p.get_all('file1')[1].file.read(), to_bytes('def'*1024))
     self.assertEqual(p.get_all('file1'), p.parts())
Esempio n. 8
0
def decode_multiform_data(data):
    s_pat = data.split(b"\r")[0][2:]
    parts = multipart.MultipartParser(BytesIO(multipart.to_bytes(data)),
                                      s_pat).parts()
    d_d = {}
    for part_t in parts:
        if part_t.content_type != '':
            d_d[part_t.name] = {
                'isfile': True,
                'filename': part_t.filename,
                'name': part_t.name,
                'file': part_t.file,
                'size': part_t.size,
            }
        else:
            try:
                value = tornado.escape.json_decode(part_t.value)
            except:
                value = part_t.value
            # endtry
            d_d[part_t.name] = {
                'isfile': False,
                'name': part_t.name,
                'value': value,
            }
        # endif
    # endfor
    return d_d
Esempio n. 9
0
 def test_multiline_header(self):
     ''' HTTP allows headers to be multiline. '''
     test_file = to_bytes('abc'*1024)
     test_text = u'Test text\n with\r\n ümläuts!'
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(to_bytes('\r\n').join(map(to_bytes,[
     '--' + boundary,
     'Content-Disposition: form-data;',
     '\tname="file1"; filename="random.png"',
     'Content-Type: image/png', '', test_file, '--' + boundary,
     'Content-Disposition: form-data;',
     ' name="text"', '', test_text,
     '--' + boundary + '--',''])))
     p = mp.MultipartParser(request, boundary, charset='utf8')
     self.assertEqual(p.get('file1').file.read(), test_file)
     self.assertEqual(p.get('file1').filename, 'random.png')
     self.assertEqual(p.get('text').value, test_text)
Esempio n. 10
0
 def test_no_newline_after_middle_content(self):
     forms, files = self.parse('--foo\r\n',
                'Content-Disposition: form-data; name="file1"; filename="random.png"\r\n',
                'Content-Type: image/png\r\n', '\r\n', 'abc', '--foo\r\n'
                'Content-Disposition: form-data; name="file2"; filename="random.png"\r\n',
                'Content-Type: image/png\r\n', '\r\n', 'abc\r\n', '--foo--')
     self.assertEqual(len(files), 1)
     self.assertTrue(to_bytes('name="file2"') in files['file1'].file.read())
Esempio n. 11
0
 def test_preamble_before_start_boundary(self):
     forms, files = self.parse('Preamble\r\n', '--foo\r\n'
                'Content-Disposition: form-data; name="file1"; filename="random.png"\r\n',
                'Content-Type: image/png\r\n', '\r\n', 'abc\r\n', '--foo--')
     self.assertEqual(files['file1'].file.read(), to_bytes('abc'))
     self.assertEqual(files['file1'].filename, 'random.png')
     self.assertEqual(files['file1'].name, 'file1')
     self.assertEqual(files['file1'].content_type, 'image/png')
Esempio n. 12
0
 def test_iterlines(self):
     data = 'abc\ndef\r\nghi'
     result = [(to_bytes('abc'), to_bytes('\n')),
               (to_bytes('def'), to_bytes('\r\n')),
               (to_bytes('ghi'), to_bytes(''))]
     i = mp.MultipartParser(BytesIO(to_bytes(data)), 'foo')._lineiter()
     self.assertEqual(list(i), result)
Esempio n. 13
0
    def read_form_data(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length).decode("utf8")

        parser = multipart.MultipartParser(
            BytesIO(multipart.to_bytes(post_data)),
            post_data.split("\r")[0][2:])
        parts = parser.parts()

        return {part.name: part.value for part in parts}
Esempio n. 14
0
 def test_multipart(self):
    forms, files = self.parse('--foo\r\n',
                'Content-Disposition: form-data; name="file1"; filename="random.png"\r\n',
                'Content-Type: image/png\r\n', '\r\n', 'abc\r\n', '--foo\r\n',
                'Content-Disposition: form-data; name="text1"\r\n', '\r\n',
                'abc\r\n', '--foo--')
    self.assertEqual(forms['text1'], 'abc')
    self.assertEqual(files['file1'].file.read(), to_bytes('abc'))
    self.assertEqual(files['file1'].filename, 'random.png')
    self.assertEqual(files['file1'].name, 'file1')
    self.assertEqual(files['file1'].content_type, 'image/png')
Esempio n. 15
0
 def test_werkzeug_examples(self):
     """Tests multipart parsing against data collected from webbrowsers"""
     for name in browser_test_cases:
         boundary = browser_test_cases[name]['boundary']
         files = browser_test_cases[name]['files']
         forms = browser_test_cases[name]['forms']
         env = {'REQUEST_METHOD': 'POST',
                'CONTENT_TYPE': 'multipart/form-data; boundary=%s'%boundary,
                'wsgi.input': BytesIO(browser_test_cases[name]['data'])}
         rforms, rfiles = mp.parse_form_data(env, strict=True, charset='utf8')
         for field in files:
             self.assertEqual(rfiles[field].name, field)
             self.assertEqual(rfiles[field].filename, files[field][0])
             self.assertEqual(rfiles[field].content_type, files[field][1])
             self.assertEqual(rfiles[field].file.read(), to_bytes(files[field][2]))
         for field in forms:
             self.assertEqual(rforms[field], forms[field])
Esempio n. 16
0
 def test_line_parser(self):
     for line in ('foo', ''):
         for ending in ('\n', '\r', '\r\n'):
             i = mp.MultipartParser(BytesIO(to_bytes(line + ending)), 'foo')
             i = next(i._lineiter())
             self.assertEqual(i, (to_bytes(line), to_bytes(ending)))
Esempio n. 17
0
 def write(self, *lines):
     for line in lines:
         self.data.write(to_bytes(line))
Esempio n. 18
0
 def test_copyfile(self):
     source = BytesIO(to_bytes('abc'))
     target = BytesIO()
     self.assertEqual(mp.copy_file(source, target), 3)
     target.seek(0)
     self.assertEqual(target.read(), to_bytes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