Ejemplo n.º 1
0
    def request_start_response(self,
                               method,
                               path,
                               HTTP_ACCEPT=None,
                               headers=None,
                               data=None,
                               json=None,
                               content_type=None,
                               token=None,
                               oauth=None,
                               jwt=None,
                               cookie=None,
                               params=None,
                               **extra):
        method = method.upper()
        extra['REQUEST_METHOD'] = method.upper()
        path = path or '/'
        extra['HTTP_ACCEPT'] = HTTP_ACCEPT or '*/*'
        extra['pulsar.connection'] = mock.MagicMock()
        heads = []
        if headers:
            heads.extend(headers)
        if json is not None:
            content_type = 'application/json'
            assert not data
            data = json
        if content_type:
            heads.append(('content-type', content_type))
        if token:
            heads.append(('Authorization', 'Bearer %s' % token))
        elif oauth:
            heads.append(('Authorization', 'OAuth %s' % oauth))
        elif jwt:
            heads.append(('Authorization', 'JWT %s' % jwt))
        if cookie:
            heads.append(('Cookie', cookie))

        if params:
            path = full_url(path, params)

        # Encode data
        if (method in ENCODE_BODY_METHODS and data is not None
                and not isinstance(data, bytes)):
            content_type = Headers(heads).get('content-type')
            if content_type is None:
                data, content_type = encode_multipart_formdata(data)
                heads.append(('content-type', content_type))
            elif content_type == 'application/json':
                data = _json.dumps(data).encode('utf-8')

        request = self.app.wsgi_request(path=path,
                                        headers=heads,
                                        body=data,
                                        **extra)
        request.environ['SERVER_NAME'] = 'localhost'
        start_response = mock.MagicMock()
        return request, start_response
Ejemplo n.º 2
0
Archivo: test.py Proyecto: SirZazu/lux
    def post(self, path=None, body=None, content_type=None, **extra):
        extra['REQUEST_METHOD'] = 'POST'
        if body and not isinstance(body, bytes):
            if content_type is None:
                body, content_type = encode_multipart_formdata(body)
            elif content_type == 'application/json':
                body = json.dumps(body).encode('utf-8')

        return self.request(path=path, content_type=content_type,
                            body=body, **extra)
Ejemplo n.º 3
0
Archivo: test.py Proyecto: SirZazu/lux
 def post(self, app=None, path=None, content_type=None, body=None,
          headers=None, **extra):
     extra['REQUEST_METHOD'] = 'POST'
     headers = headers or []
     if body and not isinstance(body, bytes):
         if content_type is None:
             body, content_type = encode_multipart_formdata(body)
     if content_type:
         headers.append(('content-type', content_type))
     return self.request(app, path=path, headers=headers,
                         body=body, **extra)
Ejemplo n.º 4
0
    def _post_put(self, m, path=None, body=None, content_type=None, **extra):
        extra['REQUEST_METHOD'] = m
        if body is not None and not isinstance(body, bytes):
            if content_type is None:
                body, content_type = encode_multipart_formdata(body)
            elif content_type == 'application/json':
                body = json.dumps(body).encode('utf-8')

        return self.request(path=path,
                            content_type=content_type,
                            body=body,
                            **extra)
Ejemplo n.º 5
0
 def _encode_params(self, data):
     content_type = self.headers.get("content-type")
     # No content type given
     if not content_type:
         if self.encode_multipart:
             return encode_multipart_formdata(data, boundary=self.multipart_boundary, charset=self.charset)
         else:
             content_type = "application/x-www-form-urlencoded"
             body = urlencode(data).encode(self.charset)
     elif content_type in JSON_CONTENT_TYPES:
         body = json.dumps(data).encode(self.charset)
     else:
         raise ValueError("Don't know how to encode body for %s" % content_type)
     return body, content_type
Ejemplo n.º 6
0
 def _encode_params(self):
     content_type = self.headers.get('content-type')
     # No content type given
     if not content_type:
         if self.encode_multipart:
             return encode_multipart_formdata(
                 self.data, boundary=self.multipart_boundary,
                 charset=self.charset)
         else:
             content_type = 'application/x-www-form-urlencoded'
             body = urlencode(self.data).encode(self.charset)
     elif content_type in JSON_CONTENT_TYPES:
         body = json.dumps(self.data).encode(self.charset)
     else:
         raise ValueError("Don't know how to encode body for %s" %
                          content_type)
     return body, content_type
