示例#1
0
    def test_property_create(self):

        multipart_data = {
            'property_name': '선릉 근처 근사한 숙소',
            'description': '머물기 정말 편한 숙소입니다.',
            'address1': '서울시 성북구',
            'address2': '',
            'postal': '12345',
            'max_people': '4',
            'price': '50000',
            'picture1': open('./property/tests/1234.png', 'rb'),
        }
        data = MultipartEncoder(multipart_data)

        response = self.client.post('/property/create',
                                    data=data.to_string(),
                                    **{
                                        'HTTP_AUTHORIZATION': self.encoded,
                                        'content_type': data.content_type
                                    })
        new_property = Property.objects.get(user__id=self.test_user.id)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(Property.objects.count(), 1)

        self.test_user.is_host = True
        response = self.client.post('/property/create',
                                    data=data.to_string(),
                                    **{
                                        'HTTP_AUTHORIZATION': self.encoded,
                                        'content_type': data.content_type
                                    })
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(), {'message': '숙소가 이미 존재합니다.'})
示例#2
0
def login(username, password):

    url = 'https://www.zhihu.com/api/v3/oauth/sign_in'

    headers = getheaders()

    data = getdata(username, password)

    checkcapthca(headers)

    # multipart_encoder = MultipartEncoder(fieles=data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')

    # todo:boundary后面的几位数可以随机,现在是固定的

    encoder = MultipartEncoder(
        data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')

    headers['Content-Type'] = encoder.content_type

    z2 = s.post(
        url,
        headers=headers,
        data=encoder.to_string(),
    )

    print(z2.json())

    print('123')
示例#3
0
def apigateway_event():

    """
    mp_encoder = MultipartEncoder(
        fields={'field0': open("tests/binary1.dat", "rb"),
            'field1': open("tests/binary2.dat", "rb")}
    )
    """

    mp_encoder = MultipartEncoder(
        fields={'image': ('filename',open(path_bmw, "rb"),'image/png'),
                'url' : url_clio3
                })
    body = mp_encoder.to_string()
    print('form-data is :')
    print(body[:100])
    body = base64.b64encode(body)
    print('It is encoded in base64')

    event = dict(httpMethod = 'POST',
                 path = '/predict',
                 headers = {'content-type': mp_encoder.content_type},
                 body = body)


    yield event
示例#4
0
    def upload_document(self, browser, raw_token, payload, document, new_file):
        with self.as_officeconnector(browser):
            encoder = MultipartEncoder({
                'form.widgets.file.action': 'replace',
                'form.buttons.upload': 'oc-file-upload',
                'form.widgets.file': (
                    basename(new_file.name),
                    new_file,
                    payload.get('content-type'),
                    ),
                '_authenticator': payload.get('csrf-token'),
                })

            headers = {
                'Authorization': ' '.join(('Bearer', raw_token, )),
                'Content-Type': encoder.content_type,
            }
            browser.open(
                document,
                view=payload.get('upload-form'),
                method='POST',
                headers=headers,
                data=encoder.to_string(),
                )

            self.assertEquals(204, browser.status_code)
示例#5
0
 def login(self, response):
     url = "https://www.zhihu.com/api/v3/oauth/sign_in"
     grant_type = "password"
     client_id = "c3cef7c66a1843f8b3a9e6a1e3160e20"
     source = "com.zhihu.web"
     timestamp = str((int(round(time.time() * 1000))))
     h = hmac.new(b"d1b964811afb40118a12068ff74a12f4", digestmod=sha1)
     h.update(grant_type.encode("utf-8"))
     h.update(client_id.encode("utf-8"))
     h.update(source.encode("utf-8"))
     h.update(timestamp.encode("utf-8"))
     signature = h.hexdigest()
     settings = get_project_settings()
     data = {
         "username": settings["ZHIHU_USERNAME"],
         "password": settings["ZHIHU_PASSWORD"],
         "grant_type": grant_type,
         "client_id": client_id,
         "source": source,
         "timestamp": timestamp,
         "signature": signature
     }
     multipartEncoder = MultipartEncoder(fields=data)
     boundary = multipartEncoder.boundary[2:]
     body = multipartEncoder.to_string()
     self.headers[
         "Content-Type"] = "multipart/form-data; boundary=" + boundary
     yield scrapy.Request(url=url,
                          method="post",
                          body=body,
                          headers=self.headers,
                          callback=self.login_callback,
                          errback=self.login_errback,
                          dont_filter=True)
示例#6
0
    def test_property_delete(self):

        multipart_data = {
            'property_name': '선릉 근처 근사한 숙소',
            'description': '머물기 정말 편한 숙소입니다.',
            'address1': '서울시 성북구',
            'address2': '',
            'postal': '12345',
            'max_people': '4',
            'price': '50000',
            'picture1': open('./property/tests/1234.png', 'rb'),
        }
        data = MultipartEncoder(multipart_data)

        response = self.client.post('/property/create',
                                    data=data.to_string(),
                                    **{
                                        'HTTP_AUTHORIZATION': self.encoded,
                                        'content_type': data.content_type
                                    })
        new_property = Property.objects.get(user__id=self.test_user.id)

        test_body = {'property_id': new_property.id}
        response = self.client.post(
            '/property/delete', json.dumps(test_body), **{
                'HTTP_AUTHORIZATION': self.encoded,
                'content_type': 'application/json'
            })

        self.assertEqual(response.status_code, 200)
        self.assertEqual(Property.objects.count(), 0)
示例#7
0
def checkcapthca(headers, cn=False):
    # 检查是否需要验证码,无论需不需要,必须要发一个请求
    if cn:
        url = 'https://www.zhihu.com/api/v3/oauth/captcha?lang=cn'
    else:
        url = 'https://www.zhihu.com/api/v3/oauth/captcha?lang=en'
    show_captcha = s.get(url, headers=headers)
    if show_captcha.json()['show_captcha']:
        req = s.put(url, headers=headers)
        print(req)
        with open('captcha.jpg', 'wb') as f:
            img_data = base64.b64decode(req.json()['img_base64'])
            # img_src = "data:image/jpg;base64," + req.json()['img_base64']
            f.write(img_data)
            f.close()
            try:
                im = Image.open('captcha.jpg')
                im.show()
                im.close()
            except:
                print(u'请到 %s 目录找到captcha.jpg 手动输入' % os.path.abspath('captcha.jpg'))
            captcha = input("请输入验证码:\n>")
    else:
        captcha = ''
    data = {
        'input_text': captcha
    }
    encoder = MultipartEncoder(data, boundary="----WebKitFormBoundarycGPN1xiTi2hCSKKZ")
    headers['Content-Type'] = encoder.content_type
    res = s.post(url, data=encoder.to_string(), headers=headers)
    return captcha
示例#8
0
    def get_captcha(self, tm):
        """
        验证码的处理,最多需要调用三次接口,至少要调用一次来确认是否需要验证码
        :param tm: str: 10位验证码
        :return: str/-1 如果不需要验证码,返回空字符串,如果需要则返回识别成功的验证码或者-1(识别识别的时候)
        """
        # 第一次调用确认是否需要验证码
        resp = self.session.get(captcha_url)
        show_captcha = json.loads(resp.text)['show_captcha']

        if show_captcha:
            # 如果需要验证码的话,则put一次获取验证码的图片信息
            pus_res = self.session.put(captcha_url)
            pus_res_dict = json.loads(pus_res.text)
            # base64的格式,其中要把%E2%86%B5替换成%0A
            img_base64 = pus_res_dict['img_base64'].replace('%E2%86%B5', '%0A')
            # 打码网站的调用实例
            api = Api(self.pd_id, self.pd_key)
            pred_resp = api.predict(self.pred_type,
                                    base64.b64decode(img_base64), tm)
            if pred_resp.ret_code == 0:
                capt = pred_resp.rsp_data
                headers = self.session.headers.copy()
                headers.update({
                    'x-xsrftoken':
                    self.get_xsrf(),
                    'x-ab-param':
                    'se_ffzx_jushen1=0;li_edu_page=old;tp_topic_style=0;top_test_4_liguangyi=1'
                    ';zr_expslotpaid=1;tp_zrec=0;qap_question_visitor= '
                    '0;qap_question_author=0;tp_dingyue_video=0;pf_noti_entry_num=0'
                    ';li_paid_answer_exp=0;li_vip_verti_search=0;tp_contents=2;pf_adjust=0'
                    ';li_panswer_topic=0;zr_slotpaidexp=1;li_sp_mqbk=0;li_video_section=0',
                    'x-ab-pb':
                    'ClhSC2AL4QuWCxsARwD0C+AABwzmANcLaABCAA8MTAvkCsIAzwt1DLcACABkDD8A4At5ALQKmwsP'
                    'C8UAbACkAK0ANwxWDAELiQzsCmcAtAA0DCYMtQvcC4sAEiwBAAEAAAAAAAAAAAIFAAAAAQsAAQI'
                    'AAAAAAAIAAAAAAAEBAAABAAAAAAMAAA==',
                    'x-requested-with':
                    'fetch',
                })
                # 识别成功后要再post一次给服务器,
                # 这里post的格式是multipart-formdata,boundary都是----WebKitFormBoundary开头的,
                # 这里直接指定了一个,测试是ok的
                multipart_formdata = MultipartEncoder(
                    fields={'input_text': capt},
                    boundary='----WebKitFormBoundary9rNe5nyAAj9qbqcF')
                headers['content-type'] = multipart_formdata.content_type
                # 把验证码单独post一次给服务器,再在登录的data里再提交一次,两次提交一致就通过啦
                resp = self.session.post(captcha_url,
                                         data=multipart_formdata.to_string(),
                                         headers=headers)
                # 提交验证成功会返回success的response
                if json.loads(resp.text)['success']:
                    return capt
                else:
                    api.justice(pred_resp.request_id)
                    return -1
            else:
                return -1
        else:
            return ''
async def test_validate_many_json(async_client):
    # Arrange
    fields = []
    for name in JSON_RESPONSES.keys():
        filename = TEST_FILE_DIR / name
        file = ('files', (filename.name, open(filename, 'rb'), 'text/plain'))
        fields.append(file)
    fields.append(('checkers', 'ags'))
    fields.append(('std_dictionary', 'v4_1'))
    fields.append(('fmt', 'json'))
    mp_encoder = MultipartEncoder(fields=fields)

    # Act
    async with async_client as ac:
        response = await ac.post(
            '/validate/',
            headers={'Content-Type': mp_encoder.content_type},
            data=mp_encoder.to_string())

    # Assert
    assert response.status_code == 200
    assert response.headers['content-type'] == 'application/json'
    body = response.json()
    assert set(body.keys()) == {'msg', 'type', 'self', 'data'}
    assert body['msg'] is not None
    assert body['type'] == 'success'
    assert body['self'] is not None
    assert len(body['data']) == len(JSON_RESPONSES)
async def test_validate_custom_dictionary(async_client, dictionary, expected):
    # Arrange
    filename = TEST_FILE_DIR / 'example_ags.ags'
    file = ('files', (filename.name, open(filename, 'rb'), 'text/plain'))
    fields = []
    fields.append(file)
    fields.append(('checkers', 'ags'))
    fields.append(('std_dictionary', dictionary))
    mp_encoder = MultipartEncoder(fields=fields)

    # Act
    async with async_client as ac:
        response = await ac.post(
            '/validate/',
            headers={'Content-Type': mp_encoder.content_type},
            data=mp_encoder.to_string())

    # Assert
    assert response.status_code == 200
    assert response.headers['content-type'] == 'application/json'
    body = response.json()
    assert len(body['data']) == 1
    # Assert
    assert body['data'][0]['filename'] == 'example_ags.ags'
    assert body['data'][0]['dictionary'] == expected
示例#11
0
    def upload_document(self, browser, tokens, payload, document, new_file):
        with self.as_officeconnector(browser):
            encoder = MultipartEncoder({
                'form.widgets.file.action':
                'replace',
                'form.buttons.upload':
                'oc-file-upload',
                'form.widgets.file': (
                    basename(new_file.name),
                    new_file,
                    document.content_type(),
                ),
                '_authenticator':
                payload.get('csrf-token'),
            })

            headers = {
                'Authorization': ' '.join((
                    'Bearer',
                    tokens.get('raw_token'),
                )),
                'Content-Type': encoder.content_type,
            }
            browser.open(
                document,
                view=payload.get('upload-form'),
                method='POST',
                headers=headers,
                data=encoder.to_string(),
            )

            self.assertEquals(204, browser.status_code)
async def test_validate_json(async_client, filename, expected):
    # Arrange
    filename = TEST_FILE_DIR / filename
    file = ('files', (filename.name, open(filename, 'rb'), 'text/plain'))
    fields = []
    fields.append(file)
    fields.append(('checkers', 'ags'))
    mp_encoder = MultipartEncoder(fields=fields)

    # Act
    async with async_client as ac:
        response = await ac.post(
            '/validate/',
            headers={'Content-Type': mp_encoder.content_type},
            data=mp_encoder.to_string())

    # Assert
    assert response.status_code == 200
    assert response.headers['content-type'] == 'application/json'
    body = response.json()
    assert set(body.keys()) == {'msg', 'type', 'self', 'data'}
    assert body['msg'] is not None
    assert body['type'] == 'success'
    assert body['self'] is not None
    assert len(body['data']) == 1
    assert set(body['data'][0]) == set(expected.keys())
    assert body['data'][0]['filename'] == expected['filename']
async def test_convert_bad_files(async_client, tmp_path):
    # Arrange
    fields = []
    for name, expected in BAD_FILE_DATA:
        filename = TEST_FILE_DIR / name
        file = ('files', (filename.name, open(filename, 'rb'), 'text/plain'))
        fields.append(file)
    mp_encoder = MultipartEncoder(fields=fields)

    # Act
    async with async_client as ac:
        response = await ac.post(
            '/convert/',
            headers={'Content-Type': mp_encoder.content_type},
            data=mp_encoder.to_string())

    # Assert
    assert response.status_code == 200
    assert response.headers['content-type'] == 'application/x-zip-compressed'
    assert response.headers[
        'content-disposition'] == 'attachment; filename=results.zip'

    zip_file = tmp_path / 'results.zip'
    unzipped_files = tmp_path / 'results'
    with open(zip_file, 'wb') as f:
        f.write(response.content)
    shutil.unpack_archive(zip_file, unzipped_files, 'zip')
    assert (unzipped_files / 'conversion.log').is_file()
    with open(unzipped_files / 'conversion.log', 'rt') as f:
        log = f.read()
    for name, expected in BAD_FILE_DATA:
        expected_message, expected_file_size = expected
        assert not (unzipped_files / name).is_file()
        assert expected_message in log
示例#14
0
def imageInfoEncoder():
    code = MultipartEncoder(fields={
        'file': ("1.png", open("C:/Users/xiaojianwei/Desktop/zx1.jpg",
                               'rb'), 'image/jpeg')
    },
                            boundary="----WebKitFormBoundary" +
                            str("123789xyz678"))
    return [code.to_string(), code.content_type]
示例#15
0
def login(username, password):
    url = 'https://www.zhihu.com/api/v3/oauth/sign_in'
    headers = getheaders()
    checkcapthca(s, headers)
    data = getdata(username, password)
    encoder = MultipartEncoder(data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')
    headers['Content-Type'] = encoder.content_type
    z2 = s.post(url, headers=headers, data=encoder.to_string())
    print(z2.json())
示例#16
0
 def login(self, response):
     url = 'https://www.zhihu.com/api/v3/oauth/sign_in'
     headers = self.get_headers()
     data = self.get_data('<账号>', '<密码>')
     self.check_captcha(headers)
     # 目前字段分隔符'----WebKitFormBoundarycGPN1xiTi2hCSKKZ'是固定的
     encoder = MultipartEncoder(data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')
     headers['Content-Type'] = encoder.content_type
     z2 = self.s.post(url, headers=headers, data=encoder.to_string())
     self.check_login(z2)
    def do_callback_request(self, browser, fields):
        meeting = self.meeting
        encoder = MultipartEncoder(fields=fields)
        data = encoder.to_string()
        headers = {'Content-Type': encoder.content_type}

        with self.logout():
            browser.open(
                meeting, view='receive_meeting_zip_pdf',
                method='POST', data=data, headers=headers)
示例#18
0
    def checkin_document(self,
                         browser,
                         tokens,
                         payload,
                         document,
                         comment=None):  # noqa
        with self.as_officeconnector(browser):
            headers = {
                'Authorization': ' '.join((
                    'Bearer',
                    tokens.get('raw_token'),
                )),
            }

            if comment:
                encoder = MultipartEncoder({
                    'form.widgets.comment':
                    comment,
                    'form.buttons.button_checkin':
                    'Checkin',
                    '_authenticator':
                    payload.get('csrf-token'),
                })

                headers['Content-Type'] = encoder.content_type

                browser.open(
                    document,
                    view=payload.get('checkin-with-comment'),
                    headers=headers,
                    method='POST',
                    data=encoder.to_string(),
                )
            else:
                browser.open(
                    document,
                    headers=headers,
                    view=payload.get('checkin-without-comment'),
                    send_authenticator=True,
                )

        self.assert_journal_entry(
            document,
            DOCUMENT_CHECKED_IN,
            u'Document checked in',
        )

        journal_comments = get_journal_entry(document).get('comments')
        if comment:
            self.assertTrue(journal_comments)
        else:
            self.assertFalse(journal_comments)

        self.assertEquals(200, browser.status_code)
示例#19
0
def login(username, password):
    url = 'https://www.zhihu.com/api/v3/oauth/sign_in'
    headers = getheaders()
    data = getdata(username, password)
    checkcapthca(headers)
    # multipart_encoder = MultipartEncoder(fieles=data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')
    # todo:boundary后面的几位数可以随机,现在是固定的
    encoder = MultipartEncoder(data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')
    headers['Content-Type'] = encoder.content_type
    z2 = s.post(url, headers=headers, data=encoder.to_string(), )
    print(z2.json())
    print('123')
示例#20
0
    def do_callback_request(self, browser, fields):
        meeting = self.meeting
        encoder = MultipartEncoder(fields=fields)
        data = encoder.to_string()
        headers = {'Content-Type': encoder.content_type}

        with self.logout():
            browser.open(meeting,
                         view='receive_meeting_zip_pdf',
                         method='POST',
                         data=data,
                         headers=headers)
示例#21
0
    def test_upload(self):
        self.login()
        manager = PluginRegistry.getInstance("UploadManager")
        fpath = os.path.join(os.path.dirname(__file__), 'create_user.zip')

        with open(fpath, "rb") as f:
            m = MultipartEncoder(
                fields={'file': ('create_user.zip', f, 'text/plain')}
            )
            data = m.to_string()

            # try to use unregistered path
            uuid, path = manager.registerUploadPath("admin", self.session_id, "workflow")
            response = self.fetch("/uploads/unknown_path", method="POST", body=data, headers={
                'Content-Type': m.content_type
            })
            assert response.code == 404
            assert manager.unregisterUploadPath(uuid) is True

            # try to use path from another user
            uuid, path = manager.registerUploadPath("other_user", self.session_id, "workflow")
            response = self.fetch(path, method="POST", body=data, headers={
                'Content-Type': m.content_type
            })
            assert response.code == 403
            assert manager.unregisterUploadPath(uuid) is True

            # try to use path from another session
            uuid, path = manager.registerUploadPath("admin", "other session id", "workflow")
            response = self.fetch(path, method="POST", body=data, headers={
                'Content-Type': m.content_type
            })
            assert response.code == 403
            assert manager.unregisterUploadPath(uuid) is True

            # try to use path for unhandled type
            uuid, path = manager.registerUploadPath("admin", self.session_id, "unknown-type")
            response = self.fetch(path, method="POST", body=data, headers={
                'Content-Type': m.content_type
            })
            assert response.code == 501
            assert manager.unregisterUploadPath(uuid) is True

            # finally a working example
            uuid, path = manager.registerUploadPath("admin", self.session_id, "workflow")
            response = self.fetch(path, method="POST", body=data, headers={
                'Content-Type': m.content_type,
                'X-File-Name': 'create_user.zip'
            })
            assert response.code == 200
            # path should have been removed by successfully unsigning it
            assert manager.unregisterUploadPath(uuid) is False
示例#22
0
    def prepare_request(self, userid='', destination='inbox', org_unit=''):
        fields = {
            'userid': userid or self.regular_user.getId(),
            'destination': destination,
            'file': ('mydocument.txt', 'my text', 'text/plain'),
        }
        if org_unit:
            fields['org_unit'] = org_unit

        encoder = MultipartEncoder(fields=fields)
        return encoder.to_string(), {
            'Content-Type': encoder.content_type,
            'Accept': 'application/json',
        }
示例#23
0
    def login(self,username, password):
        url = 'https://www.zhihu.com/api/v3/oauth/sign_in'

        headers = self.getheaders(session)
        data = self.getdata(username, password)
        self.checkcapthca(headers)
        # multipart_encoder = MultipartEncoder(fieles=data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')
        # todo:boundary后面的几位数可以随机,现在是固定的
        encoder = MultipartEncoder(data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')
        headers['Content-Type'] = encoder.content_type
        #z2 = s.post(url, headers=headers, data=encoder.to_string(), )
        z2 = self.session.post(url, headers=headers, data=encoder.to_string(), )
        with open ('cookies.json','w') as f:
            json.dump(requests.utils.dict_from_cookiejar(self.session.cookies),f)
示例#24
0
    def prepare_request(self, userid='', destination='inbox', org_unit=''):
        fields = {
            'userid': userid or self.regular_user.getId(),
            'destination': destination,
            'file': ('mydocument.txt', 'my text', 'text/plain'),
        }
        if org_unit:
            fields['org_unit'] = org_unit

        encoder = MultipartEncoder(fields=fields)
        return encoder.to_string(), {
            'Content-Type': encoder.content_type,
            'Accept': 'application/json',
        }
示例#25
0
def format_multipart(*args, **kwargs):
    output = {}
    for key, value in kwargs.items():
        if key == 'boundary':
            continue
        if isinstance(value, dict):
            output[key] = (value['name'],
                           open(
                               os.path.dirname(os.path.abspath(__file__)) +
                               value['path'], 'rb'), value['type'])
        else:
            output[key] = "{}".format(value)
    encoded = MultipartEncoder(fields=output, boundary=kwargs['boundary'])
    return encoded.to_string()
示例#26
0
 def file(cls,
          field_name,
          file_name,
          *,
          content_type='text/plain',
          headers=None):
     '''
     Upload a file and send as multipart data. Content-Type is sent as the content type got from multipart encoding.
     '''
     from arjuna import C, ArjunaOption
     file_path = os.path.join(C(ArjunaOption.DATA_FILE_DIR), file_name)
     encoder = MultipartEncoder({
         field_name: (file_name, open(file_path,
                                      'rb'), content_type, headers)
     })
     return _HttpContent(content=encoder.to_string(),
                         type=encoder.content_type)
示例#27
0
    def checkin_document(self, browser, tokens, payload, document, comment=None):  # noqa
        with self.as_officeconnector(browser):
            headers = {
                'Authorization': ' '.join((
                    'Bearer',
                    tokens.get('raw_token'),
                    )),
            }

            if comment:
                encoder = MultipartEncoder({
                    'form.widgets.comment': comment,
                    'form.buttons.button_checkin': 'Checkin',
                    '_authenticator': payload.get('csrf-token'),
                    })

                headers['Content-Type'] = encoder.content_type

                browser.open(
                    document,
                    view=payload.get('checkin-with-comment'),
                    headers=headers,
                    method='POST',
                    data=encoder.to_string(),
                    )
            else:
                browser.open(
                    document,
                    headers=headers,
                    view=payload.get('checkin-without-comment'),
                    send_authenticator=True,
                    )

        self.assert_journal_entry(
            document,
            DOCUMENT_CHECKED_IN,
            u'Document checked in',
            )

        journal_comments = get_journal_entry(document).get('comments')
        if comment:
            self.assertTrue(journal_comments)
        else:
            self.assertFalse(journal_comments)

        self.assertEquals(200, browser.status_code)
async def test_validate_bgs_text(async_client):
    # Arrange
    filename = TEST_FILE_DIR / 'example_ags.ags'
    file = ('files', (filename.name, open(filename, 'rb'), 'text/plain'))
    fields = [file]
    fields.append(('checkers', 'bgs'))
    fields.append(('fmt', 'text'))
    mp_encoder = MultipartEncoder(fields=fields)

    # Act
    async with async_client as ac:
        response = await ac.post(
            '/validate/',
            headers={'Content-Type': mp_encoder.content_type},
            data=mp_encoder.to_string())

    # Assert
    assert response.status_code == 200
    assert 'text/plain' in response.headers['content-type']
示例#29
0
    def store_blocks(self, block_data_list, duration=60):
        """Store a list of blocks on the storage server

        :param block_data_list: list of blocks represented as byte strings (iterator)
        :param duration: number of seconds to request storage
        :returns dictionary: decoded result of the request
        """
        request_identifier = self.request_identifier
        request_headers = {
            'x-request-identifier': 'request{0}'.format(request_identifier)
        }

        url = "{0}/block/store".format(self.ServiceURL)

        try:
            request_data = dict()
            request_data['operation'] = (None,
                                         json.dumps({'duration': duration}),
                                         'application/json')
            count = 0  # just needed to uniquify the keys
            for block_data in block_data_list:
                request_data['block{0}'.format(count)] = (
                    'block{0}'.format(count), block_data,
                    'application/octet-stream')
                count += 1

            mp_encoder = MultipartEncoder(request_data)
            request_headers['content-type'] = mp_encoder.content_type
        except Exception as e:
            logger.warn('unknown exception (store_blocks); %s', str(e))
            raise StorageException(str(e)) from e

        try:
            response = self.session.post(url,
                                         data=mp_encoder.to_string(),
                                         headers=request_headers,
                                         timeout=self.default_timeout)
        except Exception as e:
            logger.warn('unknown exception (store_blocks); %s', str(e))
            raise StorageException(str(e)) from e

        response.raise_for_status()
        return response.json()
示例#30
0
    def __call__(self, environ, start_response):
        try:
            block_ids = UnpackJSONRequest(environ)
        except Exception as e:
            logger.exception('get blocks')
            return ErrorResponse(
                start_response,
                'unknown exception while unpacking get blocks request')
        try:
            response = dict()
            for block_id in block_ids:
                block_data = self.block_store.get_block(block_id,
                                                        encoding='b64')
                if block_data is None:
                    msg = "unknown block; {0}".format(block_id)
                    return ErrorResponse(start_response,
                                         msg,
                                         status=HttpStatus.NOT_FOUND)
                response[block_id] = (None, block_data,
                                      'application/octet-stream')

        except Exception as e:
            logger.exception('get blocks')
            return ErrorResponse(
                start_response,
                "unknown exception while processing get blocks request")

        try:
            encoder = MultipartEncoder(response)
        except Exception as e:
            logger.exception('get blocks')
            return ErrorResponse(
                start_response,
                'unknown exception while packget get blocks response')

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('content-type', 'application/octet-stream'),
                   ('x-content-type', encoder.content_type),
                   ('content-transfer-encoding', 'utf-8'),
                   ('content-length', str(encoder.len))]
        start_response(status, headers)
        return [encoder.to_string()]
示例#31
0
    def SendSMS(self, **kwargs):
        """
        Envia SMS
        mobileNumbers="+15558675309",
        from_="+15017250604",
        message="Hello from Python!"
        """
        mobileNumbers = kwargs['mobileNumbers']
        from_ = kwargs['from_']
        message = kwargs['message']
        auth = (self.user, self.password)

        # Verifica si no existe el account_id
        if not self.account_id:
            # Solicita la cuenta del usuario
            rsp = self.__request_get(account_url, auth, None)
            if rsp['HasError']:
                return rsp

            self.account_id = str(rsp['Content'][0]['id'])

        # Solicita token del usuario
        rsp = self.__request_get(url_token, auth, None)
        if rsp['HasError']:
            return rsp

        self.token = rsp['Content']['token']

        # Datos form
        multipart_data = MultipartEncoder(
            fields={
                "acc_id": self.account_id,
                "from": from_,
                "message": message,
                "to": mobileNumbers
            })
        data = multipart_data.to_string()

        # Envia SMS
        rsp = self.__request_post(url_send_sms, multipart_data.content_type,
                                  None, data, self.token)
        return rsp
示例#32
0
def login(account, password):
    res = s.get("https://www.zhihu.com", headers=header)
    match_obj = re.match('.*id="data".*data-state="(.*?)".*', res.text, re.DOTALL)
    json_data = {}
    if match_obj:
        data_state = match_obj.group(1).replace("&quot;", '"')
        json_data = json.loads(data_state)
    headers = header
    headers['X-UDID'] = json_data['token']['xUDID']
    headers['X-Xsrftoken'] = json_data['token']['xsrf']
    headers['Content-Type'] = "----WebKitFormBoundarycGPN1xiTi2hCSKKZ"
    headers['authorization'] = "oauth c3cef7c66a1843f8b3a9e6a1e3160e20"
    post_url = "https://www.zhihu.com/api/v3/oauth/sign_in"
    data = getdata(account, password)
    data['captcha'] = checkcapthca(headers)
    encoder = MultipartEncoder(data, boundary="----WebKitFormBoundarycGPN1xiTi2hCSKKZ")
    headers['Content-Type'] = encoder.content_type
    res = s.post(post_url, data=encoder.to_string(), headers=headers)
    s.cookies.save()
    print(res.text)
示例#33
0
 def multipart(cls, *fields):
     '''
     Send the provided HTTP fields as multipart data. Content-Type is sent as the content type got from multipart encoding.
     '''
     from arjuna import C, ArjunaOption
     elist = list()
     for field in fields:
         if type(field) is dict:
             for k, v in field.items():
                 elist.append((k, str(v)))
         elif isinstance(field, _HttpField):
             if field.is_file:
                 file_path = os.path.join(C(ArjunaOption.DATA_FILE_DIR),
                                          field.value)
                 elist.append(
                     (field.name, (field.value, open(file_path, 'rb'),
                                   field.content_type, field.headers)))
             else:
                 elist.append((field.name, str(field.value)))
     encoder = MultipartEncoder(elist)
     return _HttpContent(content=encoder.to_string(),
                         type=encoder.content_type)
示例#34
0
def login(username, password):
    username = '******'
    password = '******'
    url = 'https://www.zhihu.com/api/v3/oauth/sign_in'
    headers = getheaders()
    data = getdata(username, password)
    checkcapthca(headers)
    # multipart_encoder = MultipartEncoder(fieles=data, boundary='----WebKitFormBoundarycGPN1xiTi2hCSKKZ')
    # todo:boundary后面的几位数可以随机,现在是固定的
    encoder = MultipartEncoder(
        data, boundary='-----------------------------41184676334')
    headers['Content-Type'] = encoder.content_type
    print(encoder)
    print(headers)
    z2 = s.post(
        url,
        headers=headers,
        data=encoder.to_string(),
    )
    print(z2.json())
    print('登录成功')
    s.cookies.save()
示例#35
0
        def addNotice(self, text="", notify=None, object=None):

            if object is None:
                raise ValueError("Method-Argument 'object' is required")

            request_url = self.get_sevdesk_client().build_url(
                model=(self.controller.get_apimodel_name(), "Factory",
                       "addNotice"))

            mp_encoded = MultipartEncoder(
                fields={
                    "object[id]": str(object.id),
                    "object[objectName]": str(object.objectName),
                    "text": str(text),
                    "date": str(int(time.time())),
                    "notify": "null"
                })
            response = self.get_sevdesk_client().post(
                request_url,
                data=mp_encoded.to_string(),
                headers={'Content-Type': mp_encoded.content_type})
            return response
class TestMultipartEncoder(unittest.TestCase):
    def setUp(self):
        self.parts = [('field', 'value'), ('other_field', 'other_value')]
        self.boundary = 'this-is-a-boundary'
        self.instance = MultipartEncoder(self.parts, boundary=self.boundary)

    def test_to_string(self):
        assert self.instance.to_string() == (
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="field"\r\n\r\n'
            'value\r\n'
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="other_field"\r\n\r\n'
            'other_value\r\n'
            '--this-is-a-boundary--\r\n'
        ).encode()

    def test_content_type(self):
        expected = 'multipart/form-data; boundary=this-is-a-boundary'
        assert self.instance.content_type == expected

    def test_encodes_data_the_same(self):
        encoded = encode_multipart_formdata(self.parts, self.boundary)[0]
        assert encoded == self.instance.read()

    def test_streams_its_data(self):
        large_file = LargeFileMock(123456789)
        parts = {'some field': 'value',
                 'some file': large_file,
                 }
        encoder = MultipartEncoder(parts)
        total_size = len(encoder)
        read_size = 1024 * 1024 * 128
        already_read = 0
        while True:
            read = encoder.read(read_size)
            already_read += len(read)
            if not read:
                break

        assert encoder._buffer.tell() <= read_size
        assert already_read == total_size

    def test_streams_its_data_with_correct_length(self):
        for i in range(0, 100):  # or more than 100 to increase fuzzing strength
            file_size = random.randint(0, 12345)
            if random.random() < 0.1:
                file_size = 0  # sometimes we check with an empty file
            self.check_read_file_with_chunks(file_size, read_size=1)
            self.check_read_file_with_chunks(file_size, read_size=2)
            self.check_read_file_with_chunks(file_size, read_size=3)
            read_size = random.randint(0, 2*file_size)
            self.check_read_file_with_chunks(file_size, read_size=1)
            for read_size in range(file_size - 10, file_size + 200):
                if read_size < -1 or read_size == 0:
                    continue
                self.check_read_file_with_chunks(file_size, read_size)

    def check_read_file_with_chunks(self, file_size, read_size):
        #print "===== Testing with file_size=",file_size,"read_size=",read_size
        boundary="deterministic-test-boundary"
        a_file = LargeFileMock(file_size)
        parts = {'some_field': 'this is the value...',
                 'some_file': a_file.read(),
        }
        expected_bytes = encode_multipart_formdata(parts, boundary)[0]
        content_length = len(expected_bytes)

        # Now read from our encoder :
        a_file = LargeFileMock(file_size)
        parts = {'some_field': 'this is the value...',
                 'some_file': a_file,
        }
        encoder = MultipartEncoder(parts, boundary=boundary)
        raw_bytes_count = 0
        while True:
            data = encoder.read(read_size)
            if not data:
                break
            #print "read",len(data),"bytes : ",repr(data)
            assert data == expected_bytes[raw_bytes_count:raw_bytes_count+len(data)]
            raw_bytes_count += len(data)
        #if raw_bytes_count != content_length:
        #    print "Test failed with file_size=",file_size,"and read_size=",read_size
        assert raw_bytes_count == content_length

    def test_length_is_correct(self):
        encoded = encode_multipart_formdata(self.parts, self.boundary)[0]
        assert len(encoded) == len(self.instance)

    def test_encodes_with_readable_data(self):
        s = io.BytesIO(b'value')
        m = MultipartEncoder([('field', s)], boundary=self.boundary)
        assert m.read() == (
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="field"\r\n\r\n'
            'value\r\n'
            '--this-is-a-boundary--\r\n'
        ).encode()

    def test_reads_open_file_objects(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder([('field', 'foo'), ('file', fd)])
            assert m.read() is not None

    def test_reads_open_file_objects_with_a_specified_filename(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder(
                [('field', 'foo'), ('file', ('filename', fd, 'text/plain'))]
                )
            assert m.read() is not None

    def test_reads_open_file_objects_using_to_string(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder([('field', 'foo'), ('file', fd)])
            assert m.to_string() is not None

    def test_handles_encoded_unicode_strings(self):
        m = MultipartEncoder([
            ('field',
             b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3')
        ])
        assert m.read() is not None

    def test_handles_uncode_strings(self):
        s = b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3'
        m = MultipartEncoder([
            ('field', s.decode('utf-8'))
        ])
        assert m.read() is not None

    def test_regresion_1(self):
        """Ensure issue #31 doesn't ever happen again."""
        fields = {
            "test": "t" * 100
        }

        for x in range(30):
            fields['f%d' % x] = (
                'test', open('tests/test_multipart_encoder.py', 'rb')
                )

        m = MultipartEncoder(fields=fields)
        total_size = len(m)

        blocksize = 8192
        read_so_far = 0

        while True:
            data = m.read(blocksize)
            if not data:
                break
            read_so_far += len(data)

        assert read_so_far == total_size

    def test_regression_2(self):
        """Ensure issue #31 doesn't ever happen again."""
        fields = {
            "test": "t" * 8100
        }

        m = MultipartEncoder(fields=fields)
        total_size = len(m)

        blocksize = 8192
        read_so_far = 0

        while True:
            data = m.read(blocksize)
            if not data:
                break
            read_so_far += len(data)

        assert read_so_far == total_size
class TestMultipartEncoder(unittest.TestCase):
    def setUp(self):
        self.parts = [('field', 'value'), ('other_field', 'other_value')]
        self.boundary = 'this-is-a-boundary'
        self.instance = MultipartEncoder(self.parts, boundary=self.boundary)

    def test_to_string(self):
        assert self.instance.to_string() == (
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="field"\r\n\r\n'
            'value\r\n'
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="other_field"\r\n\r\n'
            'other_value\r\n'
            '--this-is-a-boundary--\r\n'
        ).encode()

    def test_content_type(self):
        expected = 'multipart/form-data; boundary=this-is-a-boundary'
        assert self.instance.content_type == expected

    def test_encodes_data_the_same(self):
        encoded = encode_multipart_formdata(self.parts, self.boundary)[0]
        assert encoded == self.instance.read()

    def test_streams_its_data(self):
        large_file = LargeFileMock()
        parts = {'some field': 'value',
                 'some file': large_file,
                 }
        encoder = MultipartEncoder(parts)
        read_size = 1024 * 1024 * 128
        while True:
            read = encoder.read(read_size)
            if not read:
                break

        assert encoder._buffer.tell() <= read_size

    def test_length_is_correct(self):
        encoded = encode_multipart_formdata(self.parts, self.boundary)[0]
        assert len(encoded) == len(self.instance)

    def test_encodes_with_readable_data(self):
        s = io.BytesIO(b'value')
        m = MultipartEncoder([('field', s)], boundary=self.boundary)
        assert m.read() == (
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="field"\r\n\r\n'
            'value\r\n'
            '--this-is-a-boundary--\r\n'
        ).encode()

    def test_reads_open_file_objects(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder([('field', 'foo'), ('file', fd)])
            assert m.read() is not None

    def test_reads_open_file_objects_with_a_specified_filename(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder(
                [('field', 'foo'), ('file', ('filename', fd, 'text/plain'))]
                )
            assert m.read() is not None

    def test_reads_open_file_objects_using_to_string(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder([('field', 'foo'), ('file', fd)])
            assert m.to_string() is not None

    def test_handles_encoded_unicode_strings(self):
        m = MultipartEncoder([
            ('field',
             b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3')
        ])
        assert m.read() is not None

    def test_handles_uncode_strings(self):
        s = b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3'
        m = MultipartEncoder([
            ('field', s.decode('utf-8'))
        ])
        assert m.read() is not None
class TestMultipartEncoder(unittest.TestCase):
    def setUp(self):
        self.parts = [('field', 'value'), ('other_field', 'other_value')]
        self.boundary = 'this-is-a-boundary'
        self.instance = MultipartEncoder(self.parts, boundary=self.boundary)

    def test_to_string(self):
        assert self.instance.to_string() == (
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="field"\r\n\r\n'
            'value\r\n'
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="other_field"\r\n\r\n'
            'other_value\r\n'
            '--this-is-a-boundary--\r\n'
        ).encode()

    def test_content_type(self):
        expected = 'multipart/form-data; boundary=this-is-a-boundary'
        assert self.instance.content_type == expected

    def test_encodes_data_the_same(self):
        encoded = encode_multipart_formdata(self.parts, self.boundary)[0]
        assert encoded == self.instance.read()

    def test_streams_its_data(self):
        large_file = LargeFileMock()
        parts = {'some field': 'value',
                 'some file': large_file,
                 }
        encoder = MultipartEncoder(parts)
        total_size = len(encoder)
        read_size = 1024 * 1024 * 128
        already_read = 0
        while True:
            read = encoder.read(read_size)
            already_read += len(read)
            if not read:
                break

        assert encoder._buffer.tell() <= read_size
        assert already_read == total_size

    def test_length_is_correct(self):
        encoded = encode_multipart_formdata(self.parts, self.boundary)[0]
        assert len(encoded) == len(self.instance)

    def test_encodes_with_readable_data(self):
        s = io.BytesIO(b'value')
        m = MultipartEncoder([('field', s)], boundary=self.boundary)
        assert m.read() == (
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="field"\r\n\r\n'
            'value\r\n'
            '--this-is-a-boundary--\r\n'
        ).encode()

    def test_reads_open_file_objects(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder([('field', 'foo'), ('file', fd)])
            assert m.read() is not None

    def test_reads_open_file_objects_with_a_specified_filename(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder(
                [('field', 'foo'), ('file', ('filename', fd, 'text/plain'))]
                )
            assert m.read() is not None

    def test_reads_open_file_objects_using_to_string(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder([('field', 'foo'), ('file', fd)])
            assert m.to_string() is not None

    def test_handles_encoded_unicode_strings(self):
        m = MultipartEncoder([
            ('field',
             b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3')
        ])
        assert m.read() is not None

    def test_handles_uncode_strings(self):
        s = b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3'
        m = MultipartEncoder([
            ('field', s.decode('utf-8'))
        ])
        assert m.read() is not None

    def test_regresion_1(self):
        """Ensure issue #31 doesn't ever happen again."""
        fields = {
            "test": "t" * 100
        }

        for x in range(30):
            fields['f%d' % x] = (
                'test', open('tests/test_multipart_encoder.py', 'rb')
                )

        m = MultipartEncoder(fields=fields)
        total_size = len(m)

        blocksize = 8192
        read_so_far = 0

        while True:
            data = m.read(blocksize)
            if not data:
                break
            read_so_far += len(data)

        assert read_so_far == total_size

    def test_regression_2(self):
        """Ensure issue #31 doesn't ever happen again."""
        fields = {
            "test": "t" * 8100
        }

        m = MultipartEncoder(fields=fields)
        total_size = len(m)

        blocksize = 8192
        read_so_far = 0

        while True:
            data = m.read(blocksize)
            if not data:
                break
            read_so_far += len(data)

        assert read_so_far == total_size
class TestMultipartDecoder(unittest.TestCase):
    def setUp(self):
        self.sample_1 = (
            ('field 1', 'value 1'),
            ('field 2', 'value 2'),
            ('field 3', 'value 3'),
            ('field 4', 'value 4'),
        )
        self.boundary = 'test boundary'
        self.encoded_1 = MultipartEncoder(self.sample_1, self.boundary)
        self.decoded_1 = MultipartDecoder(
            self.encoded_1.to_string(),
            self.encoded_1.content_type
        )

    def test_non_multipart_response_fails(self):
        jpeg_response = mock.NonCallableMagicMock(spec=requests.Response)
        jpeg_response.headers = {'content-type': 'image/jpeg'}
        with pytest.raises(NonMultipartContentTypeException):
            MultipartDecoder.from_response(jpeg_response)

    def test_length_of_parts(self):
        assert len(self.sample_1) == len(self.decoded_1.parts)

    def test_content_of_parts(self):
        def parts_equal(part, sample):
            return part.content == encode_with(sample[1], 'utf-8')

        parts_iter = zip(self.decoded_1.parts, self.sample_1)
        assert all(parts_equal(part, sample) for part, sample in parts_iter)

    def test_header_of_parts(self):
        def parts_header_equal(part, sample):
            return part.headers[b'Content-Disposition'] == encode_with(
                'form-data; name="{0}"'.format(sample[0]), 'utf-8'
            )

        parts_iter = zip(self.decoded_1.parts, self.sample_1)
        assert all(
            parts_header_equal(part, sample)
            for part, sample in parts_iter
        )

    def test_from_response(self):
        response = mock.NonCallableMagicMock(spec=requests.Response)
        response.headers = {
            'content-type': 'multipart/related; boundary="samp1"'
        }
        cnt = io.BytesIO()
        cnt.write(b'\r\n--samp1\r\n')
        cnt.write(b'Header-1: Header-Value-1\r\n')
        cnt.write(b'Header-2: Header-Value-2\r\n')
        cnt.write(b'\r\n')
        cnt.write(b'Body 1, Line 1\r\n')
        cnt.write(b'Body 1, Line 2\r\n')
        cnt.write(b'--samp1\r\n')
        cnt.write(b'\r\n')
        cnt.write(b'Body 2, Line 1\r\n')
        cnt.write(b'--samp1--\r\n')
        response.content = cnt.getvalue()
        decoder_2 = MultipartDecoder.from_response(response)
        assert decoder_2.content_type == response.headers['content-type']
        assert (
            decoder_2.parts[0].content == b'Body 1, Line 1\r\nBody 1, Line 2'
        )
        assert decoder_2.parts[0].headers[b'Header-1'] == b'Header-Value-1'
        assert len(decoder_2.parts[1].headers) == 0
        assert decoder_2.parts[1].content == b'Body 2, Line 1'
 def test_reads_open_file_objects_using_to_string(self):
     with open('setup.py', 'rb') as fd:
         m = MultipartEncoder([('field', 'foo'), ('file', fd)])
         assert m.to_string() is not None
class TestMultipartEncoder(unittest.TestCase):
    def setUp(self):
        self.parts = [('field', 'value'), ('other_field', 'other_value')]
        self.boundary = 'this-is-a-boundary'
        self.instance = MultipartEncoder(self.parts, boundary=self.boundary)

    def test_to_string(self):
        assert self.instance.to_string() == (
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="field"\r\n\r\n'
            'value\r\n'
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="other_field"\r\n\r\n'
            'other_value\r\n'
            '--this-is-a-boundary--\r\n'
        ).encode()

    def test_content_type(self):
        expected = 'multipart/form-data; boundary=this-is-a-boundary'
        assert self.instance.content_type == expected

    def test_encodes_data_the_same(self):
        encoded = filepost.encode_multipart_formdata(self.parts,
                                                     self.boundary)[0]
        assert encoded == self.instance.read()

    def test_streams_its_data(self):
        large_file = LargeFileMock()
        parts = {'some field': 'value',
                 'some file': large_file,
                 }
        encoder = MultipartEncoder(parts)
        total_size = encoder.len
        read_size = 1024 * 1024 * 128
        already_read = 0
        while True:
            read = encoder.read(read_size)
            already_read += len(read)
            if not read:
                break

        assert encoder._buffer.tell() <= read_size
        assert already_read == total_size

    def test_length_is_correct(self):
        encoded = filepost.encode_multipart_formdata(self.parts,
                                                     self.boundary)[0]
        assert len(encoded) == self.instance.len

    def test_encodes_with_readable_data(self):
        s = io.BytesIO(b'value')
        m = MultipartEncoder([('field', s)], boundary=self.boundary)
        assert m.read() == (
            '--this-is-a-boundary\r\n'
            'Content-Disposition: form-data; name="field"\r\n\r\n'
            'value\r\n'
            '--this-is-a-boundary--\r\n'
        ).encode()

    def test_reads_open_file_objects(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder([('field', 'foo'), ('file', fd)])
            assert m.read() is not None

    def test_reads_open_file_objects_with_a_specified_filename(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder(
                [('field', 'foo'), ('file', ('filename', fd, 'text/plain'))]
                )
            assert m.read() is not None

    def test_reads_open_file_objects_using_to_string(self):
        with open('setup.py', 'rb') as fd:
            m = MultipartEncoder([('field', 'foo'), ('file', fd)])
            assert m.to_string() is not None

    def test_handles_encoded_unicode_strings(self):
        m = MultipartEncoder([
            ('field',
             b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3')
        ])
        assert m.read() is not None

    def test_handles_uncode_strings(self):
        s = b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3'
        m = MultipartEncoder([
            ('field', s.decode('utf-8'))
        ])
        assert m.read() is not None

    def test_regresion_1(self):
        """Ensure issue #31 doesn't ever happen again."""
        fields = {
            "test": "t" * 100
        }

        for x in range(30):
            fields['f%d' % x] = (
                'test', open('tests/test_multipart_encoder.py', 'rb')
                )

        m = MultipartEncoder(fields=fields)
        total_size = m.len

        blocksize = 8192
        read_so_far = 0

        while True:
            data = m.read(blocksize)
            if not data:
                break
            read_so_far += len(data)

        assert read_so_far == total_size

    def test_regression_2(self):
        """Ensure issue #31 doesn't ever happen again."""
        fields = {
            "test": "t" * 8100
        }

        m = MultipartEncoder(fields=fields)
        total_size = m.len

        blocksize = 8192
        read_so_far = 0

        while True:
            data = m.read(blocksize)
            if not data:
                break
            read_so_far += len(data)

        assert read_so_far == total_size

    def test_handles_empty_unicode_values(self):
        """Verify that the Encoder can handle empty unicode strings.

        See https://github.com/requests/toolbelt/issues/46 for
        more context.
        """
        fields = [(b'test'.decode('utf-8'), b''.decode('utf-8'))]
        m = MultipartEncoder(fields=fields)
        assert len(m.read()) > 0

    def test_accepts_custom_content_type(self):
        """Verify that the Encoder handles custom content-types.

        See https://github.com/requests/toolbelt/issues/52
        """
        fields = [
            (b'test'.decode('utf-8'), (b'filename'.decode('utf-8'),
                                       b'filecontent',
                                       b'application/json'.decode('utf-8')))
        ]
        m = MultipartEncoder(fields=fields)
        output = m.read().decode('utf-8')
        assert output.index('Content-Type: application/json\r\n') > 0

    def test_accepts_custom_headers(self):
        """Verify that the Encoder handles custom headers.

        See https://github.com/requests/toolbelt/issues/52
        """
        fields = [
            (b'test'.decode('utf-8'), (b'filename'.decode('utf-8'),
                                       b'filecontent',
                                       b'application/json'.decode('utf-8'),
                                       {'X-My-Header': 'my-value'}))
        ]
        m = MultipartEncoder(fields=fields)
        output = m.read().decode('utf-8')
        assert output.index('X-My-Header: my-value\r\n') > 0