def test_authenticated_user(self): environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/trac/wiki") request = _make_request(authenticated=True, **environ) # Running the app: app = MockApp("200 OK", []) call_wsgi_app(app, request, "/wiki") eq_("foobar", app.environ['REMOTE_USER'])
def test_webob_request(): """The WebOb request is available as a public attribute.""" environ = complete_environ() request = DjangoWSGIRequest(environ) ok_(hasattr(request, 'webob')) assert_is_instance(request.webob, Request) eq_(request.environ, request.webob.environ)
def test_mount_point(self): environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/trac/wiki") request = _make_request(**environ) # Running the app: app = MockApp("200 OK", []) call_wsgi_app(app, request, "/wiki") eq_(app.environ['SCRIPT_NAME'], "/dev/trac") eq_(app.environ['PATH_INFO'], "/wiki")
def test_write_response(self): app = MockWriteApp("200 It is OK", [("X-HEADER", "Foo")]) # Running a request: environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/blog/posts") request = _make_request(**environ) django_response = call_wsgi_app(app, request, "/posts") http_response_content = b"body as iterable" eq_(http_response_content, _resolve_response_body(django_response))
def test_right_request_class(self): """The WSGI handler must use the custom request class.""" environ = complete_environ(REQUEST_METHOD="GET", PATH_INFO="/") def start_response(status, response_headers): pass self.handler(environ, start_response) ok_(isinstance(self.handler.request, DjangoWSGIRequest))
def test_closure_response(self): """The .close() method in the response (if any) must be kept.""" app = MockClosingApp("200 It is OK", []) # Running a request: environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/blog/posts") request = _make_request(**environ) django_response = call_wsgi_app(app, request, "/posts") # Checking the .close() call: assert_false(app.app_iter.closed) django_response.close() ok_(app.app_iter.closed)
def test_routing_args_are_removed(self): """The ``wsgiorg.routing_args`` environment key must be removed.""" environ = { 'wsgiorg.routing_args': ((), {}), 'PATH_INFO': "/admin/models", } environ = complete_environ(**environ) request = _make_request(**environ) # Running the app: app = MockApp("200 OK", []) call_wsgi_app(app, request, "/models") ok_("wsgiorg.routing_args" not in app.environ)
def _make_stub_post_request(wsgi_input_class=BytesIO): input_ = urlencode({'foo': "bar", 'bar': "foo"}).encode() input_length = str(len(input_)) environ = { 'REQUEST_METHOD': "POST", 'CONTENT_TYPE': "application/x-www-form-urlencoded", 'CONTENT_LENGTH': input_length, 'wsgi.input': wsgi_input_class(input_), } environ = complete_environ(**environ) request = DjangoWSGIRequest(environ) return request
def test_webob_adhoc_attrs_are_removed(self): """WebOb's ad-hoc attributes must be removed.""" environ = { 'PATH_INFO': "/admin/models", 'wsgiorg.routing_args': ((), {}), 'webob.adhoc_attrs': {'foo': "bar"}, } environ = complete_environ(**environ) request = _make_request(**environ) # Running the app: app = MockApp("200 OK", []) call_wsgi_app(app, request, "/models") ok_("webob.adhoc_attrs" not in app.environ)
def test_original_environ_not_modified(self): """The original environ must have not been modified.""" original_environ = complete_environ(SCRIPT_NAME="/blog", PATH_INFO="/admin/models") request = _make_request(**original_environ) expected_environ = original_environ.copy() # Running the app: app = MockApp("200 OK", []) call_wsgi_app(app, request, "/models") # Checking the environment after calling the WSGI application, but first # let's remove WebOb's ad-hoc attributes: del request.environ['webob.adhoc_attrs'] eq_(request.environ, expected_environ)
def test_http_status(self): environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/trac/wiki") request = _make_request(**environ) # Running the app and make a valid request: app_ok = MockApp("200 Alright", []) django_response_ok = call_wsgi_app(app_ok, request, "/wiki") eq_(200, django_response_ok.status_code) eq_("Alright", django_response_ok.status_reason) # Running the app and make an invalid request: app_bad = MockApp("403 What are you trying to do?", []) django_response_bad = call_wsgi_app(app_bad, request, "/wiki") eq_(403, django_response_bad.status_code) eq_("What are you trying to do?", django_response_bad.status_reason)
def test_no_actual_reason_phrase(self): """It must not replace the status reason if a custom one was not set.""" environ = complete_environ(PATH_INFO="/app1/wsgi-view-ok/") start_response = MockStartResponse() self.handler(environ, start_response) eq_(start_response.status, "200 OK") eq_(len(start_response.response_headers), 3) eq_(start_response.response_headers[0][0], "Vary") eq_(start_response.response_headers[1][0], "X-SALUTATION") eq_(start_response.response_headers[2][0], "Content-Type")
def test_actual_reason_phrase(self): """It must use the status phrase set by the Django response, if any.""" environ = complete_environ(PATH_INFO="/app1/wsgi-view/") start_response = MockStartResponse() self.handler(environ, start_response) eq_(start_response.status, "206 One step at a time") eq_(len(start_response.response_headers), 3) eq_(start_response.response_headers[0][0], "Vary") eq_(start_response.response_headers[1][0], "X-SALUTATION") eq_(start_response.response_headers[2][0], "Content-Type")
def test_cookies_sent(self): environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/trac/wiki") request = _make_request(**environ) headers = [ ("Set-Cookie", "arg1=val1"), ("Set-Cookie", "arg2=val2; expires=Fri,%2031-Dec-2010%2023:59:59%20GMT"), ("Set-Cookie", "arg3=val3; path=/"), ("Set-Cookie", "arg4=val4; path=/wiki"), ("Set-Cookie", "arg5=val5; domain=.example.org"), ("Set-Cookie", "arg6=val6; max-age=3600"), ("Set-Cookie", "arg7=val7; expires=Fri,%2031-Dec-2010%2023:59:59%20GMT; max-age=3600; domain=.example.org; path=/wiki"), # Now let's try an Unicode cookie: ("Set-Cookie", u"arg8=val8; max-age=3600"), # TODO: The "secure" cookie *attribute* is broken in SimpleCookie. # See: http://bugs.python.org/issue1028088 #("Set-Cookie", "arg9=val9; secure"), ] expected_cookies = { 'arg1': {'value': "val1"}, 'arg2': {'value': "val2", 'expires': "Fri,%2031-Dec-2010%2023:59:59%20GMT"}, 'arg3': {'value': "val3", 'path': "/"}, 'arg4': {'value': "val4", 'path': "/wiki"}, 'arg5': {'value': "val5", 'domain': ".example.org"}, 'arg6': {'value': "val6", 'max-age': "3600"}, 'arg7': { 'value': "val7", 'expires': "Fri,%2031-Dec-2010%2023:59:59%20GMT", 'path': "/wiki", 'domain': ".example.org", 'max-age': "3600", }, 'arg8': {'value': "val8", 'max-age': "3600"}, # Why the next item as disabled? Check the `headers` variable above #'arg9': {'value': "val9", 'secure': True}, } # Running the app: app = MockApp("200 OK", headers) django_response = call_wsgi_app(app, request, "/wiki") # Checking the cookies: eq_(len(expected_cookies), len(django_response.cookies)) # Finally, let's check each cookie: for (cookie_set_name, cookie_set) in django_response.cookies.items(): expected_cookie = expected_cookies[cookie_set_name] expected_cookie_value = expected_cookie.pop("value") eq_(expected_cookie_value, cookie_set.value, 'Cookie "%s" has a wrong value ("%s")' % (cookie_set_name, cookie_set.value)) for (attr_key, attr_val) in expected_cookie.items(): eq_(cookie_set[attr_key], attr_val, 'Attribute "%s" in cookie "%s" is wrong (%s)' % (attr_key, cookie_set_name, cookie_set[attr_key]))
def test_incorrect_mount_point(self): """ WSGI apps are not run when the path left to them is not the last portion of the PATH_INFO in the original request. """ environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/trac/wiki") request = _make_request(**environ) path_info = "/trac" # Running the app: app = MockApp("200 OK", []) assert_raises(ApplicationCallError, call_wsgi_app, app, request, path_info)
def test_string_as_response(self): app = MockApp("200 It is OK", [("X-HEADER", "Foo")]) # Running a request: environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/blog/posts") request = _make_request(**environ) django_response = call_wsgi_app(app, request, "/posts") # Checking the response: http_response = ( "X-HEADER: Foo\n" "Content-Type: text/html; charset=utf-8\n" "X-Actual-Status-Reason: 200 It is OK\n" "\n" "body" ) eq_(http_response, str(django_response))
def test_headers_are_copied_over(self): environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/trac/wiki") request = _make_request(**environ) headers = [ ("X-Foo", "bar"), ("Content-Type", "text/plain"), ] # The same headers, but set in the format used by HttpResponse expected_headers = { 'x-foo': ("X-Foo", "bar"), 'content-type': ("Content-Type", "text/plain"), } # Running the app: app = MockApp("200 OK", headers) django_response = call_wsgi_app(app, request, "/wiki") eq_(expected_headers, django_response._headers)
def test_not_final_path(self): """ The path to be consumed by the WSGI app must be the end of the original PATH_INFO. """ # Loading a WSGI-powered Django view: headers = [("X-SALUTATION", "Hey")] app = MockApp("206 One step at a time", headers) django_view = make_wsgi_view(app) # Running a request: environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/app1/wsgi-view/foo/bar") request = _make_request(**environ) # Checking the response. Note "/foo" is NOT the end of PATH_INFO: assert_raises(ApplicationCallError, django_view, request, "/foo")
def test_headers_are_copied_over(self): environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/trac/wiki") request = _make_request(**environ) headers = [ ("X-Foo", "bar"), ("Content-Type", "text/plain"), ] # The same headers, but set in the format used by HttpResponse expected_headers = { 'x-foo': ("X-Foo", "bar"), 'content-type': ("Content-Type", "text/plain"), 'x-actual-status-reason': ("X-Actual-Status-Reason", "200 OK"), } # Running the app: app = MockApp("200 OK", headers) django_response = call_wsgi_app(app, request, "/wiki") eq_(expected_headers, django_response._headers)
def test_original_environ_not_modified(self): """The original environ must have not been modified.""" original_environ = \ complete_environ(SCRIPT_NAME="/blog", PATH_INFO="/admin/models") expected_environ = original_environ.copy() request = _make_request(**original_environ) # Running the app: app = MockApp("200 OK", []) call_wsgi_app(app, request, "/models") eq_(expected_environ.keys(), request.environ.keys()) for variable_name, expected_variable_value in expected_environ.items(): if variable_name == 'wsgi.input': actual_input = request.environ['wsgi.input'] eq_(0, actual_input.tell()) eq_(b"", actual_input.read()) else: eq_(expected_variable_value, request.environ[variable_name])
def test_right_path(self): """ The WSGI application view must work when called with the right path. """ # Loading a WSGI-powered Django view: headers = [("X-SALUTATION", "Hey")] app = MockApp("206 One step at a time", headers) django_view = make_wsgi_view(app) # Running a request: environ = complete_environ(SCRIPT_NAME="/dev", PATH_INFO="/app1/wsgi-view/foo/bar") request = _make_request(**environ) # Checking the response: django_response = django_view(request, "/foo/bar") eq_(django_response.status_code, 206) eq_(("X-SALUTATION", "Hey"), django_response._headers['x-salutation']) eq_(app.environ['PATH_INFO'], "/foo/bar") eq_(app.environ['SCRIPT_NAME'], "/dev/app1/wsgi-view")