コード例 #1
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 def test_get_all(self):
     """ Test the get() and get_all() methods. """
     boundary = "---------------------------186454651713519341951581030105"
     request = BytesIO(
         tob("\r\n").join(
             map(
                 tob,
                 [
                     "--" + 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(), tob("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(), tob("def" * 1024))
     self.assertEqual(p.get_all("file1"), p.parts())
コード例 #2
0
ファイル: test_multipart.py プロジェクト: sashahart/multipart
 def test_line_parser(self):
     for line in ('foo', ''):
         for ending in ('\n', '\r', '\r\n'):
             iterator = mp.lineiter(BytesIO(tob(line + ending)))
             for item in iterator:
                 self.assertEqual(item, (tob(line), tob(ending)))
                 break  # at first loop, as portable way of spelling .next()
コード例 #3
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(
            tob('\r\n').join(
                map(tob, [
                    '--' + 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), tob(test_file)[:1024])
        tfn = tmp_file_name()
        p.get('file1').save_as(tfn)
        tf = open(tfn, 'rb')
        self.assertEqual(tf.read(), tob(test_file))
        tf.close()
        self.assertEqual(p.get('file1').file.read(), tob(test_file)[1024:])
コード例 #4
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(
         tob('\r\n').join(
             map(tob, [
                 '--' + 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(), tob(test_file))
     self.assertTrue(p.get('file1').is_buffered())
     self.assertEqual(p.get('file2').file.read(), tob(test_file + 'a'))
     self.assertFalse(p.get('file2').is_buffered())
     self.assertEqual(p.get('file3').file.read(), tob(test_file * 2))
     self.assertFalse(p.get('file3').is_buffered())
コード例 #5
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 def test_multiline_header(self):
     """ HTTP allows headers to be multiline. """
     test_file = tob("abc" * 1024)
     test_text = u"Test text\n with\r\n ümläuts!"
     boundary = "---------------------------186454651713519341951581030105"
     request = BytesIO(
         tob("\r\n").join(
             map(
                 tob,
                 [
                     "--" + 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)
コード例 #6
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_file_seek(self):
     ''' The file object should be readable withoud a seek(0). '''
     test_file = 'abc'*1024
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(tob('\r\n').join(map(tob,[
     '--' + 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(), tob(test_file))
     self.assertEqual(p.get('file1').value(2**16), test_file)
コード例 #7
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_encoding_line_too_long(self):
     try:
         base64.encodebytes
     except:
         encode = base64.encodestring
     else:
         encode = base64.encodebytes
     self.write('--foo\r\n',
                'Content-Transfer-Encoding: base64\r\n',
                'Content-Disposition: form-data; name="text1"\r\n', '\r\n',
                encode(b'abc'*1024).replace(tob('\n'), tob('')), '\r\n', '--foo--')
     self.assertMPError(buffer_size=1024)
コード例 #8
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_unicode_value(self):
     ''' The .value property always returns unicode '''
     test_file = 'abc'*1024
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(tob('\r\n').join(map(tob,[
     '--' + 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(), tob(test_file))
     self.assertEqual(p.get('file1').value(2**16), test_file)
     self.assertTrue(hasattr(p.get('file1').value(2**16), 'encode'))
コード例 #9
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_get_all(self):
     ''' Test the get() and get_all() methods. '''
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(tob('\r\n').join(map(tob,[
     '--' + 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(), tob('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(), tob('def'*1024))
     self.assertEqual(p.get_all('file1'), p.parts())
コード例 #10
0
 def test_file_seek(self):
     ''' The file object should be readable withoud a seek(0). '''
     test_file = 'abc' * 1024
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(
         tob('\r\n').join(
             map(tob, [
                 '--' + 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(), tob(test_file))
     self.assertEqual(p.get('file1').value, test_file)
コード例 #11
0
 def test_unicode_value(self):
     ''' The .value property always returns unicode '''
     test_file = 'abc' * 1024
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(
         tob('\r\n').join(
             map(tob, [
                 '--' + 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(), tob(test_file))
     self.assertEqual(p.get('file1').value, test_file)
     self.assertTrue(hasattr(p.get('file1').value, 'encode'))
コード例 #12
0
ファイル: test_client.py プロジェクト: yejr/WeRoBot
 def post_file_callback(request):
     s = request.body.split(b"\r")[0][2:]
     p = list(
         multipart.MultipartParser(BytesIO(multipart.tob(request.body)),
                                   s))[0]
     assert "filename" in p.options
     return 200, JSON_HEADER, json.dumps({"test": "test"})
コード例 #13
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 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(tob('name="file2"') in files['file1'].file.read())
コード例 #14
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_multiline_header(self):
     ''' HTTP allows headers to be multiline. '''
     test_file = tob('abc'*1024)
     test_text = u'Test text\n with\r\n ümläuts!'
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(tob('\r\n').join(map(tob,[
     '--' + 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(2**16), test_text)
コード例 #15
0
ファイル: test_multipart.py プロジェクト: sashahart/multipart
 def test_iterlines(self):
     data = 'abc\ndef\r\nghi'
     result = [(tob('abc'), tob('\n')),
               (tob('def'), tob('\r\n')),
               (tob('ghi'), tob(''))]
     i = mp.lineiter(BytesIO(tob(data)))
     self.assertEqual(list(i), result)
コード例 #16
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(tob('name="file2"') in files['file1'].file.read())
コード例 #17
0
 def test_multiline_header(self):
     ''' HTTP allows headers to be multiline. '''
     test_file = tob('abc' * 1024)
     test_text = u'Test text\n with\r\n ümläuts!'
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(
         tob('\r\n').join(
             map(tob, [
                 '--' + 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)
コード例 #18
0
 def test_get_all(self):
     ''' Test the get() and get_all() methods. '''
     boundary = '---------------------------186454651713519341951581030105'
     request = BytesIO(
         tob('\r\n').join(
             map(tob, [
                 '--' + 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(), tob('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(), tob('def' * 1024))
     self.assertEqual(p.get_all('file1'), p.parts())
コード例 #19
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 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(), tob('abc'))
    self.assertEqual(files['file1'].filename, 'random.png')
    self.assertEqual(files['file1'].name, 'file1')
    self.assertEqual(files['file1'].content_type, 'image/png')
コード例 #20
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 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(tob('\r\n').join(map(tob,[
     '--' + 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(), tob(test_file))
     self.assertTrue(p.get('file1').is_buffered())
     self.assertEqual(p.get('file2').file.read(), tob(test_file + 'a'))
     self.assertFalse(p.get('file2').is_buffered())
     self.assertEqual(p.get('file3').file.read(), tob(test_file*2))
     self.assertFalse(p.get('file3').is_buffered())
コード例 #21
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(), tob('abc'))
     self.assertEqual(files['file1'].filename, 'random.png')
     self.assertEqual(files['file1'].name, 'file1')
     self.assertEqual(files['file1'].content_type, 'image/png')
コード例 #22
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 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(
         tob("\r\n").join(
             map(
                 tob,
                 [
                     "--" + 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(), tob(test_file))
     self.assertTrue(p.get("file1").is_buffered())
     self.assertEqual(p.get("file2").file.read(), tob(test_file + "a"))
     self.assertFalse(p.get("file2").is_buffered())
     self.assertEqual(p.get("file3").file.read(), tob(test_file * 2))
     self.assertFalse(p.get("file3").is_buffered())
コード例 #23
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 def test_file_seek(self):
     """ The file object should be readable withoud a seek(0). """
     test_file = "abc" * 1024
     boundary = "---------------------------186454651713519341951581030105"
     request = BytesIO(
         tob("\r\n").join(
             map(
                 tob,
                 [
                     "--" + 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(), tob(test_file))
     self.assertEqual(p.get("file1").value, test_file)
コード例 #24
0
def upload_file():
    if request.method == 'POST':
        data = request.data
        #s = str(data).split("\r")[0][16:]
        s = data[2:42]
        print("s is ", s)

        p = mp.MultipartParser(BytesIO(tob(data)),s)
        newFile = open("/tmp/screenshot.png", "wb")
        #newFile.write(data)
        newFile.write((p.parts()[0].value.encode("latin-1")))
        #print("Data size: {}".format(len(request.data)))
        
    return "OK"
コード例 #25
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 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(tob('\r\n').join(map(tob,[
     '--' + 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), tob(test_file)[:1024])
     tfn = tmp_file_name()
     p.get('file1').save_as(tfn)
     tf = open(tfn, 'rb')
     self.assertEqual(tf.read(), tob(test_file))
     tf.close()
     self.assertEqual(p.get('file1').file.read(), tob(test_file)[1024:])
コード例 #26
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
    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(
            tob("\r\n").join(
                map(
                    tob,
                    [
                        "--" + 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), tob(test_file)[:1024])
        tfn = tmp_file_name()
        p.get("file1").save_as(tfn)
        tf = open(tfn, "rb")
        self.assertEqual(tf.read(), tob(test_file))
        tf.close()
        self.assertEqual(p.get("file1").file.read(), tob(test_file)[1024:])
コード例 #27
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 def test_unicode_value(self):
     """ The .value property always returns unicode """
     test_file = "abc" * 1024
     boundary = "---------------------------186454651713519341951581030105"
     request = BytesIO(
         tob("\r\n").join(
             map(
                 tob,
                 [
                     "--" + 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(), tob(test_file))
     self.assertEqual(p.get("file1").value, test_file)
     self.assertTrue(hasattr(p.get("file1").value, "encode"))
コード例 #28
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 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(), tob(files[field][2]))
         for field in forms:
             self.assertEqual(rforms[field], forms[field])
コード例 #29
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 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(), tob("abc"))
     self.assertEqual(files["file1"].filename, "random.png")
     self.assertEqual(files["file1"].name, "file1")
     self.assertEqual(files["file1"].content_type, "image/png")
コード例 #30
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 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(), tob(files[field][2]))
         for field in forms:
             self.assertEqual(rforms[field], forms[field])
コード例 #31
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(),
                              tob(files[field][2]))
         for field in forms:
             self.assertEqual(rforms[field], forms[field])
コード例 #32
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_iterlines_limit(self):
     data, limit = 'abc\ndef\r\nghi', 10
     result = [(tob('abc'),tob('\n')),(tob('def'),tob('\r\n')),(tob('g'),tob(''))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo', limit)._lineiter()
     self.assertEqual(list(i), result)
     data, limit = 'abc\ndef\r\nghi', 8
     result = [(tob('abc'),tob('\n')),(tob('def'),tob('\r'))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo', limit)._lineiter()
     self.assertEqual(list(i), result)
コード例 #33
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_line_parser(self):
     for line in ('foo',''):
         for ending in ('\n','\r','\r\n'):
             i = mp.MultipartParser(BytesIO(tob(line+ending)), 'foo')
             i = i._lineiter().next()
             self.assertEqual(i, (tob(line), tob(ending)))
コード例 #34
0
 def write(self, *lines):
     for line in lines:
         self.data.write(tob(line))
コード例 #35
0
 def test_line_parser(self):
     for line in ('foo', ''):
         for ending in ('\n', '\r', '\r\n'):
             i = mp.MultipartParser(BytesIO(tob(line + ending)), 'foo')
             i = i._lineiter().next()
             self.assertEqual(i, (tob(line), tob(ending)))
コード例 #36
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def write(self, *lines):
     for line in lines:
         self.data.write(tob(line))
コード例 #37
0
 def test_iterlines(self):
     data = 'abc\ndef\r\nghi'
     result = [(tob('abc'), tob('\n')), (tob('def'), tob('\r\n')),
               (tob('ghi'), tob(''))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo')._lineiter()
     self.assertEqual(list(i), result)
コード例 #38
0
ファイル: test_client.py プロジェクト: shijingyu/WeRoBot
 def post_file_callback(request):
     s = request.body.split(b"\r")[0][2:]
     p = list(multipart.MultipartParser(BytesIO(multipart.tob(request.body)), s))[0]
     assert "filename" in p.options
     return 200, JSON_HEADER, json.dumps({"test": "test"})
コード例 #39
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 def test_iterlines_limit(self):
     data, limit = "abc\ndef\r\nghi", 10
     result = [(tob("abc"), tob("\n")), (tob("def"), tob("\r\n")), (tob("g"), tob(""))]
     i = mp.MultipartParser(BytesIO(tob(data)), "foo", limit)._lineiter()
     self.assertEqual(list(i), result)
     data, limit = "abc\ndef\r\nghi", 8
     result = [(tob("abc"), tob("\n")), (tob("def"), tob("\r"))]
     i = mp.MultipartParser(BytesIO(tob(data)), "foo", limit)._lineiter()
     self.assertEqual(list(i), result)
コード例 #40
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_copyfile(self):
     source = BytesIO(tob('abc'))
     target = BytesIO()
     self.assertEqual(mp.copy_file(source, target), 3)
     target.seek(0)
     self.assertEqual(target.read(), tob('abc'))
コード例 #41
0
 def test_copyfile(self):
     source = BytesIO(tob('abc'))
     target = BytesIO()
     self.assertEqual(mp.copy_file(source, target), 3)
     target.seek(0)
     self.assertEqual(target.read(), tob('abc'))
コード例 #42
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
browser_test_cases['firefox3-2png1txt'] = {'data': base64.b64decode(tob('''
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0xODY0NTQ2NTE3MTM1MTkzNDE5NTE1ODEwMzAx
MDUNCkNvbnRlbnQtRGlzcG9zaXRpb246IGZvcm0tZGF0YTsgbmFtZT0iZmlsZTEiOyBmaWxlbmFt
ZT0iYW5jaG9yLnBuZyINCkNvbnRlbnQtVHlwZTogaW1hZ2UvcG5nDQoNColQTkcNChoKAAAADUlI
RFIAAAAQAAAAEAgGAAAAH/P/YQAAAARnQU1BAACvyDcFiukAAAAZdEVYdFNvZnR3YXJlAEFkb2Jl
IEltYWdlUmVhZHlxyWU8AAABnUlEQVQ4y6VTMWvCQBS+qwEFB10KGaS1P6FDpw7SrVvzAwRRx04V
Ck4K6iAoDhLXdhFcW9qhZCk4FQoW0gp2U4lQRDAUS4hJmn5Xgg2lsQ198PHu3b3vu5d3L9S2bfIf
47wOer1ewzTNtGEYBP48kUjkfsrb8BIAMb1cLovwRfi07wrYzcCr4/1/Am4FzzhzBGZeefR7E7vd
7j0Iu4wYjUYDBMfD0dBiMUQfstns3toKkHgF6EgmqqruW6bFiHcsxr70awVu63Q6NiOmUinquwfM
dF1f28CVgCRJx0jMAQ1BEFquRn7CbYVCYZVbr9dbnJMohoIh9kViu90WEW9nMpmxu4JyubyF/VEs
FiNcgCPyoyxiu7XhCPBzdU4s652VnUccbDabPLyN2C6VSmwdhFgel5DB84AJb64mEUlvmqadTKcv
40gkUkUsg1DjeZ7iRsrWgByP71T7/afxYrHIYry/eoBD9mxsaK4VRamFw2EBQknMAWGvRCl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+PUtSueUC0cQ0QmpG
yE9197TUnwzq1DnUKbXSxOb6S7xtPkjngzbGVVbzvS/FjaGt9DU8xlRRJdTCMDEzRjuyZ1FwaFe9
j+d4eecaPd1dPxNTSlfWHm1v5y/EzBitblXp4JLZ5f6yBbOwaK5tsD+9c33jq/f8w2+mRSjOllPh
kAAAAABJRU5ErkJggg0KLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0xODY0NTQ2NTE3MTM1
MTkzNDE5NTE1ODEwMzAxMDUNCkNvbnRlbnQtRGlzcG9zaXRpb246IGZvcm0tZGF0YTsgbmFtZT0i
dGV4dCINCg0KZXhhbXBsZSB0ZXh0DQotLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTE4NjQ1
NDY1MTcxMzUxOTM0MTk1MTU4MTAzMDEwNS0tDQo=''')),
コード例 #43
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 def test_iterlines_maxbuf(self):
     data, limit = "abcdefgh\nijklmnop\r\nq", 9
     result = [(tob("abcdefgh"), tob("\n")), (tob("ijklmnop"), tob("")), (tob(""), tob("\r\n")), (tob("q"), tob(""))]
     i = mp.MultipartParser(BytesIO(tob(data)), "foo", buffer_size=limit)._lineiter()
     self.assertEqual(list(i), result)
     data, limit = ("X" * 3 * 1024) + "x\n", 1024
     result = [
         (tob("X" * 1024), tob("")),
         (tob("X" * 1024), tob("")),
         (tob("X" * 1024), tob("")),
         (tob("x"), tob("\n")),
     ]
     i = mp.MultipartParser(BytesIO(tob(data)), "foo", buffer_size=limit)._lineiter()
     self.assertEqual(list(i), result)
コード例 #44
0
ファイル: test_multipart.py プロジェクト: wobsta/multipart
 def test_iterlines_maxbuf(self):
     data, limit = 'abcdefgh\nijklmnop\r\nq', 9
     result = [(tob('abcdefgh'),tob('\n')),(tob('ijklmnop'),tob('')),(tob(''),tob('\r\n')),(tob('q'),tob(''))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo', buffer_size=limit)._lineiter()
     self.assertEqual(list(i), result)
     data, limit = ('X'*3*1024)+'x\n', 1024
     result = [(tob('X'*1024),tob('')),(tob('X'*1024),tob('')),(tob('X'*1024),tob('')),(tob('x'),tob('\n'))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo', buffer_size=limit)._lineiter()
     self.assertEqual(list(i), result)
コード例 #45
0
 def test_iterlines_maxbuf(self):
     data, limit = 'abcdefgh\nijklmnop\r\nq', 9
     result = [(tob('abcdefgh'), tob('\n')), (tob('ijklmnop'), tob('')),
               (tob(''), tob('\r\n')), (tob('q'), tob(''))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo',
                            buffer_size=limit)._lineiter()
     self.assertEqual(list(i), result)
     data, limit = ('X' * 3 * 1024) + 'x\n', 1024
     result = [(tob('X' * 1024), tob('')), (tob('X' * 1024), tob('')),
               (tob('X' * 1024), tob('')), (tob('x'), tob('\n'))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo',
                            buffer_size=limit)._lineiter()
     self.assertEqual(list(i), result)
コード例 #46
0
 def test_iterlines_limit(self):
     data, limit = 'abc\ndef\r\nghi', 10
     result = [(tob('abc'), tob('\n')), (tob('def'), tob('\r\n')),
               (tob('g'), tob(''))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo', limit)._lineiter()
     self.assertEqual(list(i), result)
     data, limit = 'abc\ndef\r\nghi', 8
     result = [(tob('abc'), tob('\n')), (tob('def'), tob('\r'))]
     i = mp.MultipartParser(BytesIO(tob(data)), 'foo', limit)._lineiter()
     self.assertEqual(list(i), result)
コード例 #47
0
ファイル: test_multipart.py プロジェクト: niwinz/multipart
 def test_iterlines(self):
     data = "abc\ndef\r\nghi"
     result = [(tob("abc"), tob("\n")), (tob("def"), tob("\r\n")), (tob("ghi"), tob(""))]
     i = mp.MultipartParser(BytesIO(tob(data)), "foo")._lineiter()
     self.assertEqual(list(i), result)
コード例 #48
0
        tob('''
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0xODY0NTQ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