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 @@ -2358,3 +2358,31 @@ fn pagination_links_included_if_applicable() {
2358
2358
assert_eq ! ( None , page4. meta. next_page) ;
2359
2359
assert_eq ! ( Some ( "?page=2&per_page=1" . to_string( ) ) , page3. meta. prev_page) ;
2360
2360
}
2361
+
2362
+ #[ test]
2363
+ fn pagination_parameters_only_accept_integers ( ) {
2364
+ let ( app, anon, user) = TestApp :: init ( ) . with_user ( ) ;
2365
+ let user = user. as_model ( ) ;
2366
+
2367
+ app. db ( |conn| {
2368
+ CrateBuilder :: new ( "pagination_links_1" , user. id ) . expect_build ( conn) ;
2369
+ CrateBuilder :: new ( "pagination_links_2" , user. id ) . expect_build ( conn) ;
2370
+ CrateBuilder :: new ( "pagination_links_3" , user. id ) . expect_build ( conn) ;
2371
+ } ) ;
2372
+
2373
+ let invalid_per_page_json = anon
2374
+ . get_with_query :: < ( ) > ( "/api/v1/crates" , "page=1&per_page=100%22%EF%BC%8Cexception" )
2375
+ . bad_with_status ( 400 ) ;
2376
+ assert_eq ! (
2377
+ invalid_per_page_json. errors[ 0 ] . detail,
2378
+ "invalid digit found in string"
2379
+ ) ;
2380
+
2381
+ let invalid_page_json = anon
2382
+ . get_with_query :: < ( ) > ( "/api/v1/crates" , "page=100%22%EF%BC%8Cexception&per_page=1" )
2383
+ . bad_with_status ( 400 ) ;
2384
+ assert_eq ! (
2385
+ invalid_page_json. errors[ 0 ] . detail,
2386
+ "invalid digit found in string"
2387
+ ) ;
2388
+ }
You can’t perform that action at this time.
0 commit comments