File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -182,3 +182,30 @@ where
182
182
Ok ( ( ) )
183
183
}
184
184
}
185
+
186
+ #[ cfg( test) ]
187
+ mod tests {
188
+ use super :: { Page , PaginationOptions } ;
189
+ use indexmap:: IndexMap ;
190
+
191
+ #[ test]
192
+ fn page_must_be_a_number ( ) {
193
+ let mut params = IndexMap :: new ( ) ;
194
+ params. insert ( String :: from ( "page" ) , String :: from ( "not a number" ) ) ;
195
+ let page_error = Page :: new ( & params) . unwrap_err ( ) . response ( ) . unwrap ( ) ;
196
+
197
+ assert_eq ! ( page_error. status, ( 400 , "Bad Request" ) ) ;
198
+ }
199
+
200
+ #[ test]
201
+ fn per_page_must_be_a_number ( ) {
202
+ let mut params = IndexMap :: new ( ) ;
203
+ params. insert ( String :: from ( "per_page" ) , String :: from ( "not a number" ) ) ;
204
+ let per_page_error = PaginationOptions :: new ( & params)
205
+ . unwrap_err ( )
206
+ . response ( )
207
+ . unwrap ( ) ;
208
+
209
+ assert_eq ! ( per_page_error. status, ( 400 , "Bad Request" ) ) ;
210
+ }
211
+ }
Original file line number Diff line number Diff line change @@ -2364,3 +2364,31 @@ fn pagination_links_included_if_applicable() {
2364
2364
assert_eq ! ( None , page4. meta. next_page) ;
2365
2365
assert_eq ! ( Some ( "?page=2&per_page=1" . to_string( ) ) , page3. meta. prev_page) ;
2366
2366
}
2367
+
2368
+ #[ test]
2369
+ fn pagination_parameters_only_accept_integers ( ) {
2370
+ let ( app, anon, user) = TestApp :: init ( ) . with_user ( ) ;
2371
+ let user = user. as_model ( ) ;
2372
+
2373
+ app. db ( |conn| {
2374
+ CrateBuilder :: new ( "pagination_links_1" , user. id ) . expect_build ( conn) ;
2375
+ CrateBuilder :: new ( "pagination_links_2" , user. id ) . expect_build ( conn) ;
2376
+ CrateBuilder :: new ( "pagination_links_3" , user. id ) . expect_build ( conn) ;
2377
+ } ) ;
2378
+
2379
+ let invalid_per_page_json = anon
2380
+ . get_with_query :: < ( ) > ( "/api/v1/crates" , "page=1&per_page=100%22%EF%BC%8Cexception" )
2381
+ . bad_with_status ( 400 ) ;
2382
+ assert_eq ! (
2383
+ invalid_per_page_json. errors[ 0 ] . detail,
2384
+ "invalid digit found in string"
2385
+ ) ;
2386
+
2387
+ let invalid_page_json = anon
2388
+ . get_with_query :: < ( ) > ( "/api/v1/crates" , "page=100%22%EF%BC%8Cexception&per_page=1" )
2389
+ . bad_with_status ( 400 ) ;
2390
+ assert_eq ! (
2391
+ invalid_page_json. errors[ 0 ] . detail,
2392
+ "invalid digit found in string"
2393
+ ) ;
2394
+ }
You can’t perform that action at this time.
0 commit comments