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