Added to redirect when the page size is invalid in the Pagination utility.

This commit is contained in:
依瑪貓 2023-02-04 10:55:49 +08:00
parent 62697fb782
commit 5090e59bb1
2 changed files with 4 additions and 1 deletions

View File

@ -117,7 +117,7 @@ class Pagination(t.Generic[T]):
page_size: int = int(request.args["page-size"]) page_size: int = int(request.args["page-size"])
except ValueError: except ValueError:
raise Redirection(self.__uri_set("page-size", None)) raise Redirection(self.__uri_set("page-size", None))
if page_size == self.DEFAULT_PAGE_SIZE: if page_size == self.DEFAULT_PAGE_SIZE or page_size < 1:
raise Redirection(self.__uri_set("page-size", None)) raise Redirection(self.__uri_set("page-size", None))
return page_size return page_size

View File

@ -276,6 +276,9 @@ class PaginationTestCase(unittest.TestCase):
f"&page-size={Pagination.DEFAULT_PAGE_SIZE}" f"&page-size={Pagination.DEFAULT_PAGE_SIZE}"
"&page-no=37&next=%2F", "&page-no=37&next=%2F",
range(1, 691), "q=word&page-no=37&next=%2F") range(1, 691), "q=word&page-no=37&next=%2F")
# An invalid page size
self.__test_malformed("q=word&page-size=0&page-no=37&next=%2F",
range(1, 691), "q=word&page-no=37&next=%2F")
# A malformed page number # A malformed page number
self.__test_malformed("q=word&page-size=15&page-no=37a&next=%2F", self.__test_malformed("q=word&page-size=15&page-no=37a&next=%2F",
range(1, 691), "q=word&page-size=15&next=%2F") range(1, 691), "q=word&page-size=15&next=%2F")