def test_cookie_set_and_delete(self, path, scriptroot): environ = dict(standard_environ) if scriptroot is not None: environ.update(dict(SCRIPT_NAME=scriptroot)) request = OctoPrintFlaskRequest(environ) if path: expected_path = path else: expected_path = "/" if scriptroot: expected_path = scriptroot + expected_path if path is not None: kwargs = dict(path=path) else: kwargs = dict() with mock.patch("flask.request", new=request): response = OctoPrintFlaskResponse() # test set_cookie with mock.patch("flask.Response.set_cookie") as set_cookie_mock: response.set_cookie("some_key", "some_value", **kwargs) set_cookie_mock.assert_called_once_with(response, "some_key_P5000", "some_value", path=expected_path) # test delete_cookie with mock.patch("flask.Response.delete_cookie") as delete_cookie_mock: response.delete_cookie("some_key", "some_value", **kwargs) delete_cookie_mock.assert_called_once_with(response, "some_key_P5000", "some_value", path=expected_path)
def test_cookie_set_and_delete(self, path, scriptroot): environ = dict(standard_environ) expected_suffix = "_P5000" if scriptroot is not None: environ.update(dict(SCRIPT_NAME=scriptroot)) expected_suffix += "_R" + scriptroot.replace("/", "|") request = OctoPrintFlaskRequest(environ) if path: expected_path_set = expected_path_delete = path else: expected_path_set = expected_path_delete = "/" if scriptroot: expected_path_set = scriptroot + expected_path_set if path is not None: kwargs = dict(path=path) else: kwargs = dict() with mock.patch("flask.request", new=request): response = OctoPrintFlaskResponse() # test set_cookie with mock.patch("flask.Response.set_cookie") as set_cookie_mock: response.set_cookie("some_key", "some_value", **kwargs) # set_cookie should have key and path values adjusted set_cookie_mock.assert_called_once_with(response, "some_key" + expected_suffix, "some_value", path=expected_path_set) # test delete_cookie with mock.patch("flask.Response.set_cookie") as set_cookie_mock: with mock.patch( "flask.Response.delete_cookie") as delete_cookie_mock: response.delete_cookie("some_key", **kwargs) # delete_cookie internally calls set_cookie - so our delete_cookie call still uses the non modified # key and path values, set_cookie will translate those (as tested above) delete_cookie_mock.assert_called_once_with( response, "some_key", path=expected_path_delete, domain=None) # we also test if an additional set_cookie call for the non modified versions happens, as # implemented to ensure any old cookies from before introduction of the suffixes and path handling # are deleted as well set_cookie_mock.assert_called_once_with( response, "some_key", expires=0, max_age=0, path=expected_path_delete, domain=None)
def test_environment_wrapper(self): def environment_wrapper(environ): environ.update({"TEST": "yes"}) return environ OctoPrintFlaskRequest.environment_wrapper = staticmethod(environment_wrapper) request = OctoPrintFlaskRequest(standard_environ) self.assertTrue("TEST" in request.environ)
def test_cookies(self): environ = dict(standard_environ) environ["HTTP_COOKIE"] = "postfixed_P5000=postfixed_value; " \ "postfixed_wrong_P5001=postfixed_wrong_value; " \ "unpostfixed=unpostfixed_value; " \ "both_P5000=both_postfixed_value; " \ "both=both_unpostfixed_value;" request = OctoPrintFlaskRequest(environ) cookies = request.cookies self.assertDictEqual({"postfixed": u"postfixed_value", "postfixed_wrong_P5001": u"postfixed_wrong_value", "unpostfixed": u"unpostfixed_value", "both": u"both_postfixed_value"}, cookies)
def test_cookie_suffix_with_root(self): script_root_environ = dict(standard_environ) script_root_environ["SCRIPT_NAME"] = "/path/to/octoprint" request = OctoPrintFlaskRequest(script_root_environ) self.assertEqual("_P5000_R|path|to|octoprint", request.cookie_suffix)
def test_cookie_suffix(self): request = OctoPrintFlaskRequest(standard_environ) self.assertEqual("_P5000", request.cookie_suffix)
def test_server_port(self): request = OctoPrintFlaskRequest(standard_environ) self.assertEqual("5000", request.server_port)
def test_server_name(self): request = OctoPrintFlaskRequest(standard_environ) self.assertEqual("localhost", request.server_name)