def set_body_when_argument_is_set(self) -> None: request = create_request("POST", "/") request.add_argument("foo", "bar") with assert_raises(ValueError): request.body = b"" with assert_raises(ValueError): request.body = b"Body"
def test_invalid_values(self) -> None: with assert_raises(ValueError): parse_filter("foo=bar") with assert_raises(ValueError): parse_filter("foo='bar") with assert_raises(ValueError): parse_filter("foo='") with assert_raises(ValueError): parse_filter("foo=2000-12-32")
def parse_json_request__unknown_encoding(self) -> None: self.environ["wsgi.input"] = BytesIO(b"{}") self.environ["CONTENT_LENGTH"] = "2" self.environ["CONTENT_TYPE"] = "application/json; charset=unknown" handler = StubHandler(self.environ, self.start_response) with assert_raises(UnsupportedMediaType): handler.parse_json_request()
def decorator__skip__without_arguments(self) -> None: with assert_raises(TypeError): class MyTestCase(TestCase): @skip # type: ignore def foo(self) -> None: pass
def start_response_not_called(self) -> None: def app(_: WSGIEnvironment, __: StartResponse) -> Iterable[bytes]: return [] request = create_request("GET", "/foo/bar") with assert_raises(AssertionError): run_wsgi_test(app, request)
def patch_wrong_content_type__required_args(self) -> None: self.env["REQUEST_METHOD"] = "PATCH" self.env["CONTENT_TYPE"] = "application/octet-stream" self.env["CONTENT_LENGTH"] = "2" self.env["wsgi.input"] = BytesIO(b"AB") with assert_raises(BadRequest): parse_args(self.env, [("foo", str, Multiplicity.REQUIRED)])
def parse_json_request__wrong_content_type(self) -> None: self.environ["wsgi.input"] = BytesIO(b"{}") self.environ["CONTENT_LENGTH"] = "2" self.environ["CONTENT_TYPE"] = "application/octet-stream" handler = StubHandler(self.environ, self.start_response) with assert_raises(UnsupportedMediaType): handler.parse_json_request()
def parse_json_request__invalid_data(self) -> None: self.environ["wsgi.input"] = BytesIO(b"INVALID") self.environ["CONTENT_LENGTH"] = "7" self.environ["CONTENT_TYPE"] = "application/json" handler = StubHandler(self.environ, self.start_response) with assert_raises(UnsupportedMediaType): handler.parse_json_request()
def parse_json_body__invalid_encoding(self) -> None: response = FakeResponse( "200 OK", [("Content-Type", "application/json; charset=utf-8")] ) response.body = '{"föo": 5}'.encode("iso-8859-1") with assert_raises(AssertionError): response.parse_json_body()
def parse_json_body__wrong_content_encoding(self) -> None: response = FakeResponse( "200 OK", [("Content-Type", "application/json; charset=latin1")] ) response.body = b"{}" with assert_raises(AssertionError): response.parse_json_body()
def parse_json_body__invalid_json(self) -> None: response = FakeResponse( "200 OK", [("Content-Type", "application/json")] ) response.body = b'{"foo":' with assert_raises(AssertionError): response.parse_json_body()
def message_and_html_message(self) -> None: with assert_raises(ValueError): http_status_page( HTTPStatus.NOT_ACCEPTABLE, message="Test", html_message="HTML Test", )
def assert_set_cookie__no_http_only(self) -> None: response = FakeResponse("200 OK", [("Set-Cookie", "Foo=Bar")]) with assert_succeeds(AssertionError): response.assert_set_cookie("Foo", "Bar") with assert_raises(AssertionError): response.assert_set_cookie("Foo", "Bar", http_only=True) with assert_succeeds(AssertionError): response.assert_set_cookie("Foo", "Bar", http_only=False)
def assert_set_cookie__invalid_max_age(self) -> None: response = FakeResponse( "200 OK", [("Set-Cookie", "Foo=Bar; Max-Age=INVALID")] ) with assert_succeeds(AssertionError): response.assert_set_cookie("Foo", "Bar") with assert_raises(AssertionError): response.assert_set_cookie("Foo", "Bar", max_age=1234)
def handler_key_error_without_error_handling(self) -> None: def handle(_: WSGIEnvironment, __: StartResponse) -> Iterable[bytes]: raise KeyError() self.router.add_routes([("foo", "GET", handle)]) self.router.error_handling = False with assert_raises(KeyError): self.handle_wsgi("GET", "/foo")
def handle_other_errors(self) -> None: def app(_: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]: sr("500 Internal Server Error", []) return [] request = create_request("POST", "/") with assert_raises(AssertionError): run_wsgi_arguments_test(app, request, [])
def error_if_content_type_also_in_extra_headers(self) -> None: sr = StubStartResponse() with assert_raises(ValueError): respond( sr, content_type="image/png", extra_headers=[("Content-Type", "image/jpeg")], )
def assert_set_cookie__has_secure(self) -> None: response = FakeResponse("200 OK", [("Set-Cookie", "Foo=Bar; Secure")]) with assert_succeeds(AssertionError): response.assert_set_cookie("Foo", "Bar") with assert_succeeds(AssertionError): response.assert_set_cookie("Foo", "Bar", secure=True) with assert_raises(AssertionError): response.assert_set_cookie("Foo", "Bar", secure=False)
def _failing_arg_test( self, app_args: Sequence[ArgumentTemplate], expected_args: Iterable[ArgumentToTest], ) -> None: app = self._create_app(app_args) request = create_request("GET", "/") with assert_raises(AssertionError): run_wsgi_arguments_test(app, request, expected_args)
def invalid_value_parser(self) -> None: with assert_raises(TypeError): parse_args( self.env, [( "foo", # type: ignore "INVALID", Multiplicity.OPTIONAL, )], )
def template_key_error_without_error_handling(self) -> None: def raise_key_error(_: Request, __: Sequence[str], ___: str) -> None: raise KeyError() self.router.add_template_handler("handler", raise_key_error) self.router.add_routes([("foo/{handler}/bar", "GET", fail_if_called)]) self.router.error_handling = False with assert_raises(KeyError): self.handle_wsgi("GET", "/foo/xyz/bar")
def start_response_called_after_output_written(self) -> None: def app(_: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]: writer = sr("200 OK", []) writer(b"abc") sr("404 OK", [], _get_exc_info()) return [] request = create_request("GET", "/foo/bar") with assert_raises(ValueError): run_wsgi_test(app, request)
def empty_delete__required_not_supplied(self) -> None: self.env["REQUEST_METHOD"] = "DELETE" self.setup_empty_urlencoded_request() with assert_raises(ArgumentsError): parse_args( self.env, [ ("req", str, Multiplicity.REQUIRED), ("once", str, Multiplicity.REQUIRED_ANY), ], )
def get_header_value(self) -> None: response = FakeResponse( "200 OK", [ ("X-Header", "Foobar"), ("Content-Type", "image/png"), ("Allow", "GET"), ], ) assert_equal("image/png", response.get_header_value("Content-Type")) assert_equal("image/png", response.get_header_value("content-TYPE")) with assert_raises(ValueError): response.get_header_value("X-Unknown")
def test_maximum_below_minimum(self): time = TimeInput() time.minimum = datetime.time(12, 1) with assert_raises(ValueError): time.maximum = datetime.time(12, 0)
def unsupported_method(self) -> None: self.env["REQUEST_METHOD"] = "UNKNOWN" with assert_raises(ValueError): parse_args(self.env, [])
def test_set_selected_value__value_not_found(self): select = Select() select.create_option("L1", "v1") select.create_option("L2", "v2") with assert_raises(ValueError): select.selected_value = "not-found"
def test_invalid_class(self): generator = _TestingGenerator([5]) with assert_raises(TypeError): str(generator)
def test_remove_raw_not_found(self): generator = HTMLChildGenerator() generator.extend(["foo", "bar"]) with assert_raises(ValueError): generator.remove_raw("baz")
def test_remove__not_found(self): generator = ChildGenerator() generator.append("foo") with assert_raises(ValueError): generator.remove("abc")
def test_id_space(self): element = Element("div") with assert_raises(ValueError): element.id = "Test ID"
def wildcard_path__not_at_end(self) -> None: with assert_raises(ValueError): self.router.add_routes([("foo/*/bar", "GET", handle_success)])
def unknown_template(self) -> None: with assert_raises(KeyError): self.router.add_routes([("foo/{unknown}/bar", "GET", fail_if_called)])
def test_data_get_not_set(self): element = Element("div") with assert_raises(KeyError): element.data["foo"]
def test_data_delete_unknown(self): element = Element("div") with assert_raises(KeyError): del element.data["foo"]
def test_step_set_invalid(self): time = TimeInput() with assert_raises(ValueError): time.step = 0