Beispiel #1
0
 def test_is_secure(self):
     environ = sample_environ(HTTPS='HTTPS')
     environ['wsgi.url_scheme'] = 'https'
     request = create_request_from_environ(environ)
     assert str(
         request) == 'GET https://127.0.0.1:80/ HTTP/1.1\r\nHost: 127.0.0.1\r\nHttps: HTTPS\r\n\r\n'
     assert request.is_secure()
Beispiel #2
0
 def test_create_put_from_environ(self):
     environ = sample_environ(REQUEST_METHOD='POST')
     environ['wsgi.input'] = BufferedReader(
         BytesIO(b'HTTP_REQUEST_METHOD=PUT'))
     request = create_request_from_environ(environ)
     assert request.post['HTTP_REQUEST_METHOD'] == 'PUT'
     assert request.is_method('PUT')
Beispiel #3
0
    def test_get_form_vars_with_file(self):
        environ = sample_environ(
                    REQUEST_METHOD='POST',
                    CONTENT_TYPE='multipart/form-data; boundary=---------------------------721837373350705526688164684',
                    CONTENT_LENGTH='558'
                )
        postdata = """-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="id"

1234
-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="title"


-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

Testing 123.

-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="submit"

 Add\x20
-----------------------------721837373350705526688164684--
"""
        encoding = 'utf-8'
        fp = BytesIO(postdata.encode(encoding))
        environ['wsgi.input'] = fp
        get, post, files = get_form_vars(environ)
        file = files.get('file')
        assert file.filename == 'test.txt'
        assert post.get('id') == '1234'
Beispiel #4
0
    def test_get_form_vars_with_file(self):
        environ = sample_environ(
            REQUEST_METHOD='POST',
            CONTENT_TYPE=
            'multipart/form-data; boundary=---------------------------721837373350705526688164684',
            CONTENT_LENGTH='558')
        postdata = """-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="id"

1234
-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="title"


-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

Testing 123.

-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="submit"

 Add\x20
-----------------------------721837373350705526688164684--
"""
        encoding = 'utf-8'
        fp = BytesIO(postdata.encode(encoding))
        environ['wsgi.input'] = fp
        get, post, files = get_form_vars(environ)
        file = files.get('file')
        assert file.filename == 'test.txt'
        assert post.get('id') == '1234'
Beispiel #5
0
 def test_get_form_vars(self):
     environ = sample_environ(
         QUERY_STRING='test=test',
         REQUEST_METHOD='PUT')
     get, post, files = get_form_vars(environ)
     assert get['test'] == 'test'
     assert environ['CONTENT_TYPE'] == 'application/x-www-form-urlencoded'
Beispiel #6
0
 def test_create_put_from_environ(self):
     environ = sample_environ(REQUEST_METHOD='POST')
     environ['wsgi.input'] = BufferedReader(
         BytesIO(b'HTTP_REQUEST_METHOD=PUT'))
     request = create_request_from_environ(environ)
     assert request.post['HTTP_REQUEST_METHOD'] == 'PUT'
     assert request.is_method('PUT')
Beispiel #7
0
 def test_create_mutable(self):
     environ = sample_environ()
     environ['REQUEST_METHOD'] = 'POST'
     environ['wsgi.input'] = BufferedReader(BytesIO(b'HTTP_REQUEST_METHOD=PUT'))
     request = create_request_from_environ(environ)
     new_request = copy(request)
     assert isinstance(request.post, ImmutableMultiDict)
     assert isinstance(new_request.post, MultiDict)
Beispiel #8
0
 def test_create_put_from_environ(self):
     data = 'HTTP_REQUEST_METHOD=PUT'
     environ = sample_environ(REQUEST_METHOD='POST', CONTENT_LENGTH=len(data))
     environ['wsgi.input'] = BufferedReader(BytesIO(data.encode('utf-8')))
     request = Request.from_environ(environ)
     assert request.post['HTTP_REQUEST_METHOD'] == 'PUT'
     assert not request.files
     assert request.is_method('PUT')
Beispiel #9
0
 def test_session_from_https_request(self):
     environ = sample_environ(HTTPS='HTTPS')
     request = create_request_from_environ(environ)
     assert request.is_secure()
     request.session_to_cookie()
     cookie = request.cookies[sessions.COOKIE_KEY]
     assert cookie['httponly']
     assert cookie['secure']
