Exemplo n.º 1
0
def test_basic(key='key', val='bingles'):
    app = build(dump_environ,{key:val})
    (status,headers,content,errors) = \
        raw_interactive(app)
    value = header_value(headers,'Set-Cookie')
    assert "Path=/;" in value
    assert "expires=" not in value
    cookie = value.split(";")[0]
    (status,headers,content,errors) = \
            raw_interactive(app,{'HTTP_COOKIE': cookie})
    assert ("%s: %s" % (key,val.replace("\n","\n    "))) in content
def test_basic(key='key', val='bingles'):
    app = build(dump_environ, {key: val})
    (status,headers,content,errors) = \
        raw_interactive(app)
    value = header_value(headers, 'Set-Cookie')
    assert "Path=/;" in value
    assert "expires=" not in value
    cookie = value.split(";")[0]
    (status,headers,content,errors) = \
            raw_interactive(app,{'HTTP_COOKIE': cookie})
    expected = ("%s: %s" % (key, val.replace("\n", "\n    ")))
    if six.PY3:
        expected = expected.encode('utf8')
    assert expected in content
Exemplo n.º 3
0
def check(username, password, path="/"):
    """ perform two-stage authentication to verify login """
    (status,headers,content,errors) = \
        raw_interactive(application,path, accept='text/html')
    assert status.startswith("401")
    challenge = WWW_AUTHENTICATE(headers)
    response = AUTHORIZATION(username=username, password=password,
                             challenge=challenge, path=path)
    assert "Digest" in response and username in response
    (status,headers,content,errors) = \
        raw_interactive(application,path,
                        HTTP_AUTHORIZATION=response)
    if status.startswith("200"):
        return content
    if status.startswith("401"):
        return None
    assert False, "Unexpected Status: %s" % status
Exemplo n.º 4
0
def check(username, password, path="/"):
    """ perform two-stage authentication to verify login """
    (status,headers,content,errors) = \
        raw_interactive(application,path, accept='text/html')
    assert status.startswith("401")
    challenge = WWW_AUTHENTICATE(headers)
    response = AUTHORIZATION(username=username, password=password,
                             challenge=challenge, path=path)
    assert "Digest" in response and username in response
    (status,headers,content,errors) = \
        raw_interactive(application,path,
                        HTTP_AUTHORIZATION=response)
    if status.startswith("200"):
        return content
    if status.startswith("401"):
        return None
    assert False, "Unexpected Status: %s" % status
Exemplo n.º 5
0
    def command(self):
        vars = {}
        app_spec = self.args[0]
        url = self.args[1]
        url = urljoin('/.command/', url)
        if self.options.config_vars:
            for item in self.option.config_vars:
                if ':' not in item:
                    raise BadCommand(
                        "Bad option, should be name:value : --config-var=%s" %
                        item)
                name, value = item.split(':', 1)
                vars[name] = value
        headers = {}
        if self.options.headers:
            for item in self.options.headers:
                if ':' not in item:
                    raise BadCommand(
                        "Bad option, should be name:value : --header=%s" %
                        item)
                name, value = item.split(':', 1)
                headers[name] = value.strip()
        if not self._scheme_re.search(app_spec):
            app_spec = 'config:' + app_spec
        if self.options.app_name:
            if '#' in app_spec:
                app_spec = app_spec.split('#', 1)[0]
            app_spec = app_spec + '#' + self.options.app_name
        app = loadapp(app_spec, relative_to=os.getcwd(), global_conf=vars)
        if self.command_name.lower() == 'post':
            request_method = 'POST'
        else:
            request_method = 'GET'
        qs = []
        for item in self.args[2:]:
            if '=' in item:
                item = quote(item.split('=', 1)[0]) + '=' + quote(
                    item.split('=', 1)[1])
            else:
                item = quote(item)
            qs.append(item)
        qs = '&'.join(qs)

        environ = {
            'REQUEST_METHOD': request_method,
            ## FIXME: shouldn't be static (an option?):
            'CONTENT_TYPE': 'text/plain',
            'wsgi.run_once': True,
            'wsgi.multithread': False,
            'wsgi.multiprocess': False,
            'wsgi.errors': sys.stderr,
            'QUERY_STRING': qs,
            'HTTP_ACCEPT': 'text/plain;q=1.0, */*;q=0.1',
            'paste.command_request': True,
        }
        if request_method == 'POST':
            environ['wsgi.input'] = sys.stdin
            environ['CONTENT_LENGTH'] = '-1'
        for name, value in headers.items():
            if name.lower() == 'content-type':
                name = 'CONTENT_TYPE'
            else:
                name = 'HTTP_' + name.upper().replace('-', '_')
            environ[name] = value

        status, headers, output, errors = raw_interactive(app, url, **environ)
        assert not errors, "errors should be printed directly to sys.stderr"
        if self.options.display_headers:
            for name, value in headers:
                sys.stdout.write('%s: %s\n' % (name, value))
            sys.stdout.write('\n')
        if six.PY3:
            sys.stdout.flush()
            sys.stdout.buffer.write(output)
            sys.stdout.buffer.flush()
        else:
            sys.stdout.write(output)
        sys.stdout.flush()
        status_int = int(status.split()[0])
        if status_int != 200:
            return status_int
Exemplo n.º 6
0
def test_noop():
    app = build(dump_environ,{})
    (status,headers,content,errors) = \
        raw_interactive(app)
    assert not header_value(headers,'Set-Cookie')