Ejemplo n.º 7
0
    def request_start_response(self, method, path, HTTP_ACCEPT=None,
                               headers=None, data=None, json=None,
                               content_type=None, token=None, oauth=None,
                               jwt=None, cookie=None, params=None, **extra):
        method = method.upper()
        extra['REQUEST_METHOD'] = method.upper()
        path = path or '/'
        extra['HTTP_ACCEPT'] = HTTP_ACCEPT or '*/*'
        extra['pulsar.connection'] = mock.MagicMock()
        heads = []
        if headers:
            heads.extend(headers)
        if json is not None:
            content_type = 'application/json'
            assert not data
            data = json
        if content_type:
            heads.append(('content-type', content_type))
        if token:
            heads.append(('Authorization', 'Bearer %s' % token))
        elif oauth:
            heads.append(('Authorization', 'OAuth %s' % oauth))
        elif jwt:
            heads.append(('Authorization', 'JWT %s' % jwt))
        if cookie:
            heads.append(('Cookie', cookie))

        if params:
            path = full_url(path, params)

        # Encode data
        if (method in ENCODE_BODY_METHODS and data is not None and
                not isinstance(data, bytes)):
            content_type = Headers(heads).get('content-type')
            if content_type is None:
                data, content_type = encode_multipart_formdata(data)
                heads.append(('content-type', content_type))
            elif content_type == 'application/json':
                data = _json.dumps(data).encode('utf-8')

        request = self.app.wsgi_request(path=path, headers=heads, body=data,
                                        **extra)
        request.environ['SERVER_NAME'] = 'localhost'
        start_response = mock.MagicMock()
        return request, start_response
Ejemplo n.º 8
0
    def _encode_params(self, params):
        content_type = self.headers.get('content-type')
        # No content type given, chose one
        if not content_type:
            content_type = FORM_URL_ENCODED

        if hasattr(params, 'read'):
            params = params.read()

        if content_type in JSON_CONTENT_TYPES:
            body = _json.dumps(params)
        elif content_type == FORM_URL_ENCODED:
            body = urlencode(tuple(split_url_params(params)))
        elif content_type == MULTIPART_FORM_DATA:
            body, content_type = encode_multipart_formdata(
                params, charset=self.charset)
        else:
            body = params
        return to_bytes(body, self.charset), content_type
Ejemplo n.º 9
0
Archivo: test.py Proyecto: tourist/lux
 def post(self,
          app=None,
          path=None,
          content_type=None,
          body=None,
          headers=None,
          **extra):
     extra['REQUEST_METHOD'] = 'POST'
     headers = headers or []
     if body and not isinstance(body, bytes):
         if content_type is None:
             body, content_type = encode_multipart_formdata(body)
     if content_type:
         headers.append(('content-type', content_type))
     return self.request(app,
                         path=path,
                         headers=headers,
                         body=body,
                         **extra)
Ejemplo n.º 10
0
    def _encode_params(self, params):
        content_type = self.headers.get('content-type')
        # No content type given, chose one
        if not content_type:
            content_type = FORM_URL_ENCODED

        if hasattr(params, 'read'):
            params = params.read()

        if content_type in JSON_CONTENT_TYPES:
            body = _json.dumps(params)
        elif content_type == FORM_URL_ENCODED:
            body = urlencode(tuple(split_url_params(params)))
        elif content_type == MULTIPART_FORM_DATA:
            body, content_type = encode_multipart_formdata(
                params, charset=self.charset)
        else:
            body = params
        return to_bytes(body, self.charset), content_type
Ejemplo n.º 11
0
    def _encode_params(self, data):
        content_type = self.headers.get('content-type')
        # No content type given, chose one
        if not content_type:
            if self.encode_multipart:
                content_type = MULTIPART_FORM_DATA
            else:
                content_type = FORM_URL_ENCODED

        if content_type in JSON_CONTENT_TYPES:
            body = json.dumps(data).encode(self.charset)
        elif content_type == FORM_URL_ENCODED:
            body = urlencode(data).encode(self.charset)
        elif content_type == MULTIPART_FORM_DATA:
            body, content_type = encode_multipart_formdata(
                data, boundary=self.multipart_boundary, charset=self.charset)
        else:
            raise ValueError("Don't know how to encode body for %s" %
                             content_type)
        return body, content_type
