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()
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')
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'
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'
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'
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)
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')
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']
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)
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']
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)
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
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()
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'
def test_url(self): environ = sample_environ() request = Request.from_environ(environ) assert request.url.path == '/'
def test_is_method(self): environ = sample_environ() request = Request.from_environ(environ) assert request.is_method('get')
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)
def test_create_from_environ(self): environ = sample_environ() request = create_request_from_environ(environ) assert request.method == 'GET' assert request.is_method('GET')
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'
def test_cookies(self): environ = sample_environ(HTTP_COOKIE='test=something;') request = create_request_from_environ(environ) assert request.cookies['test'].value == 'something'
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'
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'
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)
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'
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()
def test_no_post(self): environ = sample_environ() request = Request.from_environ(environ) assert not request.post
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'
def test_cookies(self): environ = sample_environ(HTTP_COOKIE='test=something;') request = Request.from_environ(environ) assert request.cookies['test'].value == 'something'
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/>'
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()
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'
def test_server(self): environ = sample_environ() request = create_request_from_environ(environ) assert request.server['PATH_INFO'] == '/'
def test_headers(self): environ = sample_environ() request = Request.from_environ(environ) assert len(request.headers) == 1
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)
def test_server(self): environ = sample_environ() request = Request.from_environ(environ) assert request.server['PATH_INFO'] == '/'
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/>'
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