Exemplo n.º 7
0
 def command(self):
     vars = {}
     app_spec = self.args[0]
     url = self.args[1]
     url = urlparse.urljoin('/.command/', url)
     if self.options.config_vars:
         for item in self.option.config_vars:
             if ':' not in item:
                 raise BadCommand(
                     "Bad option, should be name:value : --config-var=%s" % item)
             name, value = item.split(':', 1)
             vars[name] = value
     headers = {}
     if self.options.headers:
         for item in self.options.headers:
             if ':' not in item:
                 raise BadCommand(
                     "Bad option, should be name:value : --header=%s" % item)
             name, value = item.split(':', 1)
             headers[name] = value.strip()
     if not self._scheme_re.search(app_spec):
         app_spec = 'config:'+app_spec
     if self.options.app_name:
         if '#' in app_spec:
             app_spec = app_spec.split('#', 1)[0]
         app_spec = app_spec + '#' + options.app_name
     app = loadapp(app_spec, relative_to=os.getcwd(), global_conf=vars)
     if self.command_name.lower() == 'post':
         request_method = 'POST'
     else:
         request_method = 'GET'
     qs = []
     for item in self.args[2:]:
         if '=' in item:
             item = urllib.quote(item.split('=', 1)[0]) + '=' + urllib.quote(item.split('=', 1)[1])
         else:
             item = urllib.quote(item)
         qs.append(item)
     qs = '&'.join(qs)
     
     environ = {
         'REQUEST_METHOD': request_method,
         ## FIXME: shouldn't be static (an option?):
         'CONTENT_TYPE': 'text/plain',
         'wsgi.run_once': True,
         'wsgi.multithread': False,
         'wsgi.multiprocess': False,
         'wsgi.errors': sys.stderr,
         'QUERY_STRING': qs,
         'HTTP_ACCEPT': 'text/plain;q=1.0, */*;q=0.1',
         'paste.command_request': True,
         }
     if request_method == 'POST':
         environ['wsgi.input'] = sys.stdin
         environ['CONTENT_LENGTH'] = '-1'
     for name, value in headers.items():
         if name.lower() == 'content-type':
             name = 'CONTENT_TYPE'
         else:
             name = 'HTTP_'+name.upper().replace('-', '_')
         environ[name] = value
         
     status, headers, output, errors = raw_interactive(app, url, **environ)
     assert not errors, "errors should be printed directly to sys.stderr"
     if self.options.display_headers:
         for name, value in headers:
             sys.stdout.write('%s: %s\n' % (name, value))
         sys.stdout.write('\n')
     sys.stdout.write(output)
     sys.stdout.flush()
     status_int = int(status.split()[0])
     if status_int != 200:
         return status_int
Exemplo n.º 8
0
    def command(self):
        vars = {}
        app_spec = self.args[0]
        url = self.args[1]
        url = urllib.parse.urljoin("/.command/", url)
        if self.options.config_vars:
            for item in self.option.config_vars:
                if ":" not in item:
                    raise BadCommand("Bad option, should be name:value : --config-var=%s" % item)
                name, value = item.split(":", 1)
                vars[name] = value
        headers = {}
        if self.options.headers:
            for item in self.options.headers:
                if ":" not in item:
                    raise BadCommand("Bad option, should be name:value : --header=%s" % item)
                name, value = item.split(":", 1)
                headers[name] = value.strip()
        if not self._scheme_re.search(app_spec):
            app_spec = "config:" + app_spec
        if self.options.app_name:
            if "#" in app_spec:
                app_spec = app_spec.split("#", 1)[0]
            app_spec = app_spec + "#" + options.app_name
        app = loadapp(app_spec, relative_to=os.getcwd(), global_conf=vars)
        if self.command_name.lower() == "post":
            request_method = "POST"
        else:
            request_method = "GET"
        qs = []
        for item in self.args[2:]:
            if "=" in item:
                item = urllib.parse.quote(item.split("=", 1)[0]) + "=" + urllib.parse.quote(item.split("=", 1)[1])
            else:
                item = urllib.parse.quote(item)
            qs.append(item)
        qs = "&".join(qs)

        environ = {
            "REQUEST_METHOD": request_method,
            ## FIXME: shouldn't be static (an option?):
            "CONTENT_TYPE": "text/plain",
            "wsgi.run_once": True,
            "wsgi.multithread": False,
            "wsgi.multiprocess": False,
            "wsgi.errors": sys.stderr,
            "QUERY_STRING": qs,
            "HTTP_ACCEPT": "text/plain;q=1.0, */*;q=0.1",
            "paste.command_request": True,
        }
        if request_method == "POST":
            environ["wsgi.input"] = sys.stdin
            environ["CONTENT_LENGTH"] = "-1"
        for name, value in list(headers.items()):
            if name.lower() == "content-type":
                name = "CONTENT_TYPE"
            else:
                name = "HTTP_" + name.upper().replace("-", "_")
            environ[name] = value

        status, headers, output, errors = raw_interactive(app, url, **environ)
        assert not errors, "errors should be printed directly to sys.stderr"
        if self.options.display_headers:
            for name, value in headers:
                sys.stdout.write("%s: %s\n" % (name, value))
            sys.stdout.write("\n")
        sys.stdout.write(output)
        sys.stdout.flush()
        status_int = int(status.split()[0])
        if status_int != 200:
            return status_int
def test_noop():
    app = build(dump_environ, {})
    (status,headers,content,errors) = \
        raw_interactive(app)
    assert not header_value(headers, 'Set-Cookie')