Ejemplo n.º 12
0
    def _encode_params(self, data):
        content_type = self.headers.get('content-type')
        # No content type given, chose one
        if not content_type:
            if self.encode_multipart:
                content_type = MULTIPART_FORM_DATA
            else:
                content_type = FORM_URL_ENCODED

        if content_type in JSON_CONTENT_TYPES:
            body = json.dumps(data).encode(self.charset)
        elif content_type == FORM_URL_ENCODED:
            body = urlencode(data).encode(self.charset)
        elif content_type == MULTIPART_FORM_DATA:
            body, content_type = encode_multipart_formdata(
                data, boundary=self.multipart_boundary, charset=self.charset)
        else:
            raise ValueError("Don't know how to encode body for %s" %
                             content_type)
        return body, content_type
Ejemplo n.º 13
0
 def _encode_files(self, data):
     fields = []
     for field, val in mapping_iterator(data or ()):
         if isinstance(val, str) or isinstance(val, bytes) or not hasattr(val, "__iter__"):
             val = [val]
         for v in val:
             if v is not None:
                 if not isinstance(v, bytes):
                     v = str(v)
                 fields.append(
                     (
                         field.decode("utf-8") if isinstance(field, bytes) else field,
                         v.encode("utf-8") if isinstance(v, str) else v,
                     )
                 )
     for (k, v) in mapping_iterator(self.files):
         # support for explicit filename
         ft = None
         if isinstance(v, (tuple, list)):
             if len(v) == 2:
                 fn, fp = v
             else:
                 fn, fp, ft = v
         else:
             fn = guess_filename(v) or k
             fp = v
         if isinstance(fp, bytes):
             fp = BytesIO(fp)
         elif isinstance(fp, str):
             fp = StringIO(fp)
         if ft:
             new_v = (fn, fp.read(), ft)
         else:
             new_v = (fn, fp.read())
         fields.append((k, new_v))
     #
     return encode_multipart_formdata(fields, charset=self.charset)
Ejemplo n.º 14
0
 def _encode_files(self, data):
     fields = []
     for field, val in mapping_iterator(data or ()):
         if (isinstance(val, str) or isinstance(val, bytes)
                 or not hasattr(val, '__iter__')):
             val = [val]
         for v in val:
             if v is not None:
                 if not isinstance(v, bytes):
                     v = str(v)
                 fields.append(
                     (field.decode('utf-8')
                      if isinstance(field, bytes) else field,
                      v.encode('utf-8') if isinstance(v, str) else v))
     for (k, v) in mapping_iterator(self.files):
         # support for explicit filename
         ft = None
         if isinstance(v, (tuple, list)):
             if len(v) == 2:
                 fn, fp = v
             else:
                 fn, fp, ft = v
         else:
             fn = guess_filename(v) or k
             fp = v
         if isinstance(fp, bytes):
             fp = BytesIO(fp)
         elif isinstance(fp, str):
             fp = StringIO(fp)
         if ft:
             new_v = (fn, fp.read(), ft)
         else:
             new_v = (fn, fp.read())
         fields.append((k, new_v))
     #
     return encode_multipart_formdata(fields, charset=self.charset)
Ejemplo n.º 15
0
 def test_encode_multipart_formdata(self):
     data, ct = encode_multipart_formdata([('bla', 'foo'),
                                           ('foo', ('pippo', 'pluto'))])
     idx = data.find(b'\r\n')
     boundary = data[2:idx].decode('utf-8')
     self.assertEqual(ct, 'multipart/form-data; boundary=%s' % boundary)
Ejemplo n.º 16
0
 def test_encode_multipart_formdata(self):
     data, ct = encode_multipart_formdata([('bla', 'foo'),
                                           ('foo', ('pippo', 'pluto'))])
     idx = data.find(b'\r\n')
     boundary = data[2:idx].decode('utf-8')
     self.assertEqual(ct, 'multipart/form-data; boundary=%s' % boundary)
Ejemplo n.º 17
0
 def test_encode_multipart_formdata(self):
     data, ct = encode_multipart_formdata([("bla", "foo"), ("foo", ("pippo", "pluto"))])
     idx = data.find(b"\r\n")
     boundary = data[2:idx].decode("utf-8")
     self.assertEqual(ct, "multipart/form-data; boundary=%s" % boundary)