@@ -496,6 +496,11 @@ pub(crate) async fn get_all_platforms(
496
496
Extension ( pool) : Extension < Pool > ,
497
497
uri : Uri ,
498
498
) -> AxumResult < AxumResponse > {
499
+ let is_crate_root = params
500
+ . path
501
+ . as_ref ( )
502
+ . map ( |path| path == "index.html" )
503
+ . unwrap_or ( true ) ;
499
504
let req_path: String = params. path . unwrap_or_default ( ) ;
500
505
let req_path: Vec < & str > = req_path. split ( '/' ) . collect ( ) ;
501
506
@@ -618,7 +623,7 @@ pub(crate) async fn get_all_platforms(
618
623
doc_targets,
619
624
} ,
620
625
inner_path,
621
- use_direct_platform_links : true ,
626
+ use_direct_platform_links : is_crate_root ,
622
627
current_target,
623
628
} ;
624
629
Ok ( res. into_response ( ) )
@@ -1235,40 +1240,87 @@ mod tests {
1235
1240
1236
1241
#[ test]
1237
1242
fn platform_links_are_direct_and_without_nofollow ( ) {
1243
+ fn check_links ( response_text : String , ajax : bool , should_contain_redirect : bool ) {
1244
+ let platform_links: Vec < ( String , String ) > = kuchikiki:: parse_html ( )
1245
+ . one ( response_text)
1246
+ . select ( & format ! ( r#"{}li a"# , if ajax { "" } else { "#platforms " } ) )
1247
+ . expect ( "invalid selector" )
1248
+ . map ( |el| {
1249
+ let attributes = el. attributes . borrow ( ) ;
1250
+ let url = attributes. get ( "href" ) . expect ( "href" ) . to_string ( ) ;
1251
+ let rel = attributes. get ( "rel" ) . unwrap_or ( "" ) . to_string ( ) ;
1252
+ ( url, rel)
1253
+ } )
1254
+ . collect ( ) ;
1255
+
1256
+ assert_eq ! ( platform_links. len( ) , 2 ) ;
1257
+
1258
+ for ( url, rel) in platform_links {
1259
+ assert_eq ! (
1260
+ url. contains( "/target-redirect/" ) ,
1261
+ should_contain_redirect,
1262
+ "ajax: {ajax:?}, should_contain_redirect: {should_contain_redirect:?}" ,
1263
+ ) ;
1264
+ if !should_contain_redirect {
1265
+ assert_eq ! ( rel, "" ) ;
1266
+ } else {
1267
+ assert_eq ! ( rel, "nofollow" ) ;
1268
+ }
1269
+ }
1270
+ }
1271
+
1238
1272
wrapper ( |env| {
1239
1273
env. fake_release ( )
1240
1274
. name ( "dummy" )
1241
1275
. version ( "0.4.0" )
1242
1276
. rustdoc_file ( "dummy/index.html" )
1243
1277
. rustdoc_file ( "x86_64-pc-windows-msvc/dummy/index.html" )
1278
+ . rustdoc_file ( "x86_64-pc-windows-msvc/dummy/struct.A.html" )
1244
1279
. default_target ( "x86_64-unknown-linux-gnu" )
1245
1280
. add_target ( "x86_64-pc-windows-msvc" )
1246
1281
. create ( ) ?;
1247
1282
1248
1283
let response = env
1249
1284
. frontend ( )
1250
- . get ( "/-/menus/platforms/ dummy/0.4.0/x86_64-pc-windows-msvc " )
1285
+ . get ( "/dummy/latest/dummy " )
1251
1286
. send ( ) ?;
1252
1287
assert ! ( response. status( ) . is_success( ) ) ;
1288
+ check_links ( response. text ( ) ?, false , true ) ;
1289
+ // Same test with AJAX endpoint.
1290
+ let response = env
1291
+ . frontend ( )
1292
+ . get ( "/-/menus/platforms/dummy/latest/dummy" )
1293
+ . send ( ) ?;
1294
+ assert ! ( response. status( ) . is_success( ) ) ;
1295
+ check_links ( response. text ( ) ?, true , false ) ;
1253
1296
1254
- let platform_links : Vec < ( String , String ) > = kuchikiki :: parse_html ( )
1255
- . one ( response . text ( ) ? )
1256
- . select ( r#"li a"# )
1257
- . expect ( "invalid selector" )
1258
- . map ( |el| {
1259
- let attributes = el . attributes . borrow ( ) ;
1260
- let url = attributes . get ( "href" ) . expect ( "href" ) . to_string ( ) ;
1261
- let rel = attributes . get ( "rel" ) . unwrap_or ( "" ) . to_string ( ) ;
1262
- ( url , rel )
1263
- } )
1264
- . collect ( ) ;
1265
-
1266
- assert_eq ! ( platform_links . len ( ) , 2 ) ;
1297
+ let response = env
1298
+ . frontend ( )
1299
+ . get ( "/dummy/0.4.0/x86_64-pc-windows-msvc/dummy" )
1300
+ . send ( ) ? ;
1301
+ assert ! ( response . status ( ) . is_success ( ) ) ;
1302
+ check_links ( response . text ( ) ? , false , true ) ;
1303
+ // Same test with AJAX endpoint.
1304
+ let response = env
1305
+ . frontend ( )
1306
+ . get ( "/-/menus/platforms/dummy/0.4.0/x86_64-pc-windows-msvc/dummy" )
1307
+ . send ( ) ? ;
1308
+ assert ! ( response . status ( ) . is_success ( ) ) ;
1309
+ check_links ( response . text ( ) ? , true , true ) ;
1267
1310
1268
- for ( url, rel) in platform_links {
1269
- assert ! ( !url. contains( "/target-redirect/" ) ) ;
1270
- assert_eq ! ( rel, "" ) ;
1271
- }
1311
+ let response = env
1312
+ . frontend ( )
1313
+ . get ( "/dummy/0.4.0/x86_64-pc-windows-msvc/dummy/struct.A.html" )
1314
+ . send ( ) ?;
1315
+ assert ! ( response. status( ) . is_success( ) ) ;
1316
+ check_links ( response. text ( ) ?, false , true ) ;
1317
+ // Same test with AJAX endpoint.
1318
+ let response = env
1319
+ . frontend ( )
1320
+ . get ( "/-/menus/platforms/dummy/0.4.0/x86_64-pc-windows-msvc/dummy/struct.A.html" )
1321
+ . send ( ) ?;
1322
+ assert ! ( response. status( ) . is_success( ) ) ;
1323
+ check_links ( response. text ( ) ?, true , true ) ;
1272
1324
1273
1325
Ok ( ( ) )
1274
1326
} ) ;
0 commit comments