Ejemplo n.º 1
0
 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"
Ejemplo n.º 2
0
 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")
Ejemplo n.º 3
0
 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()
Ejemplo n.º 4
0
    def decorator__skip__without_arguments(self) -> None:
        with assert_raises(TypeError):

            class MyTestCase(TestCase):
                @skip  # type: ignore
                def foo(self) -> None:
                    pass
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
 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)])
Ejemplo n.º 7
0
 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()
Ejemplo n.º 8
0
 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()
Ejemplo n.º 9
0
 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()
Ejemplo n.º 10
0
 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()
Ejemplo n.º 11
0
 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()
Ejemplo n.º 12
0
 def message_and_html_message(self) -> None:
     with assert_raises(ValueError):
         http_status_page(
             HTTPStatus.NOT_ACCEPTABLE,
             message="Test",
             html_message="HTML Test",
         )
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
 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)
Ejemplo n.º 15
0
    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")
Ejemplo n.º 16
0
    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, [])
Ejemplo n.º 17
0
 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")],
         )
Ejemplo n.º 18
0
 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)
Ejemplo n.º 19
0
 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)
Ejemplo n.º 20
0
 def invalid_value_parser(self) -> None:
     with assert_raises(TypeError):
         parse_args(
             self.env,
             [(
                 "foo",  # type: ignore
                 "INVALID",
                 Multiplicity.OPTIONAL,
             )],
         )
Ejemplo n.º 21
0
    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")
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
0
 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),
             ],
         )
Ejemplo n.º 24
0
 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")
Ejemplo n.º 25
0
 def test_maximum_below_minimum(self):
     time = TimeInput()
     time.minimum = datetime.time(12, 1)
     with assert_raises(ValueError):
         time.maximum = datetime.time(12, 0)
Ejemplo n.º 26
0
 def unsupported_method(self) -> None:
     self.env["REQUEST_METHOD"] = "UNKNOWN"
     with assert_raises(ValueError):
         parse_args(self.env, [])
Ejemplo n.º 27
0
 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"
Ejemplo n.º 28
0
 def test_invalid_class(self):
     generator = _TestingGenerator([5])
     with assert_raises(TypeError):
         str(generator)
Ejemplo n.º 29
0
 def test_remove_raw_not_found(self):
     generator = HTMLChildGenerator()
     generator.extend(["foo", "bar"])
     with assert_raises(ValueError):
         generator.remove_raw("baz")
Ejemplo n.º 30
0
 def test_remove__not_found(self):
     generator = ChildGenerator()
     generator.append("foo")
     with assert_raises(ValueError):
         generator.remove("abc")
Ejemplo n.º 31
0
 def test_id_space(self):
     element = Element("div")
     with assert_raises(ValueError):
         element.id = "Test ID"
Ejemplo n.º 32
0
 def wildcard_path__not_at_end(self) -> None:
     with assert_raises(ValueError):
         self.router.add_routes([("foo/*/bar", "GET", handle_success)])
Ejemplo n.º 33
0
 def unknown_template(self) -> None:
     with assert_raises(KeyError):
         self.router.add_routes([("foo/{unknown}/bar", "GET",
                                  fail_if_called)])
Ejemplo n.º 34
0
 def test_data_get_not_set(self):
     element = Element("div")
     with assert_raises(KeyError):
         element.data["foo"]
Ejemplo n.º 35
0
 def test_data_delete_unknown(self):
     element = Element("div")
     with assert_raises(KeyError):
         del element.data["foo"]
Ejemplo n.º 36
0
 def test_step_set_invalid(self):
     time = TimeInput()
     with assert_raises(ValueError):
         time.step = 0