Ejemplo n.º 1
0
    def test_byte_errors(self):
        if sys.version_info < (3, 0):
            self.assertRaises(UnicodeEncodeError,
                              lambda: compat.binary(uchar()))

        else:
            self.assertRaises(TypeError, lambda: compat.binary(uchar()))
Ejemplo n.º 2
0
    def __init__(self, environ=None, path=b"/", POST=None, **kw):
        """Initialize a blank environment.
        
        The path argument may be a full URL or simple urlencoded path.

        If you pass a dictionary as environ, your keys will take prescedence.
        """

        scheme = b"http"
        netloc = b"localhost:80"
        query_string = b""

        if SCHEME_RE.search(path):
            scheme, netloc, path, query_string, fragment = urlparse.urlsplit(path)

            if ":" not in netloc:
                netloc += dict(http=b":80", https=b":443")[scheme]

        elif path and "?" in path:
            path, query_string = path.split("?", 1)

        path = urllib.unquote(path)

        # TODO: Update this to better conform to PEP 444.
        env = {
            "REQUEST_METHOD": "GET",
            "SCRIPT_NAME": "",
            "PATH_INFO": path,
            "QUERY_STRING": query_string,
            "SERVER_NAME": netloc.split(":")[0],
            "SERVER_PORT": netloc.split(":")[1],
            "HTTP_HOST": netloc,
            "SERVER_PROTOCOL": "HTTP/1.0",
            "CONTENT_LENGTH": "0",
            "wsgi.version": (2, 0),
            "wsgi.url_scheme": scheme,
            "wsgi.input": IO(""),
            # 'wsgi.errors': sys.stderr,
            "wsgi.multithread": False,
            "wsgi.multiprocess": False,
            "wsgi.run_once": True,
        }

        if POST is not None:
            env["REQUEST_METHOD"] = "POST"
            body = urllib.urlencode(POST.items() if hasattr(POST, "items") else POST)
            env["wsgi.input"] = IO(body)
            env["CONTENT_LENGTH"] = binary(len(body), "ascii")
            env["CONTENT_TYPE"] = "application/x-www-form-urlencoded"

        if environ:
            env.update(environ)

        super(LocalRequest, self).__init__(env, **kw)
Ejemplo n.º 3
0
 def test_byte_errors(self):
     if sys.version_info < (3, 0):
         self.assertRaises(UnicodeEncodeError, lambda: compat.binary(uchar()))
     
     else:
         self.assertRaises(TypeError, lambda: compat.binary(uchar()))
Ejemplo n.º 4
0
 def test_bytes(self):
     data = compat.binary(b'\xc3\xbc')
     self.assertEquals(len(data), 2)
     
     self.failUnless(isinstance(data.decode('utf8'), compat.unicode))
     self.assertEquals(len(data.decode('utf8')), 1)
Ejemplo n.º 5
0
 def __delete__(self, obj):
     del obj.length
     self.__set__(obj, binary())
Ejemplo n.º 6
0
    def test_bytes(self):
        data = compat.binary(b'\xc3\xbc')
        self.assertEquals(len(data), 2)

        self.failUnless(isinstance(data.decode('utf8'), compat.unicode))
        self.assertEquals(len(data.decode('utf8')), 1)