Beispiel #10
0
 def test_create_mutable(self):
     environ = sample_environ()
     environ['REQUEST_METHOD'] = 'POST'
     environ['wsgi.input'] = BufferedReader(
         BytesIO(b'HTTP_REQUEST_METHOD=PUT'))
     request = create_request_from_environ(environ)
     new_request = copy(request)
     assert isinstance(request.post, ImmutableMultiDict)
     assert isinstance(new_request.post, MultiDict)
Beispiel #11
0
 def test_session_from_https_request(self):
     environ = sample_environ(HTTPS='HTTPS')
     request = Request.from_environ(environ,
                                    session_class='watson.http.sessions.Memory')
     assert request.is_secure()
     request.session['arbitrary'] = 'value'
     sessions.session_to_cookie(request, Response())
     cookie = request.cookies[sessions.COOKIE_KEY]
     assert cookie['httponly']
     assert cookie['secure']
Beispiel #12
0
 def test_create(self):
     data = 'test'
     environ = sample_environ(CONTENT_LENGTH=len(data))
     environ['wsgi.input'] = BufferedReader(BytesIO(data.encode('utf-8')))
     request = Request.from_environ(environ)
     assert request.method == 'GET'
     assert not request.is_method('PUT', 'PATCH')
     assert repr(request) == '<watson.http.messages.Request method:GET url:http://127.0.0.1/>'
     assert 'Content-Length: 4' in str(request)
     assert "\r\n\r\ntest" in str(request)
Beispiel #13
0
 def test_json_body(self):
     json_str = '{"test": [1, 2, 3]}'
     environ = sample_environ(CONTENT_TYPE='application/json; charset=utf-8',
                              CONTENT_LENGTH=len(json_str),
                              REQUEST_METHOD='put')
     environ['wsgi.input'] = BufferedReader(
         BytesIO(json_str.encode('utf-8')))
     request = Request.from_environ(environ)
     json_output = json.loads(request.body)
     assert 'test' in json_output
     assert 'test' in request.json_body
Beispiel #14
0
 def test_is_xml_http_request(self):
     environ = sample_environ(HTTP_X_REQUESTED_WITH='XmlHttpRequest')
     request = Request.from_environ(environ)
     assert request.is_xml_http_request()
Beispiel #15
0
 def test_host(self):
     environ = sample_environ(HTTP_X_FORWARDED_FOR='10.11.12.13')
     request = Request.from_environ(environ)
     assert request.host() == '10.11.12.13'
Beispiel #16
0
 def test_url(self):
     environ = sample_environ()
     request = Request.from_environ(environ)
     assert request.url.path == '/'
Beispiel #17
0
 def test_is_method(self):
     environ = sample_environ()
     request = Request.from_environ(environ)
     assert request.is_method('get')
Beispiel #18
0
 def test_session_from_request(self):
     environ = sample_environ(HTTP_COOKIE='watson.session=12345')
     request = create_request_from_environ(environ)
     assert isinstance(request.session, sessions.File)
     assert isinstance(request, sessions.SessionMixin)
Beispiel #19
0
 def test_create_from_environ(self):
     environ = sample_environ()
     request = create_request_from_environ(environ)
     assert request.method == 'GET'
     assert request.is_method('GET')
Beispiel #20
0
 def test_post(self):
     data = 'test=test'
     environ = sample_environ(REQUEST_METHOD='POST', CONTENT_LENGTH=len(data))
     environ['wsgi.input'] = BufferedReader(BytesIO(data.encode('utf-8')))
     request = Request.from_environ(environ)
     assert request.post['test'] == 'test'
Beispiel #21
0
 def test_cookies(self):
     environ = sample_environ(HTTP_COOKIE='test=something;')
     request = create_request_from_environ(environ)
     assert request.cookies['test'].value == 'something'
Beispiel #22
0
 def test_host(self):
     environ = sample_environ(HTTP_X_FORWARDED_FOR='10.11.12.13')
     request = create_request_from_environ(environ)
     assert request.host() == '10.11.12.13'
Beispiel #23
0
 def test_get_vars(self):
     environ = sample_environ(
         QUERY_STRING='blah=something&someget=test&arr[]=a&arr[]=b')
     request = create_request_from_environ(environ)
     assert request.get['blah'] == 'something'
