Beispiel #1
0
def test_encode_multipart_without_files():
    mocker = Mox()
    file_path = '/path/to/file.pdf'

    old_guess_type = multipart.guess_type
    multipart.guess_type = mocker.CreateMockAnything()
    multipart.guess_type(file_path).AndReturn(('some-mimetype', None))

    file_mock = mocker.CreateMockAnything()
    file_mock.name = file_path
    file_mock.read().AndReturn('MY_FILE_CONTENT')

    mocker.ReplayAll()
    try:
        my_data = {
            'foo': u'bar',
            'age': 21,
            'my_file': file_mock,
        }
        got = multipart.encode_multipart('fakeboundary', my_data)
        assert_equals(got, '--fakeboundary\r\nContent-Disposition: form-data; ' \
                           'name="age"\r\n\r\n21\r\n--fakeboundary\r\nContent-' \
                           'Disposition: form-data; name="foo"\r\n\r\nbar\r\n-' \
                           '-fakeboundary\r\nContent-Disposition: form-data; ' \
                           'name="my_file"; filename="file.pdf"\r\nContent-Type: ' \
                           'some-mimetype\r\n\r\nMY_FILE_CONTENT\r\n--' \
                           'fakeboundary--\r\n')
        mocker.VerifyAll()
    finally:
        multipart.guess_type = old_guess_type
Beispiel #2
0
def test_encode_multipart_single_data():
    my_data = {
        'name': u'Gabriel Falcão',
    }
    got = multipart.encode_multipart('some-boundary', my_data)
    assert_equals(got, '--some-boundary\r\nContent-Disposition: ' \
                       'form-data; name="name"\r\n\r\nGabriel Falc\xc3\xa3o\r\n' \
                       '--some-boundary--\r\n')
Beispiel #3
0
def test_encode_multipart_without_files():
    my_data = {
        'foo': u'bar',
        'age': 21
    }
    got = multipart.encode_multipart('wee', my_data)
    assert_equals(got, '--wee\r\nContent-Disposition: form-data; ' \
                       'name="age"\r\n\r\n21\r\n--wee\r\nContent-Disposition: ' \
                       'form-data; name="foo"\r\n\r\nbar\r\n--wee--\r\n')
Beispiel #4
0
    def request(self, url, method, body=None, headers=None):
        if not isinstance(url, basestring):
            raise TypeError, 'Bolacha.request, parameter url must be ' \
                  'a string. Got %s' % repr(url)

        if not isinstance(method, basestring):
            raise TypeError, 'Bolacha.request, parameter method must be ' \
                  'a string. Got %s' % repr(method)

        if method not in HTTP_METHODS:
            raise TypeError, 'Bolacha.request, parameter method must be ' \
                  'a valid HTTP method. Got %s. %s' % (method,
                                                        RFC_LOCATION)

        if body is None:
            body = ''

        if not isinstance(body, (basestring, dict)):
            raise TypeError, 'Bolacha.request, parameter body must be ' \
                  'a string or dict. Got %s.' % (repr(body))

        is_urlencoded = False

        body_has_file = isinstance(body, dict) and any([is_file(fobj)
                                                        for fobj in body.values()])

        if isinstance(body, dict):
            if body_has_file:
                rbody = encode_multipart(BOUNDARY, body)
            else:
                rbody = urlencode(body)
                is_urlencoded = True
        else:
            rbody = body

        if headers is None:
            headers = {}

        if not isinstance(headers, dict):
            raise TypeError, 'Bolacha.request, parameter headers must be ' \
                  'a dict or NoneType. Got %s' % repr(headers)

        rheaders = self.headers.copy()
        rheaders.update(headers)

        if self.persistent:
            if 'set-cookie' in self.headers:
                rheaders['Cookie'] = self.headers['set-cookie']

        if 'set-cookie' in rheaders:
            del rheaders['set-cookie']

        if is_urlencoded and not 'Content-type' in rheaders:
            rheaders['Content-type'] = 'application/x-www-form-urlencoded'
        elif body_has_file:
            rheaders['Content-type'] = 'multipart/form-data; boundary=%s' % BOUNDARY
            rheaders['content-length'] = '%d' % len(rbody)

        response, content = self.http.request(url, method, rbody, rheaders)

        if self.persistent and 'set-cookie' in response:
            self.headers['set-cookie'] = response['set-cookie']

        if not self.persistent:
            if 'connection' in response:
                self.headers['connection'] = response['connection']

        return response, content
Beispiel #5
0
    def request(self, url, method, body=None, headers=None):
        if not isinstance(url, basestring):
            raise TypeError, 'Bolacha.request, parameter url must be ' \
                  'a string. Got %s' % repr(url)

        if not isinstance(method, basestring):
            raise TypeError, 'Bolacha.request, parameter method must be ' \
                  'a string. Got %s' % repr(method)

        if method not in HTTP_METHODS:
            raise TypeError, 'Bolacha.request, parameter method must be ' \
                  'a valid HTTP method. Got %s. %s' % (method,
                                                        RFC_LOCATION)

        if body is None:
            body = ''

        if not isinstance(body, (basestring, dict)):
            raise TypeError, 'Bolacha.request, parameter body must be ' \
                  'a string or dict. Got %s.' % (repr(body))

        is_urlencoded = False

        body_has_file = isinstance(body, dict) and any([is_file(value)
                                                       for key,value
                                                       in expand_items(body)])

        #We can only send files with POST or PUT
        if body_has_file and method not in ('POST', 'PUT'):
            raise Exception('Bolacha.request, can only send files with POST or PUT!')

        if isinstance(body, dict):
            if body_has_file:
                rbody = encode_multipart(BOUNDARY, body)
            else:
                rbody = urlencode(body, doseq=True)
                is_urlencoded = True
        else:
            rbody = body

        if headers is None:
            headers = {}

        if not isinstance(headers, dict):
            raise TypeError, 'Bolacha.request, parameter headers must be ' \
                  'a dict or NoneType. Got %s' % repr(headers)

        rheaders = self.headers.copy()
        rheaders.update(headers)

        if self.persistent:
            if 'set-cookie' in self.headers:
                rheaders['Cookie'] = self.headers['set-cookie']

        if 'set-cookie' in rheaders:
            del rheaders['set-cookie']

        if is_urlencoded and not 'Content-type' in rheaders:
            rheaders['Content-type'] = 'application/x-www-form-urlencoded'
        elif body_has_file:
            rheaders['Content-type'] = 'multipart/form-data; boundary=%s' % BOUNDARY
            rheaders['content-length'] = '%d' % len(rbody)

        #For httplib2, if the request is either GET or HEAD, need to append
        #body to url.
        if method in ('GET', 'HEAD'):
            url += '?%s' % rbody
            rbody = ''

        response, content = self.http.request(url, method, rbody, rheaders)

        if self.persistent and 'set-cookie' in response:
            self.headers['set-cookie'] = response['set-cookie']

        if not self.persistent:
            if 'connection' in response:
                self.headers['connection'] = response['connection']

        return response, content