Beispiel #24
0
 def test_session(self):
     environ = sample_environ(HTTP_COOKIE='watson.session=123456;')
     request = Request.from_environ(environ,
                                    session_class='watson.http.sessions.Memory')
     assert request.session.id == '123456'
     assert isinstance(request.session, sessions.Memory)
Beispiel #25
0
 def test_body(self):
     environ = sample_environ()
     request = Request.from_environ(environ)
     request.body = 'Test'
     assert request.raw_body == b'Test'
     assert request.body == 'Test'
Beispiel #26
0
 def test_is_secure(self):
     environ = sample_environ(HTTPS='HTTPS')
     request = Request.from_environ(environ)
     assert request.is_secure()
     request = Request.from_environ({'PATH_INFO': '/', 'wsgi.url_scheme': 'https', 'HTTP_HOST': '127.0.0.1'})
     assert request.is_secure()
Beispiel #27
0
 def test_no_post(self):
     environ = sample_environ()
     request = Request.from_environ(environ)
     assert not request.post
Beispiel #28
0
 def test_get_vars(self):
     environ = sample_environ(
         QUERY_STRING='blah=something&someget=test&arr[]=a&arr[]=b')
     request = create_request_from_environ(environ)
     assert request.get['blah'] == 'something'
Beispiel #29
0
 def test_get_form_vars(self):
     environ = sample_environ(QUERY_STRING='test=test',
                              REQUEST_METHOD='PUT')
     get, post, files = get_form_vars(environ)
     assert get['test'] == 'test'
     assert environ['CONTENT_TYPE'] == 'application/x-www-form-urlencoded'
Beispiel #30
0
 def test_create_from_environ(self):
     environ = sample_environ()
     request = create_request_from_environ(environ)
     assert request.method == 'GET'
     assert request.is_method('GET')
Beispiel #31
0
 def test_cookies(self):
     environ = sample_environ(HTTP_COOKIE='test=something;')
     request = Request.from_environ(environ)
     assert request.cookies['test'].value == 'something'
Beispiel #32
0
 def test_repr(self):
     environ = sample_environ()
     assert repr(create_request_from_environ(environ)
                 ) == '<watson.http.messages.Request method:GET url:http://127.0.0.1/>'
Beispiel #33
0
 def test_is_xml_http_request(self):
     environ = sample_environ(HTTP_X_REQUESTED_WITH='XmlHttpRequest')
     request = create_request_from_environ(environ)
     assert request.is_xml_http_request()
Beispiel #34
0
 def test_get_vars(self):
     environ = sample_environ(
         QUERY_STRING='blah=something&someget=test&arr[]=a&arr[]=b')
     request = Request.from_environ(environ)
     assert len(request.get['arr[]']) == 2
     assert request.get['blah'] == 'something'
Beispiel #35
0
 def test_server(self):
     environ = sample_environ()
     request = create_request_from_environ(environ)
     assert request.server['PATH_INFO'] == '/'
Beispiel #36
0
 def test_headers(self):
     environ = sample_environ()
     request = Request.from_environ(environ)
     assert len(request.headers) == 1
Beispiel #37
0
 def test_session(self):
     environ = sample_environ(HTTP_COOKIE='watson.session=123456;')
     request = create_request_from_environ(environ,
                                           'watson.http.sessions.Memory')
     assert request.session.id == '123456'
     assert isinstance(request.session, sessions.Memory)
Beispiel #38
0
 def test_server(self):
     environ = sample_environ()
     request = Request.from_environ(environ)
     assert request.server['PATH_INFO'] == '/'
Beispiel #39
0
 def test_repr(self):
     environ = sample_environ()
     assert repr(
         create_request_from_environ(environ)
     ) == '<watson.http.messages.Request method:GET url:http://127.0.0.1/>'
Beispiel #40
0
 def test_multiple_requests(self):
     request1 = messages.Request.from_environ(support.sample_environ(), 'watson.http.sessions.Memory')
     request1.session['testing'] = 'test'
     request2 = messages.Request.from_environ(support.sample_environ(), 'watson.http.sessions.Memory')
     request2.session['testing'] = 'test'
     assert request1.session.id != request2.session.id