1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
|
# frozen_string_literal: true
require "erb"
require "abstract_unit"
require "controller/fake_controllers"
require "active_support/messages/rotation_configuration"
class TestRoutingMapper < ActionDispatch::IntegrationTest
SprocketsApp = lambda { |env|
[200, { "Content-Type" => "text/html" }, ["javascripts"]]
}
class IpRestrictor
def self.matches?(request)
/192\.168\.1\.1\d\d/.match?(request.ip)
end
end
class GrumpyRestrictor
def self.matches?(request)
false
end
end
class YoutubeFavoritesRedirector
def self.call(params, request)
"http://www.youtube.com/watch?v=#{params[:youtube_id]}"
end
end
def test_logout
draw do
controller :sessions do
delete "logout" => :destroy
end
end
delete "/logout"
assert_equal "sessions#destroy", @response.body
assert_equal "/logout", logout_path
assert_equal "/logout", url_for(controller: "sessions", action: "destroy", only_path: true)
end
def test_login
draw do
default_url_options host: "rubyonrails.org"
controller :sessions do
get "login" => :new
post "login" => :create
end
end
get "/login"
assert_equal "sessions#new", @response.body
assert_equal "/login", login_path
post "/login"
assert_equal "sessions#create", @response.body
assert_equal "/login", url_for(controller: "sessions", action: "create", only_path: true)
assert_equal "/login", url_for(controller: "sessions", action: "new", only_path: true)
assert_equal "http://rubyonrails.org/login", url_for(controller: "sessions", action: "create")
assert_equal "http://rubyonrails.org/login", login_url
end
def test_login_redirect
draw do
get "account/login", to: redirect("/login")
end
get "/account/login"
verify_redirect "http://www.example.com/login"
end
def test_logout_redirect_without_to
draw do
get "account/logout" => redirect("/logout"), :as => :logout_redirect
end
assert_equal "/account/logout", logout_redirect_path
get "/account/logout"
verify_redirect "http://www.example.com/logout"
end
def test_namespace_redirect
draw do
namespace :private do
root to: redirect("/private/index")
get "index", to: "private#index"
end
end
get "/private"
verify_redirect "http://www.example.com/private/index"
end
def test_redirect_with_failing_constraint
draw do
get "hi", to: redirect("/foo"), constraints: ::TestRoutingMapper::GrumpyRestrictor
end
get "/hi"
assert_equal 404, status
end
def test_redirect_with_passing_constraint
draw do
get "hi", to: redirect("/foo"), constraints: ->(req) { true }
end
get "/hi"
assert_equal 301, status
end
def test_accepts_a_constraint_object_responding_to_call
constraint = Class.new do
def call(*); true; end
def matches?(*); false; end
end
draw do
get "/", to: "home#show", constraints: constraint.new
end
assert_nothing_raised do
get "/"
end
end
def test_namespace_with_controller_segment
assert_raise(ArgumentError) do
draw do
namespace :admin do
ActionDispatch.deprecator.silence do
get "/:controller(/:action(/:id(.:format)))"
end
end
end
end
end
def test_namespace_without_controller_segment
draw do
namespace :admin do
ActionDispatch.deprecator.silence do
get "hello/:controllers/:action"
end
end
end
get "/admin/hello/foo/new"
assert_equal "foo", @request.params["controllers"]
end
def test_session_singleton_resource
draw do
resource :session do
get :create
post :reset
end
end
get "/session"
assert_equal "sessions#create", @response.body
assert_equal "/session", session_path
post "/session"
assert_equal "sessions#create", @response.body
put "/session"
assert_equal "sessions#update", @response.body
delete "/session"
assert_equal "sessions#destroy", @response.body
get "/session/new"
assert_equal "sessions#new", @response.body
assert_equal "/session/new", new_session_path
get "/session/edit"
assert_equal "sessions#edit", @response.body
assert_equal "/session/edit", edit_session_path
post "/session/reset"
assert_equal "sessions#reset", @response.body
assert_equal "/session/reset", reset_session_path
end
def test_session_singleton_resource_for_api_app
config = ActionDispatch::Routing::RouteSet::Config.new
config.api_only = true
self.class.stub_controllers(config) do |routes|
routes.draw do
resource :session do
get :create
post :reset
end
end
@app = RoutedRackApp.new routes
end
get "/session"
assert_equal "sessions#create", @response.body
assert_equal "/session", session_path
post "/session"
assert_equal "sessions#create", @response.body
put "/session"
assert_equal "sessions#update", @response.body
delete "/session"
assert_equal "sessions#destroy", @response.body
post "/session/reset"
assert_equal "sessions#reset", @response.body
assert_equal "/session/reset", reset_session_path
get "/session/new"
assert_equal "Not Found", @response.body
get "/session/edit"
assert_equal "Not Found", @response.body
end
def test_session_info_nested_singleton_resource
draw do
resource :session do
resource :info
end
end
get "/session/info"
assert_equal "infos#show", @response.body
assert_equal "/session/info", session_info_path
end
def test_member_on_resource
draw do
resource :session do
member do
get :crush
end
end
end
get "/session/crush"
assert_equal "sessions#crush", @response.body
assert_equal "/session/crush", crush_session_path
end
def test_redirect_modulo
draw do
get "account/modulo/:name", to: redirect("/%{name}s")
end
get "/account/modulo/name"
verify_redirect "http://www.example.com/names"
end
def test_redirect_proc
draw do
get "account/proc/:name", to: redirect { |params, req| "/#{params[:name].pluralize}" }
end
get "/account/proc/person"
verify_redirect "http://www.example.com/people"
end
def test_redirect_proc_with_request
draw do
get "account/proc_req" => redirect { |params, req| "/#{req.method}" }
end
get "/account/proc_req"
verify_redirect "http://www.example.com/GET"
end
def test_redirect_hash_with_subdomain
draw do
get "mobile", to: redirect(subdomain: "mobile")
end
get "/mobile"
verify_redirect "http://mobile.example.com/mobile"
end
def test_redirect_hash_with_domain_and_path
draw do
get "documentation", to: redirect(domain: "example-documentation.com", path: "")
end
get "/documentation"
verify_redirect "http://www.example-documentation.com"
end
def test_redirect_hash_with_path
draw do
get "new_documentation", to: redirect(path: "/documentation/new")
end
get "/new_documentation"
verify_redirect "http://www.example.com/documentation/new"
end
def test_redirect_hash_with_host
draw do
get "super_new_documentation", to: redirect(host: "super-docs.com")
end
get "/super_new_documentation?section=top"
verify_redirect "http://super-docs.com/super_new_documentation?section=top"
end
def test_redirect_hash_path_substitution
draw do
get "stores/:name", to: redirect(subdomain: "stores", path: "/%{name}")
end
get "/stores/iernest"
verify_redirect "http://stores.example.com/iernest"
end
def test_redirect_hash_path_substitution_with_catch_all
draw do
get "stores/:name(*rest)", to: redirect(subdomain: "stores", path: "/%{name}%{rest}")
end
get "/stores/iernest/products"
verify_redirect "http://stores.example.com/iernest/products"
end
def test_redirect_class
draw do
get "youtube_favorites/:youtube_id/:name", to: redirect(YoutubeFavoritesRedirector)
end
get "/youtube_favorites/oHg5SJYRHA0/rick-rolld"
verify_redirect "http://www.youtube.com/watch?v=oHg5SJYRHA0"
end
def test_openid
draw do
match "openid/login", via: [:get, :post], to: "openid#login"
end
get "/openid/login"
assert_equal "openid#login", @response.body
post "/openid/login"
assert_equal "openid#login", @response.body
end
def test_bookmarks
draw do
scope "bookmark", controller: "bookmarks", as: :bookmark do
get :new, path: "build"
post :create, path: "create", as: ""
put :update
get :remove, action: :destroy, as: :remove
end
end
get "/bookmark/build"
assert_equal "bookmarks#new", @response.body
assert_equal "/bookmark/build", bookmark_new_path
post "/bookmark/create"
assert_equal "bookmarks#create", @response.body
assert_equal "/bookmark/create", bookmark_path
put "/bookmark/update"
assert_equal "bookmarks#update", @response.body
assert_equal "/bookmark/update", bookmark_update_path
get "/bookmark/remove"
assert_equal "bookmarks#destroy", @response.body
assert_equal "/bookmark/remove", bookmark_remove_path
end
def test_pagemarks
draw do
scope "pagemark", controller: "pagemarks", as: :pagemark do
get "build", action: "new", as: "new"
post "create", as: ""
put "update"
get "remove", action: :destroy, as: :remove
get "", action: :show, as: :show
end
end
get "/pagemark/build"
assert_equal "pagemarks#new", @response.body
assert_equal "/pagemark/build", pagemark_new_path
post "/pagemark/create"
assert_equal "pagemarks#create", @response.body
assert_equal "/pagemark/create", pagemark_path
put "/pagemark/update"
assert_equal "pagemarks#update", @response.body
assert_equal "/pagemark/update", pagemark_update_path
get "/pagemark/remove"
assert_equal "pagemarks#destroy", @response.body
assert_equal "/pagemark/remove", pagemark_remove_path
get "/pagemark"
assert_equal "pagemarks#show", @response.body
assert_equal "/pagemark", pagemark_show_path
end
def test_admin
draw do
constraints(ip: /192\.168\.1\.\d\d\d/) do
get "admin" => "queenbee#index"
end
constraints ::TestRoutingMapper::IpRestrictor do
get "admin/accounts" => "queenbee#accounts"
end
get "admin/passwords" => "queenbee#passwords", :constraints => ::TestRoutingMapper::IpRestrictor
end
get "/admin", headers: { "REMOTE_ADDR" => "192.168.1.100" }
assert_equal "queenbee#index", @response.body
get "/admin", headers: { "REMOTE_ADDR" => "10.0.0.100" }
assert_equal "pass", @response.headers["x-cascade"]
get "/admin/accounts", headers: { "REMOTE_ADDR" => "192.168.1.100" }
assert_equal "queenbee#accounts", @response.body
get "/admin/accounts", headers: { "REMOTE_ADDR" => "10.0.0.100" }
assert_equal "pass", @response.headers["x-cascade"]
get "/admin/passwords", headers: { "REMOTE_ADDR" => "192.168.1.100" }
assert_equal "queenbee#passwords", @response.body
get "/admin/passwords", headers: { "REMOTE_ADDR" => "10.0.0.100" }
assert_equal "pass", @response.headers["x-cascade"]
end
def test_global
draw do
controller(:global) do
get "global/hide_notice"
get "global/export", action: :export, as: :export_request
get "/export/:id/:file", action: :export, as: :export_download, constraints: { file: /.*/ }
ActionDispatch.deprecator.silence do
get "global/:action"
end
end
end
get "/global/dashboard"
assert_equal "global#dashboard", @response.body
get "/global/export"
assert_equal "global#export", @response.body
get "/global/hide_notice"
assert_equal "global#hide_notice", @response.body
get "/export/123/foo.txt"
assert_equal "global#export", @response.body
assert_equal "/global/export", export_request_path
assert_equal "/global/hide_notice", global_hide_notice_path
assert_equal "/export/123/foo.txt", export_download_path(id: 123, file: "foo.txt")
end
def test_local
draw do
ActionDispatch.deprecator.silence do
get "/local/:action", controller: "local"
end
end
get "/local/dashboard"
assert_equal "local#dashboard", @response.body
end
# tests the use of dup in url_for
def test_url_for_with_no_side_effects
draw do
get "/projects/status(.:format)"
end
# without dup, additional (and possibly unwanted) values will be present in the options (e.g. :host)
original_options = { controller: "projects", action: "status" }
options = original_options.dup
url_for options
# verify that the options passed in have not changed from the original ones
assert_equal original_options, options
end
def test_url_for_does_not_modify_controller
draw do
get "/projects/status(.:format)"
end
controller = "/projects"
options = { controller: controller, action: "status", only_path: true }
url = url_for(options)
assert_equal "/projects/status", url
assert_equal "/projects", controller
end
# tests the arguments modification free version of define_hash_access
def test_named_route_with_no_side_effects
draw do
resources :customers do
get "profile", on: :member
end
end
original_options = { host: "test.host" }
options = original_options.dup
profile_customer_url("customer_model", options)
# verify that the options passed in have not changed from the original ones
assert_equal original_options, options
end
def test_projects_status
draw do
get "/projects/status(.:format)"
end
assert_equal "/projects/status", url_for(controller: "projects", action: "status", only_path: true)
assert_equal "/projects/status.json", url_for(controller: "projects", action: "status", format: "json", only_path: true)
end
def test_projects
draw do
resources :projects, controller: :project
end
get "/projects"
assert_equal "project#index", @response.body
assert_equal "/projects", projects_path
post "/projects"
assert_equal "project#create", @response.body
get "/projects.xml"
assert_equal "project#index", @response.body
assert_equal "/projects.xml", projects_path(format: "xml")
get "/projects/new"
assert_equal "project#new", @response.body
assert_equal "/projects/new", new_project_path
get "/projects/new.xml"
assert_equal "project#new", @response.body
assert_equal "/projects/new.xml", new_project_path(format: "xml")
get "/projects/1"
assert_equal "project#show", @response.body
assert_equal "/projects/1", project_path(id: "1")
get "/projects/1.xml"
assert_equal "project#show", @response.body
assert_equal "/projects/1.xml", project_path(id: "1", format: "xml")
get "/projects/1/edit"
assert_equal "project#edit", @response.body
assert_equal "/projects/1/edit", edit_project_path(id: "1")
end
def test_projects_for_api_app
config = ActionDispatch::Routing::RouteSet::Config.new
config.api_only = true
self.class.stub_controllers(config) do |routes|
routes.draw do
resources :projects, controller: :project
end
@app = RoutedRackApp.new routes
end
get "/projects"
assert_equal "project#index", @response.body
assert_equal "/projects", projects_path
post "/projects"
assert_equal "project#create", @response.body
get "/projects.xml"
assert_equal "project#index", @response.body
assert_equal "/projects.xml", projects_path(format: "xml")
get "/projects/1"
assert_equal "project#show", @response.body
assert_equal "/projects/1", project_path(id: "1")
get "/projects/1.xml"
assert_equal "project#show", @response.body
assert_equal "/projects/1.xml", project_path(id: "1", format: "xml")
get "/projects/1/edit"
assert_equal "Not Found", @response.body
end
def test_projects_with_post_action_and_new_path_on_collection
draw do
resources :projects, controller: :project do
post "new", action: "new", on: :collection, as: :new
end
end
post "/projects/new"
assert_equal "project#new", @response.body
assert_equal "/projects/new", new_projects_path
end
def test_projects_involvements
draw do
resources :projects, controller: :project do
resources :involvements, :attachments
end
end
get "/projects/1/involvements"
assert_equal "involvements#index", @response.body
assert_equal "/projects/1/involvements", project_involvements_path(project_id: "1")
get "/projects/1/involvements/new"
assert_equal "involvements#new", @response.body
assert_equal "/projects/1/involvements/new", new_project_involvement_path(project_id: "1")
get "/projects/1/involvements/1"
assert_equal "involvements#show", @response.body
assert_equal "/projects/1/involvements/1", project_involvement_path(project_id: "1", id: "1")
put "/projects/1/involvements/1"
assert_equal "involvements#update", @response.body
delete "/projects/1/involvements/1"
assert_equal "involvements#destroy", @response.body
get "/projects/1/involvements/1/edit"
assert_equal "involvements#edit", @response.body
assert_equal "/projects/1/involvements/1/edit", edit_project_involvement_path(project_id: "1", id: "1")
end
def test_projects_attachments
draw do
resources :projects, controller: :project do
resources :involvements, :attachments
end
end
get "/projects/1/attachments"
assert_equal "attachments#index", @response.body
assert_equal "/projects/1/attachments", project_attachments_path(project_id: "1")
end
def test_projects_participants
draw do
resources :projects, controller: :project do
resources :participants do
put :update_all, on: :collection
end
end
end
get "/projects/1/participants"
assert_equal "participants#index", @response.body
assert_equal "/projects/1/participants", project_participants_path(project_id: "1")
put "/projects/1/participants/update_all"
assert_equal "participants#update_all", @response.body
assert_equal "/projects/1/participants/update_all", update_all_project_participants_path(project_id: "1")
end
def test_projects_companies
draw do
resources :projects, controller: :project do
resources :companies do
resources :people
resource :avatar, controller: :avatar
end
end
end
get "/projects/1/companies"
assert_equal "companies#index", @response.body
assert_equal "/projects/1/companies", project_companies_path(project_id: "1")
get "/projects/1/companies/1/people"
assert_equal "people#index", @response.body
assert_equal "/projects/1/companies/1/people", project_company_people_path(project_id: "1", company_id: "1")
get "/projects/1/companies/1/avatar"
assert_equal "avatar#show", @response.body
assert_equal "/projects/1/companies/1/avatar", project_company_avatar_path(project_id: "1", company_id: "1")
end
def test_project_manager
draw do
resources :projects do
resource :manager, as: :super_manager do
post :fire
end
end
end
get "/projects/1/manager"
assert_equal "managers#show", @response.body
assert_equal "/projects/1/manager", project_super_manager_path(project_id: "1")
get "/projects/1/manager/new"
assert_equal "managers#new", @response.body
assert_equal "/projects/1/manager/new", new_project_super_manager_path(project_id: "1")
post "/projects/1/manager/fire"
assert_equal "managers#fire", @response.body
assert_equal "/projects/1/manager/fire", fire_project_super_manager_path(project_id: "1")
end
def test_project_images
draw do
resources :projects do
resources :images, as: :funny_images do
post :revise, on: :member
end
end
end
get "/projects/1/images"
assert_equal "images#index", @response.body
assert_equal "/projects/1/images", project_funny_images_path(project_id: "1")
get "/projects/1/images/new"
assert_equal "images#new", @response.body
assert_equal "/projects/1/images/new", new_project_funny_image_path(project_id: "1")
post "/projects/1/images/1/revise"
assert_equal "images#revise", @response.body
assert_equal "/projects/1/images/1/revise", revise_project_funny_image_path(project_id: "1", id: "1")
end
def test_projects_people
draw do
resources :projects do
resources :people do
nested do
scope "/:access_token" do
resource :avatar
end
end
member do
put :accessible_projects
post :resend, :generate_new_password
end
end
end
end
get "/projects/1/people"
assert_equal "people#index", @response.body
assert_equal "/projects/1/people", project_people_path(project_id: "1")
get "/projects/1/people/1"
assert_equal "people#show", @response.body
assert_equal "/projects/1/people/1", project_person_path(project_id: "1", id: "1")
get "/projects/1/people/1/7a2dec8/avatar"
assert_equal "avatars#show", @response.body
assert_equal "/projects/1/people/1/7a2dec8/avatar", project_person_avatar_path(project_id: "1", person_id: "1", access_token: "7a2dec8")
put "/projects/1/people/1/accessible_projects"
assert_equal "people#accessible_projects", @response.body
assert_equal "/projects/1/people/1/accessible_projects", accessible_projects_project_person_path(project_id: "1", id: "1")
post "/projects/1/people/1/resend"
assert_equal "people#resend", @response.body
assert_equal "/projects/1/people/1/resend", resend_project_person_path(project_id: "1", id: "1")
post "/projects/1/people/1/generate_new_password"
assert_equal "people#generate_new_password", @response.body
assert_equal "/projects/1/people/1/generate_new_password", generate_new_password_project_person_path(project_id: "1", id: "1")
end
def test_projects_with_resources_path_names
draw do
resources_path_names correlation_indexes: "info_about_correlation_indexes"
resources :projects do
get :correlation_indexes, on: :collection
end
end
get "/projects/info_about_correlation_indexes"
assert_equal "projects#correlation_indexes", @response.body
assert_equal "/projects/info_about_correlation_indexes", correlation_indexes_projects_path
end
def test_projects_posts
draw do
resources :projects do
resources :posts do
get :archive, :toggle_view, on: :collection
post :preview, on: :member
resource :subscription
resources :comments do
post :preview, on: :collection
end
end
end
end
get "/projects/1/posts"
assert_equal "posts#index", @response.body
assert_equal "/projects/1/posts", project_posts_path(project_id: "1")
get "/projects/1/posts/archive"
assert_equal "posts#archive", @response.body
assert_equal "/projects/1/posts/archive", archive_project_posts_path(project_id: "1")
get "/projects/1/posts/toggle_view"
assert_equal "posts#toggle_view", @response.body
assert_equal "/projects/1/posts/toggle_view", toggle_view_project_posts_path(project_id: "1")
post "/projects/1/posts/1/preview"
assert_equal "posts#preview", @response.body
assert_equal "/projects/1/posts/1/preview", preview_project_post_path(project_id: "1", id: "1")
get "/projects/1/posts/1/subscription"
assert_equal "subscriptions#show", @response.body
assert_equal "/projects/1/posts/1/subscription", project_post_subscription_path(project_id: "1", post_id: "1")
get "/projects/1/posts/1/comments"
assert_equal "comments#index", @response.body
assert_equal "/projects/1/posts/1/comments", project_post_comments_path(project_id: "1", post_id: "1")
post "/projects/1/posts/1/comments/preview"
assert_equal "comments#preview", @response.body
assert_equal "/projects/1/posts/1/comments/preview", preview_project_post_comments_path(project_id: "1", post_id: "1")
end
def test_replies
draw do
resources :replies do
member do
put :answer, action: :mark_as_answer
delete :answer, action: :unmark_as_answer
end
end
end
put "/replies/1/answer"
assert_equal "replies#mark_as_answer", @response.body
delete "/replies/1/answer"
assert_equal "replies#unmark_as_answer", @response.body
end
def test_resource_routes_with_only_and_except
draw do
resources :posts, only: [:index, :show] do
resources :comments, except: :destroy
end
end
get "/posts"
assert_equal "posts#index", @response.body
assert_equal "/posts", posts_path
get "/posts/1"
assert_equal "posts#show", @response.body
assert_equal "/posts/1", post_path(id: 1)
get "/posts/1/comments"
assert_equal "comments#index", @response.body
assert_equal "/posts/1/comments", post_comments_path(post_id: 1)
post "/posts"
assert_equal "pass", @response.headers["x-cascade"]
put "/posts/1"
assert_equal "pass", @response.headers["x-cascade"]
delete "/posts/1"
assert_equal "pass", @response.headers["x-cascade"]
delete "/posts/1/comments"
assert_equal "pass", @response.headers["x-cascade"]
end
def test_resource_routes_only_create_update_destroy
draw do
resource :past, only: :destroy
resource :present, only: :update
resource :future, only: :create
end
delete "/past"
assert_equal "pasts#destroy", @response.body
assert_equal "/past", past_path
patch "/present"
assert_equal "presents#update", @response.body
assert_equal "/present", present_path
put "/present"
assert_equal "presents#update", @response.body
assert_equal "/present", present_path
post "/future"
assert_equal "futures#create", @response.body
assert_equal "/future", future_path
end
def test_resources_routes_only_create_update_destroy
draw do
resources :relationships, only: [:create, :destroy]
resources :friendships, only: [:update]
end
post "/relationships"
assert_equal "relationships#create", @response.body
assert_equal "/relationships", relationships_path
delete "/relationships/1"
assert_equal "relationships#destroy", @response.body
assert_equal "/relationships/1", relationship_path(1)
patch "/friendships/1"
assert_equal "friendships#update", @response.body
assert_equal "/friendships/1", friendship_path(1)
put "/friendships/1"
assert_equal "friendships#update", @response.body
assert_equal "/friendships/1", friendship_path(1)
end
def test_resource_with_slugs_in_ids
draw do
resources :posts
end
get "/posts/rails-rocks"
assert_equal "posts#show", @response.body
assert_equal "/posts/rails-rocks", post_path(id: "rails-rocks")
end
def test_resources_for_uncountable_names
draw do
resources :sheep do
get "_it", on: :member
end
end
assert_equal "/sheep", sheep_index_path
assert_equal "/sheep/1", sheep_path(1)
assert_equal "/sheep/new", new_sheep_path
assert_equal "/sheep/1/edit", edit_sheep_path(1)
assert_equal "/sheep/1/_it", _it_sheep_path(1)
end
def test_resource_does_not_modify_passed_options
options = { id: /.+?/, format: /json|xml/ }
draw { resource :user, options }
assert_equal({ id: /.+?/, format: /json|xml/ }, options)
end
def test_resources_does_not_modify_passed_options
options = { id: /.+?/, format: /json|xml/ }
draw { resources :users, options }
assert_equal({ id: /.+?/, format: /json|xml/ }, options)
end
def test_path_names
draw do
scope "pt", as: "pt" do
resources :projects, path_names: { edit: "editar", new: "novo" }, path: "projetos"
resource :admin, path_names: { new: "novo", activate: "ativar" }, path: "administrador" do
put :activate, on: :member
end
end
end
get "/pt/projetos"
assert_equal "projects#index", @response.body
assert_equal "/pt/projetos", pt_projects_path
get "/pt/projetos/1/editar"
assert_equal "projects#edit", @response.body
assert_equal "/pt/projetos/1/editar", edit_pt_project_path(1)
get "/pt/administrador"
assert_equal "admins#show", @response.body
assert_equal "/pt/administrador", pt_admin_path
get "/pt/administrador/novo"
assert_equal "admins#new", @response.body
assert_equal "/pt/administrador/novo", new_pt_admin_path
put "/pt/administrador/ativar"
assert_equal "admins#activate", @response.body
assert_equal "/pt/administrador/ativar", activate_pt_admin_path
end
def test_path_option_override
draw do
scope "pt", as: "pt" do
resources :projects, path_names: { new: "novo" }, path: "projetos" do
put :close, on: :member, path: "fechar"
get :open, on: :new, path: "abrir"
end
end
end
get "/pt/projetos/novo/abrir"
assert_equal "projects#open", @response.body
assert_equal "/pt/projetos/novo/abrir", open_new_pt_project_path
put "/pt/projetos/1/fechar"
assert_equal "projects#close", @response.body
assert_equal "/pt/projetos/1/fechar", close_pt_project_path(1)
end
def test_sprockets
draw do
get "sprockets.js" => ::TestRoutingMapper::SprocketsApp
end
get "/sprockets.js"
assert_equal "javascripts", @response.body
end
def test_update_person_route
draw do
get "people/:id/update", to: "people#update", as: :update_person
end
get "/people/1/update"
assert_equal "people#update", @response.body
assert_equal "/people/1/update", update_person_path(id: 1)
end
def test_update_project_person
draw do
get "/projects/:project_id/people/:id/update", to: "people#update", as: :update_project_person
end
get "/projects/1/people/2/update"
assert_equal "people#update", @response.body
assert_equal "/projects/1/people/2/update", update_project_person_path(project_id: 1, id: 2)
end
def test_forum_products
draw do
namespace :forum do
resources :products, path: "" do
resources :questions
end
end
end
get "/forum"
assert_equal "forum/products#index", @response.body
assert_equal "/forum", forum_products_path
get "/forum/basecamp"
assert_equal "forum/products#show", @response.body
assert_equal "/forum/basecamp", forum_product_path(id: "basecamp")
get "/forum/basecamp/questions"
assert_equal "forum/questions#index", @response.body
assert_equal "/forum/basecamp/questions", forum_product_questions_path(product_id: "basecamp")
get "/forum/basecamp/questions/1"
assert_equal "forum/questions#show", @response.body
assert_equal "/forum/basecamp/questions/1", forum_product_question_path(product_id: "basecamp", id: 1)
end
def test_articles_perma
draw do
get "articles/:year/:month/:day/:title", to: "articles#show", as: :article
end
get "/articles/2009/08/18/rails-3"
assert_equal "articles#show", @response.body
assert_equal "/articles/2009/8/18/rails-3", article_path(year: 2009, month: 8, day: 18, title: "rails-3")
end
def test_account_namespace
draw do
namespace :account do
resource :subscription, :credit, :credit_card
end
end
get "/account/subscription"
assert_equal "account/subscriptions#show", @response.body
assert_equal "/account/subscription", account_subscription_path
get "/account/credit"
assert_equal "account/credits#show", @response.body
assert_equal "/account/credit", account_credit_path
get "/account/credit_card"
assert_equal "account/credit_cards#show", @response.body
assert_equal "/account/credit_card", account_credit_card_path
end
def test_nested_namespace
draw do
namespace :account do
namespace :admin do
resource :subscription
end
end
end
get "/account/admin/subscription"
assert_equal "account/admin/subscriptions#show", @response.body
assert_equal "/account/admin/subscription", account_admin_subscription_path
end
def test_namespace_nested_in_resources
draw do
resources :clients do
namespace :google do
resource :account do
namespace :secret do
resource :info
end
end
end
end
end
get "/clients/1/google/account"
assert_equal "/clients/1/google/account", client_google_account_path(1)
assert_equal "google/accounts#show", @response.body
get "/clients/1/google/account/secret/info"
assert_equal "/clients/1/google/account/secret/info", client_google_account_secret_info_path(1)
assert_equal "google/secret/infos#show", @response.body
end
def test_namespace_with_options
draw do
namespace :users, path: "usuarios" do
root to: "home#index"
end
end
get "/usuarios"
assert_equal "/usuarios", users_root_path
assert_equal "users/home#index", @response.body
end
def test_namespaced_shallow_routes_with_module_option
draw do
namespace :foo, module: "bar" do
resources :posts, only: [:index, :show] do
resources :comments, only: [:index, :show], shallow: true
end
end
end
get "/foo/posts"
assert_equal "/foo/posts", foo_posts_path
assert_equal "bar/posts#index", @response.body
get "/foo/posts/1"
assert_equal "/foo/posts/1", foo_post_path("1")
assert_equal "bar/posts#show", @response.body
get "/foo/posts/1/comments"
assert_equal "/foo/posts/1/comments", foo_post_comments_path("1")
assert_equal "bar/comments#index", @response.body
get "/foo/comments/2"
assert_equal "/foo/comments/2", foo_comment_path("2")
assert_equal "bar/comments#show", @response.body
end
def test_namespaced_shallow_routes_with_path_option
draw do
namespace :foo, path: "bar" do
resources :posts, only: [:index, :show] do
resources :comments, only: [:index, :show], shallow: true
end
end
end
get "/bar/posts"
assert_equal "/bar/posts", foo_posts_path
assert_equal "foo/posts#index", @response.body
get "/bar/posts/1"
assert_equal "/bar/posts/1", foo_post_path("1")
assert_equal "foo/posts#show", @response.body
get "/bar/posts/1/comments"
assert_equal "/bar/posts/1/comments", foo_post_comments_path("1")
assert_equal "foo/comments#index", @response.body
get "/bar/comments/2"
assert_equal "/bar/comments/2", foo_comment_path("2")
assert_equal "foo/comments#show", @response.body
end
def test_namespaced_shallow_routes_with_as_option
draw do
namespace :foo, as: "bar" do
resources :posts, only: [:index, :show] do
resources :comments, only: [:index, :show], shallow: true
end
end
end
get "/foo/posts"
assert_equal "/foo/posts", bar_posts_path
assert_equal "foo/posts#index", @response.body
get "/foo/posts/1"
assert_equal "/foo/posts/1", bar_post_path("1")
assert_equal "foo/posts#show", @response.body
get "/foo/posts/1/comments"
assert_equal "/foo/posts/1/comments", bar_post_comments_path("1")
assert_equal "foo/comments#index", @response.body
get "/foo/comments/2"
assert_equal "/foo/comments/2", bar_comment_path("2")
assert_equal "foo/comments#show", @response.body
end
def test_namespaced_shallow_routes_with_shallow_path_option
draw do
namespace :foo, shallow_path: "bar" do
resources :posts, only: [:index, :show] do
resources :comments, only: [:index, :show], shallow: true
end
end
end
get "/foo/posts"
assert_equal "/foo/posts", foo_posts_path
assert_equal "foo/posts#index", @response.body
get "/foo/posts/1"
assert_equal "/foo/posts/1", foo_post_path("1")
assert_equal "foo/posts#show", @response.body
get "/foo/posts/1/comments"
assert_equal "/foo/posts/1/comments", foo_post_comments_path("1")
assert_equal "foo/comments#index", @response.body
get "/bar/comments/2"
assert_equal "/bar/comments/2", foo_comment_path("2")
assert_equal "foo/comments#show", @response.body
end
def test_namespaced_shallow_routes_with_shallow_prefix_option
draw do
namespace :foo, shallow_prefix: "bar" do
resources :posts, only: [:index, :show] do
resources :comments, only: [:index, :show], shallow: true
end
end
end
get "/foo/posts"
assert_equal "/foo/posts", foo_posts_path
assert_equal "foo/posts#index", @response.body
get "/foo/posts/1"
assert_equal "/foo/posts/1", foo_post_path("1")
assert_equal "foo/posts#show", @response.body
get "/foo/posts/1/comments"
assert_equal "/foo/posts/1/comments", foo_post_comments_path("1")
assert_equal "foo/comments#index", @response.body
get "/foo/comments/2"
assert_equal "/foo/comments/2", bar_comment_path("2")
assert_equal "foo/comments#show", @response.body
end
def test_namespace_containing_numbers
draw do
namespace :v2 do
resources :subscriptions
end
end
get "/v2/subscriptions"
assert_equal "v2/subscriptions#index", @response.body
assert_equal "/v2/subscriptions", v2_subscriptions_path
end
def test_articles_with_id
draw do
controller :articles do
scope "/articles", as: "article" do
scope path: "/:title", title: /[a-z]+/, as: :with_title do
get "/:id", action: :with_id, as: ""
end
end
end
end
get "/articles/rails/1"
assert_equal "articles#with_id", @response.body
get "/articles/123/1"
assert_equal "pass", @response.headers["x-cascade"]
assert_equal "/articles/rails/1", article_with_title_path(title: "rails", id: 1)
end
def test_access_token_rooms
draw do
scope ":access_token", constraints: { access_token: /\w{5,5}/ } do
resources :rooms
end
end
get "/12345/rooms"
assert_equal "rooms#index", @response.body
get "/12345/rooms/1"
assert_equal "rooms#show", @response.body
get "/12345/rooms/1/edit"
assert_equal "rooms#edit", @response.body
end
def test_root
draw do
root to: "projects#index"
end
assert_equal "/", root_path
get "/"
assert_equal "projects#index", @response.body
end
def test_scoped_root
draw do
scope "(:locale)", locale: /en|pl/ do
root to: "projects#index"
end
end
assert_equal "/en", root_path(locale: "en")
get "/en"
assert_equal "projects#index", @response.body
end
def test_scoped_root_as_name
draw do
scope "(:locale)", locale: /en|pl/ do
root to: "projects#index", as: "projects"
end
end
assert_equal "/en", projects_path(locale: "en")
assert_equal "/", projects_path
get "/en"
assert_equal "projects#index", @response.body
end
def test_optional_scoped_root_hierarchy
draw do
scope "(:locale)" do
scope "(:platform)" do
scope "(:browser)" do
root to: "projects#index"
end
end
end
end
assert_equal "/", root_path
assert_equal "/en", root_path(locale: "en")
assert_equal "/en/osx", root_path(locale: "en", platform: "osx")
assert_equal "/en/osx/chrome",
root_path(locale: "en", platform: "osx", browser: "chrome")
get "/"
assert_equal "projects#index", @response.body
get "/en"
assert_equal "projects#index", @response.body
get "/en/osx"
assert_equal "projects#index", @response.body
get "/en/osx/chrome"
assert_equal "projects#index", @response.body
end
def test_optional_scoped_root_multiple_choice
draw do
scope "(:locale)" do
scope "(p/:platform)" do
scope "(b/:browser)" do
root to: "projects#index"
end
end
end
end
# Note, in this particular case where we rely on pattern matching instead
# of hierarchy to match parameters in a root path, root_path returns ""
# when given no path parameters.
assert_equal "/en", root_path(locale: "en")
assert_equal "/p/osx", root_path(platform: "osx")
assert_equal "/en/p/osx", root_path(locale: "en", platform: "osx")
assert_equal "/b/chrome", root_path(browser: "chrome")
assert_equal "/en/b/chrome", root_path(locale: "en", browser: "chrome")
assert_equal "/p/osx/b/chrome",
root_path(platform: "osx", browser: "chrome")
assert_equal "/en/p/osx/b/chrome",
root_path(locale: "en", platform: "osx", browser: "chrome")
get "/en"
assert_equal "projects#index", @response.body
get "/p/osx"
assert_equal "projects#index", @response.body
get "/en/p/osx"
assert_equal "projects#index", @response.body
get "/b/chrome"
assert_equal "projects#index", @response.body
get "/en/b/chrome"
assert_equal "projects#index", @response.body
get "/p/osx/b/chrome"
assert_equal "projects#index", @response.body
get "/en/p/osx/b/chrome"
assert_equal "projects#index", @response.body
end
def test_optional_part_of_segment
draw do
get "/star-trek(-tng)/:episode", to: "star_trek#show"
end
get "/star-trek/02-15-the-trouble-with-tribbles"
assert_equal "star_trek#show", @response.body
get "/star-trek-tng/05-02-darmok"
assert_equal "star_trek#show", @response.body
end
def test_scope_with_format_option
draw do
get "direct/index", as: :no_format_direct, format: false
scope format: false do
get "scoped/index", as: :no_format_scoped
end
end
assert_equal "/direct/index", no_format_direct_path
assert_equal "/direct/index?format=html", no_format_direct_path(format: "html")
assert_equal "/scoped/index", no_format_scoped_path
assert_equal "/scoped/index?format=html", no_format_scoped_path(format: "html")
get "/scoped/index"
assert_equal "scoped#index", @response.body
get "/scoped/index.html"
assert_equal "Not Found", @response.body
end
def test_resources_with_format_false_from_scope
draw do
scope format: false do
resources :posts
resource :user
end
end
get "/posts"
assert_response :success
assert_equal "posts#index", @response.body
assert_equal "/posts", posts_path
get "/posts.html"
assert_response :not_found
assert_equal "Not Found", @response.body
assert_equal "/posts?format=html", posts_path(format: "html")
get "/user"
assert_response :success
assert_equal "users#show", @response.body
assert_equal "/user", user_path
get "/user.html"
assert_response :not_found
assert_equal "Not Found", @response.body
assert_equal "/user?format=html", user_path(format: "html")
end
def test_index
draw do
get "/info" => "projects#info", :as => "info"
end
assert_equal "/info", info_path
get "/info"
assert_equal "projects#info", @response.body
end
def test_match_with_many_paths_containing_a_slash
draw do
get "get/first", "get/second", "get/third", to: "get#show"
end
get "/get/first"
assert_equal "get#show", @response.body
get "/get/second"
assert_equal "get#show", @response.body
get "/get/third"
assert_equal "get#show", @response.body
end
def test_match_shorthand_with_no_scope
draw do
get "account/overview"
end
assert_equal "/account/overview", account_overview_path
get "/account/overview"
assert_equal "account#overview", @response.body
end
def test_match_shorthand_inside_namespace
draw do
namespace :account do
get "shorthand"
end
end
assert_equal "/account/shorthand", account_shorthand_path
get "/account/shorthand"
assert_equal "account#shorthand", @response.body
end
def test_match_shorthand_with_multiple_paths_inside_namespace
draw do
namespace :proposals do
put "activate", "inactivate"
end
end
put "/proposals/activate"
assert_equal "proposals#activate", @response.body
put "/proposals/inactivate"
assert_equal "proposals#inactivate", @response.body
end
def test_match_shorthand_inside_namespace_with_controller
draw do
namespace :api do
get "products/list"
end
end
assert_equal "/api/products/list", api_products_list_path
get "/api/products/list"
assert_equal "api/products#list", @response.body
end
def test_match_shorthand_inside_scope_with_variables_with_controller
draw do
scope ":locale" do
match "questions/new", via: [:get]
end
end
get "/de/questions/new"
assert_equal "questions#new", @response.body
assert_equal "de", @request.params[:locale]
end
def test_match_shorthand_inside_nested_namespaces_and_scopes_with_controller
draw do
namespace :api do
namespace :v3 do
scope ":locale" do
get "products/list"
end
end
end
end
get "/api/v3/en/products/list"
assert_equal "api/v3/products#list", @response.body
end
def test_not_matching_shorthand_with_dynamic_parameters
draw do
ActionDispatch.deprecator.silence do
get ":controller/:action/admin"
end
end
get "/finances/overview/admin"
assert_equal "finances#overview", @response.body
end
def test_controller_option_with_nesting_and_leading_slash
draw do
scope "/job", controller: "job" do
scope ":id", action: "manage_applicant" do
get "/active"
end
end
end
get "/job/5/active"
assert_equal "job#manage_applicant", @response.body
end
def test_dynamically_generated_helpers_on_collection_do_not_clobber_resources_url_helper
draw do
resources :replies do
collection do
get "page/:page" => "replies#index", :page => %r{\d+}
get ":page" => "replies#index", :page => %r{\d+}
end
end
end
assert_equal "/replies", replies_path
end
def test_scoped_controller_with_namespace_and_action
draw do
namespace :account do
ActionDispatch.deprecator.silence do
get ":action/callback", action: /twitter|github/, controller: "callbacks", as: :callback
end
end
end
assert_equal "/account/twitter/callback", account_callback_path("twitter")
get "/account/twitter/callback"
assert_equal "account/callbacks#twitter", @response.body
get "/account/whatever/callback"
assert_equal "Not Found", @response.body
end
def test_convention_match_nested_and_with_leading_slash
draw do
get "/account/nested/overview"
end
assert_equal "/account/nested/overview", account_nested_overview_path
get "/account/nested/overview"
assert_equal "account/nested#overview", @response.body
end
def test_convention_with_explicit_end
draw do
get "sign_in" => "sessions#new"
end
get "/sign_in"
assert_equal "sessions#new", @response.body
assert_equal "/sign_in", sign_in_path
end
def test_redirect_with_complete_url_and_status
draw do
get "account/google" => redirect("http://www.google.com/", status: 302)
end
get "/account/google"
verify_redirect "http://www.google.com/", 302
end
def test_redirect_with_port
draw do
get "account/login", to: redirect("/login")
end
previous_host, self.host = host, "www.example.com:3000"
get "/account/login"
verify_redirect "http://www.example.com:3000/login"
ensure
self.host = previous_host
end
def test_normalize_namespaced_matches
draw do
namespace :account do
get "description", action: :description, as: "description"
end
end
assert_equal "/account/description", account_description_path
get "/account/description"
assert_equal "account#description", @response.body
end
def test_namespaced_roots
draw do
namespace :account do
root to: "account#index"
end
end
assert_equal "/account", account_root_path
get "/account"
assert_equal "account/account#index", @response.body
end
def test_optional_scoped_root
draw do
scope "(:locale)", locale: /en|pl/ do
root to: "projects#index"
end
end
assert_equal "/en", root_path("en")
get "/en"
assert_equal "projects#index", @response.body
end
def test_optional_scoped_path
draw do
scope "(:locale)", locale: /en|pl/ do
resources :descriptions
end
end
assert_equal "/en/descriptions", descriptions_path("en")
assert_equal "/descriptions", descriptions_path(nil)
assert_equal "/en/descriptions/1", description_path("en", 1)
assert_equal "/descriptions/1", description_path(nil, 1)
get "/en/descriptions"
assert_equal "descriptions#index", @response.body
get "/descriptions"
assert_equal "descriptions#index", @response.body
get "/en/descriptions/1"
assert_equal "descriptions#show", @response.body
get "/descriptions/1"
assert_equal "descriptions#show", @response.body
end
def test_nested_optional_scoped_path
draw do
namespace :admin do
scope "(:locale)", locale: /en|pl/ do
resources :descriptions
end
end
end
assert_equal "/admin/en/descriptions", admin_descriptions_path("en")
assert_equal "/admin/descriptions", admin_descriptions_path(nil)
assert_equal "/admin/en/descriptions/1", admin_description_path("en", 1)
assert_equal "/admin/descriptions/1", admin_description_path(nil, 1)
get "/admin/en/descriptions"
assert_equal "admin/descriptions#index", @response.body
get "/admin/descriptions"
assert_equal "admin/descriptions#index", @response.body
get "/admin/en/descriptions/1"
assert_equal "admin/descriptions#show", @response.body
get "/admin/descriptions/1"
assert_equal "admin/descriptions#show", @response.body
end
def test_nested_optional_path_shorthand
draw do
scope "(:locale)", locale: /en|pl/ do
get "registrations/new"
end
end
get "/registrations/new"
assert_nil @request.params[:locale]
get "/en/registrations/new"
assert_equal "en", @request.params[:locale]
end
def test_default_string_params
draw do
get "inline_pages/(:id)", to: "pages#show", id: "home"
get "default_pages/(:id)", to: "pages#show", defaults: { id: "home" }
defaults id: "home" do
get "scoped_pages/(:id)", to: "pages#show"
end
end
get "/inline_pages"
assert_equal "home", @request.params[:id]
get "/default_pages"
assert_equal "home", @request.params[:id]
get "/scoped_pages"
assert_equal "home", @request.params[:id]
end
def test_default_integer_params
draw do
get "inline_pages/(:page)", to: "pages#show", page: 1
get "default_pages/(:page)", to: "pages#show", defaults: { page: 1 }
defaults page: 1 do
get "scoped_pages/(:page)", to: "pages#show"
end
end
get "/inline_pages"
assert_equal 1, @request.params[:page]
get "/default_pages"
assert_equal 1, @request.params[:page]
get "/scoped_pages"
assert_equal 1, @request.params[:page]
end
def test_keyed_default_string_params_with_match
draw do
match "/", to: "pages#show", via: :get, defaults: { id: "home" }
end
get "/"
assert_equal "home", @request.params[:id]
end
def test_default_string_params_with_match
draw do
match "/", to: "pages#show", via: :get, id: "home"
end
get "/"
assert_equal "home", @request.params[:id]
end
def test_keyed_default_string_params_with_root
draw do
root to: "pages#show", defaults: { id: "home" }
end
get "/"
assert_equal "home", @request.params[:id]
end
def test_default_string_params_with_root
draw do
root to: "pages#show", id: "home"
end
get "/"
assert_equal "home", @request.params[:id]
end
def test_resource_constraints
draw do
resources :products, constraints: { id: /\d{4}/ } do
root to: "products#root"
get :favorite, on: :collection
resources :images
end
resource :dashboard, constraints: { ip: /192\.168\.1\.\d{1,3}/ }
end
get "/products/1"
assert_equal "pass", @response.headers["x-cascade"]
get "/products"
assert_equal "products#root", @response.body
get "/products/favorite"
assert_equal "products#favorite", @response.body
get "/products/0001"
assert_equal "products#show", @response.body
get "/products/1/images"
assert_equal "pass", @response.headers["x-cascade"]
get "/products/0001/images"
assert_equal "images#index", @response.body
get "/products/0001/images/0001"
assert_equal "images#show", @response.body
get "/dashboard", headers: { "REMOTE_ADDR" => "10.0.0.100" }
assert_equal "pass", @response.headers["x-cascade"]
get "/dashboard", headers: { "REMOTE_ADDR" => "192.168.1.100" }
assert_equal "dashboards#show", @response.body
end
def test_root_works_in_the_resources_scope
draw do
resources :products do
root to: "products#root"
end
end
get "/products"
assert_equal "products#root", @response.body
assert_equal "/products", products_root_path
end
def test_module_scope
draw do
resource :token, module: :api
end
get "/token"
assert_equal "api/tokens#show", @response.body
assert_equal "/token", token_path
end
def test_path_scope
draw do
scope path: "api" do
resource :me
get "/" => "mes#index"
end
end
get "/api/me"
assert_equal "mes#show", @response.body
assert_equal "/api/me", me_path
get "/api"
assert_equal "mes#index", @response.body
end
def test_symbol_scope
draw do
scope path: "api" do
scope :v2 do
resource :me, as: "v2_me"
get "/" => "mes#index"
end
scope :v3, :admin do
resource :me, as: "v3_me"
end
end
end
get "/api/v2/me"
assert_equal "mes#show", @response.body
assert_equal "/api/v2/me", v2_me_path
get "/api/v2"
assert_equal "mes#index", @response.body
get "/api/v3/admin/me"
assert_equal "mes#show", @response.body
end
def test_url_generator_for_generic_route
draw do
ActionDispatch.deprecator.silence do
get "whatever/:controller(/:action(/:id))"
end
end
get "/whatever/foo/bar"
assert_equal "foo#bar", @response.body
assert_equal "http://www.example.com/whatever/foo/bar/1",
url_for(controller: "foo", action: "bar", id: 1)
end
def test_url_generator_for_namespaced_generic_route
draw do
ActionDispatch.deprecator.silence do
get "whatever/:controller(/:action(/:id))", id: /\d+/
end
end
get "/whatever/foo/bar/show"
assert_equal "foo/bar#show", @response.body
get "/whatever/foo/bar/show/1"
assert_equal "foo/bar#show", @response.body
assert_equal "http://www.example.com/whatever/foo/bar/show",
url_for(controller: "foo/bar", action: "show")
assert_equal "http://www.example.com/whatever/foo/bar/show/1",
url_for(controller: "foo/bar", action: "show", id: "1")
end
def test_resource_new_actions
draw do
resources :replies do
new do
post :preview
end
end
scope "pt", as: "pt" do
resources :projects, path_names: { new: "novo" }, path: "projetos" do
post :preview, on: :new
end
resource :admin, path_names: { new: "novo" }, path: "administrador" do
post :preview, on: :new
end
resources :products, path_names: { new: "novo" } do
new do
post :preview
end
end
end
resource :profile do
new do
post :preview
end
end
end
assert_equal "/replies/new/preview", preview_new_reply_path
assert_equal "/pt/projetos/novo/preview", preview_new_pt_project_path
assert_equal "/pt/administrador/novo/preview", preview_new_pt_admin_path
assert_equal "/pt/products/novo/preview", preview_new_pt_product_path
assert_equal "/profile/new/preview", preview_new_profile_path
post "/replies/new/preview"
assert_equal "replies#preview", @response.body
post "/pt/projetos/novo/preview"
assert_equal "projects#preview", @response.body
post "/pt/administrador/novo/preview"
assert_equal "admins#preview", @response.body
post "/pt/products/novo/preview"
assert_equal "products#preview", @response.body
post "/profile/new/preview"
assert_equal "profiles#preview", @response.body
end
def test_resource_merges_options_from_scope
draw do
scope only: :show do
resource :account
end
end
assert_raise(NoMethodError) { new_account_path }
get "/account/new"
assert_equal 404, status
end
def test_resources_merges_options_from_scope
draw do
scope only: [:index, :show] do
resources :products do
resources :images
end
end
end
assert_raise(NoMethodError) { edit_product_path("1") }
get "/products/1/edit"
assert_equal 404, status
assert_raise(NoMethodError) { edit_product_image_path("1", "2") }
post "/products/1/images/2/edit"
assert_equal 404, status
end
def test_shallow_nested_resources
draw do
shallow do
namespace :api do
resources :teams do
resources :players
resource :captain
end
end
end
resources :threads, shallow: true do
resource :owner
resources :messages do
resources :comments do
member do
post :preview
end
end
end
end
end
get "/api/teams"
assert_equal "api/teams#index", @response.body
assert_equal "/api/teams", api_teams_path
get "/api/teams/new"
assert_equal "api/teams#new", @response.body
assert_equal "/api/teams/new", new_api_team_path
get "/api/teams/1"
assert_equal "api/teams#show", @response.body
assert_equal "/api/teams/1", api_team_path(id: "1")
get "/api/teams/1/edit"
assert_equal "api/teams#edit", @response.body
assert_equal "/api/teams/1/edit", edit_api_team_path(id: "1")
get "/api/teams/1/players"
assert_equal "api/players#index", @response.body
assert_equal "/api/teams/1/players", api_team_players_path(team_id: "1")
get "/api/teams/1/players/new"
assert_equal "api/players#new", @response.body
assert_equal "/api/teams/1/players/new", new_api_team_player_path(team_id: "1")
get "/api/players/2"
assert_equal "api/players#show", @response.body
assert_equal "/api/players/2", api_player_path(id: "2")
get "/api/players/2/edit"
assert_equal "api/players#edit", @response.body
assert_equal "/api/players/2/edit", edit_api_player_path(id: "2")
get "/api/teams/1/captain"
assert_equal "api/captains#show", @response.body
assert_equal "/api/teams/1/captain", api_team_captain_path(team_id: "1")
get "/api/teams/1/captain/new"
assert_equal "api/captains#new", @response.body
assert_equal "/api/teams/1/captain/new", new_api_team_captain_path(team_id: "1")
get "/api/teams/1/captain/edit"
assert_equal "api/captains#edit", @response.body
assert_equal "/api/teams/1/captain/edit", edit_api_team_captain_path(team_id: "1")
get "/threads"
assert_equal "threads#index", @response.body
assert_equal "/threads", threads_path
get "/threads/new"
assert_equal "threads#new", @response.body
assert_equal "/threads/new", new_thread_path
get "/threads/1"
assert_equal "threads#show", @response.body
assert_equal "/threads/1", thread_path(id: "1")
get "/threads/1/edit"
assert_equal "threads#edit", @response.body
assert_equal "/threads/1/edit", edit_thread_path(id: "1")
get "/threads/1/owner"
assert_equal "owners#show", @response.body
assert_equal "/threads/1/owner", thread_owner_path(thread_id: "1")
get "/threads/1/messages"
assert_equal "messages#index", @response.body
assert_equal "/threads/1/messages", thread_messages_path(thread_id: "1")
get "/threads/1/messages/new"
assert_equal "messages#new", @response.body
assert_equal "/threads/1/messages/new", new_thread_message_path(thread_id: "1")
get "/messages/2"
assert_equal "messages#show", @response.body
assert_equal "/messages/2", message_path(id: "2")
get "/messages/2/edit"
assert_equal "messages#edit", @response.body
assert_equal "/messages/2/edit", edit_message_path(id: "2")
get "/messages/2/comments"
assert_equal "comments#index", @response.body
assert_equal "/messages/2/comments", message_comments_path(message_id: "2")
get "/messages/2/comments/new"
assert_equal "comments#new", @response.body
assert_equal "/messages/2/comments/new", new_message_comment_path(message_id: "2")
get "/comments/3"
assert_equal "comments#show", @response.body
assert_equal "/comments/3", comment_path(id: "3")
get "/comments/3/edit"
assert_equal "comments#edit", @response.body
assert_equal "/comments/3/edit", edit_comment_path(id: "3")
post "/comments/3/preview"
assert_equal "comments#preview", @response.body
assert_equal "/comments/3/preview", preview_comment_path(id: "3")
end
def test_shallow_nested_resources_inside_resource
draw do
resource :membership, shallow: true do
resources :cards
end
end
get "/membership/cards"
assert_equal "cards#index", @response.body
assert_equal "/membership/cards", membership_cards_path
get "/membership/cards/new"
assert_equal "cards#new", @response.body
assert_equal "/membership/cards/new", new_membership_card_path
post "/membership/cards"
assert_equal "cards#create", @response.body
get "/cards/1"
assert_equal "cards#show", @response.body
assert_equal "/cards/1", card_path("1")
get "/cards/1/edit"
assert_equal "cards#edit", @response.body
assert_equal "/cards/1/edit", edit_card_path("1")
put "/cards/1"
assert_equal "cards#update", @response.body
patch "/cards/1"
assert_equal "cards#update", @response.body
delete "/cards/1"
assert_equal "cards#destroy", @response.body
end
def test_shallow_false_inside_nested_shallow_resource
draw do
resources :blogs, shallow: true do
resources :posts do
resources :comments, shallow: false
resources :tags
end
end
end
get "/posts/1/comments"
assert_equal "comments#index", @response.body
assert_equal "/posts/1/comments", post_comments_path("1")
get "/posts/1/comments/new"
assert_equal "comments#new", @response.body
assert_equal "/posts/1/comments/new", new_post_comment_path("1")
get "/posts/1/comments/2"
assert_equal "comments#show", @response.body
assert_equal "/posts/1/comments/2", post_comment_path("1", "2")
get "/posts/1/comments/2/edit"
assert_equal "comments#edit", @response.body
assert_equal "/posts/1/comments/2/edit", edit_post_comment_path("1", "2")
get "/tags/3"
assert_equal "tags#show", @response.body
assert_equal "/tags/3", tag_path("3")
end
def test_shallow_deeply_nested_resources
draw do
resources :blogs do
resources :posts do
resources :comments, shallow: true
end
end
end
get "/comments/1"
assert_equal "comments#show", @response.body
assert_equal "/comments/1", comment_path("1")
assert_equal "/blogs/new", new_blog_path
assert_equal "/blogs/1/posts/new", new_blog_post_path(blog_id: 1)
assert_equal "/blogs/1/posts/2/comments/new", new_blog_post_comment_path(blog_id: 1, post_id: 2)
end
def test_direct_children_of_shallow_resources
draw do
resources :blogs do
resources :posts, shallow: true do
resources :comments
end
end
end
post "/posts/1/comments"
assert_equal "comments#create", @response.body
assert_equal "/posts/1/comments", post_comments_path("1")
get "/posts/2/comments/new"
assert_equal "comments#new", @response.body
assert_equal "/posts/2/comments/new", new_post_comment_path("2")
get "/posts/1/comments"
assert_equal "comments#index", @response.body
assert_equal "/posts/1/comments", post_comments_path("1")
end
def test_shallow_nested_resources_within_scope
draw do
scope "/hello" do
shallow do
resources :notes do
resources :trackbacks
end
end
end
end
get "/hello/notes/1/trackbacks"
assert_equal "trackbacks#index", @response.body
assert_equal "/hello/notes/1/trackbacks", note_trackbacks_path(note_id: 1)
get "/hello/notes/1/edit"
assert_equal "notes#edit", @response.body
assert_equal "/hello/notes/1/edit", edit_note_path(id: "1")
get "/hello/notes/1/trackbacks/new"
assert_equal "trackbacks#new", @response.body
assert_equal "/hello/notes/1/trackbacks/new", new_note_trackback_path(note_id: 1)
get "/hello/trackbacks/1"
assert_equal "trackbacks#show", @response.body
assert_equal "/hello/trackbacks/1", trackback_path(id: "1")
get "/hello/trackbacks/1/edit"
assert_equal "trackbacks#edit", @response.body
assert_equal "/hello/trackbacks/1/edit", edit_trackback_path(id: "1")
put "/hello/trackbacks/1"
assert_equal "trackbacks#update", @response.body
post "/hello/notes/1/trackbacks"
assert_equal "trackbacks#create", @response.body
delete "/hello/trackbacks/1"
assert_equal "trackbacks#destroy", @response.body
get "/hello/notes"
assert_equal "notes#index", @response.body
post "/hello/notes"
assert_equal "notes#create", @response.body
get "/hello/notes/new"
assert_equal "notes#new", @response.body
assert_equal "/hello/notes/new", new_note_path
get "/hello/notes/1"
assert_equal "notes#show", @response.body
assert_equal "/hello/notes/1", note_path(id: 1)
put "/hello/notes/1"
assert_equal "notes#update", @response.body
delete "/hello/notes/1"
assert_equal "notes#destroy", @response.body
end
def test_shallow_option_nested_resources_within_scope
draw do
scope "/hello" do
resources :notes, shallow: true do
resources :trackbacks
end
end
end
get "/hello/notes/1/trackbacks"
assert_equal "trackbacks#index", @response.body
assert_equal "/hello/notes/1/trackbacks", note_trackbacks_path(note_id: 1)
get "/hello/notes/1/edit"
assert_equal "notes#edit", @response.body
assert_equal "/hello/notes/1/edit", edit_note_path(id: "1")
get "/hello/notes/1/trackbacks/new"
assert_equal "trackbacks#new", @response.body
assert_equal "/hello/notes/1/trackbacks/new", new_note_trackback_path(note_id: 1)
get "/hello/trackbacks/1"
assert_equal "trackbacks#show", @response.body
assert_equal "/hello/trackbacks/1", trackback_path(id: "1")
get "/hello/trackbacks/1/edit"
assert_equal "trackbacks#edit", @response.body
assert_equal "/hello/trackbacks/1/edit", edit_trackback_path(id: "1")
put "/hello/trackbacks/1"
assert_equal "trackbacks#update", @response.body
post "/hello/notes/1/trackbacks"
assert_equal "trackbacks#create", @response.body
delete "/hello/trackbacks/1"
assert_equal "trackbacks#destroy", @response.body
get "/hello/notes"
assert_equal "notes#index", @response.body
post "/hello/notes"
assert_equal "notes#create", @response.body
get "/hello/notes/new"
assert_equal "notes#new", @response.body
assert_equal "/hello/notes/new", new_note_path
get "/hello/notes/1"
assert_equal "notes#show", @response.body
assert_equal "/hello/notes/1", note_path(id: 1)
put "/hello/notes/1"
assert_equal "notes#update", @response.body
delete "/hello/notes/1"
assert_equal "notes#destroy", @response.body
end
def test_custom_resource_routes_are_scoped
draw do
resources :customers do
get :recent, on: :collection
get "profile", on: :member
get "secret/profile" => "customers#secret", :on => :member
post "preview" => "customers#preview", :as => :another_preview, :on => :new
resource :avatar do
get "thumbnail" => "avatars#thumbnail", :as => :thumbnail, :on => :member
end
resources :invoices do
get "outstanding" => "invoices#outstanding", :on => :collection
get "overdue", action: :overdue, on: :collection
get "print" => "invoices#print", :as => :print, :on => :member
post "preview" => "invoices#preview", :as => :preview, :on => :new
end
resources :notes, shallow: true do
get "preview" => "notes#preview", :as => :preview, :on => :new
get "print" => "notes#print", :as => :print, :on => :member
end
end
namespace :api do
resources :customers do
get "recent" => "customers#recent", :as => :recent, :on => :collection
get "profile" => "customers#profile", :as => :profile, :on => :member
post "preview" => "customers#preview", :as => :preview, :on => :new
end
end
end
assert_equal "/customers/recent", recent_customers_path
assert_equal "/customers/1/profile", profile_customer_path(id: "1")
assert_equal "/customers/1/secret/profile", secret_profile_customer_path(id: "1")
assert_equal "/customers/new/preview", another_preview_new_customer_path
assert_equal "/customers/1/avatar/thumbnail.jpg", thumbnail_customer_avatar_path(customer_id: "1", format: :jpg)
assert_equal "/customers/1/invoices/outstanding", outstanding_customer_invoices_path(customer_id: "1")
assert_equal "/customers/1/invoices/2/print", print_customer_invoice_path(customer_id: "1", id: "2")
assert_equal "/customers/1/invoices/new/preview", preview_new_customer_invoice_path(customer_id: "1")
assert_equal "/customers/1/notes/new/preview", preview_new_customer_note_path(customer_id: "1")
assert_equal "/notes/1/print", print_note_path(id: "1")
assert_equal "/api/customers/recent", recent_api_customers_path
assert_equal "/api/customers/1/profile", profile_api_customer_path(id: "1")
assert_equal "/api/customers/new/preview", preview_new_api_customer_path
get "/customers/1/invoices/overdue"
assert_equal "invoices#overdue", @response.body
get "/customers/1/secret/profile"
assert_equal "customers#secret", @response.body
end
def test_shallow_nested_routes_ignore_module
draw do
scope module: :api do
resources :errors, shallow: true do
resources :notices
end
end
end
get "/errors/1/notices"
assert_equal "api/notices#index", @response.body
assert_equal "/errors/1/notices", error_notices_path(error_id: "1")
get "/notices/1"
assert_equal "api/notices#show", @response.body
assert_equal "/notices/1", notice_path(id: "1")
end
def test_non_greedy_regexp
draw do
namespace :api do
scope(":version", version: /.+/) do
resources :users, id: /.+?/, format: /json|xml/
end
end
end
get "/api/1.0/users"
assert_equal "api/users#index", @response.body
assert_equal "/api/1.0/users", api_users_path(version: "1.0")
get "/api/1.0/users.json"
assert_equal "api/users#index", @response.body
assert_equal true, @request.format.json?
assert_equal "/api/1.0/users.json", api_users_path(version: "1.0", format: :json)
get "/api/1.0/users/first.last"
assert_equal "api/users#show", @response.body
assert_equal "first.last", @request.params[:id]
assert_equal "/api/1.0/users/first.last", api_user_path(version: "1.0", id: "first.last")
get "/api/1.0/users/first.last.xml"
assert_equal "api/users#show", @response.body
assert_equal "first.last", @request.params[:id]
assert_equal true, @request.format.xml?
assert_equal "/api/1.0/users/first.last.xml", api_user_path(version: "1.0", id: "first.last", format: :xml)
end
def test_match_without_via
assert_raises(ArgumentError) do
draw do
match "/foo/bar", to: "files#show"
end
end
end
def test_match_with_empty_via
assert_raises(ArgumentError) do
draw do
match "/foo/bar", to: "files#show", via: []
end
end
end
def test_glob_parameter_accepts_regexp
draw do
get "/:locale/*file.:format", to: "files#show", file: /path\/to\/existing\/file/
end
get "/en/path/to/existing/file.html"
assert_equal 200, @response.status
end
def test_resources_controller_name_is_not_pluralized
draw do
resources :content
end
get "/content"
assert_equal "content#index", @response.body
end
def test_url_generator_for_optional_prefix_dynamic_segment
draw do
get "(/:username)/followers" => "followers#index"
end
get "/bob/followers"
assert_equal "followers#index", @response.body
assert_equal "http://www.example.com/bob/followers",
url_for(controller: "followers", action: "index", username: "bob")
get "/followers"
assert_equal "followers#index", @response.body
assert_equal "http://www.example.com/followers",
url_for(controller: "followers", action: "index", username: nil)
end
def test_url_generator_for_optional_suffix_static_and_dynamic_segment
draw do
get "/groups(/user/:username)" => "groups#index"
end
get "/groups/user/bob"
assert_equal "groups#index", @response.body
assert_equal "http://www.example.com/groups/user/bob",
url_for(controller: "groups", action: "index", username: "bob")
get "/groups"
assert_equal "groups#index", @response.body
assert_equal "http://www.example.com/groups",
url_for(controller: "groups", action: "index", username: nil)
end
def test_url_generator_for_optional_prefix_static_and_dynamic_segment
draw do
get "(/user/:username)/photos" => "photos#index"
end
get "/user/bob/photos"
assert_equal "photos#index", @response.body
assert_equal "http://www.example.com/user/bob/photos",
url_for(controller: "photos", action: "index", username: "bob")
get "/photos"
assert_equal "photos#index", @response.body
assert_equal "http://www.example.com/photos",
url_for(controller: "photos", action: "index", username: nil)
end
def test_url_recognition_for_optional_static_segments
draw do
scope "(groups)" do
scope "(discussions)" do
resources :messages
end
end
end
get "/groups/discussions/messages"
assert_equal "messages#index", @response.body
get "/groups/discussions/messages/1"
assert_equal "messages#show", @response.body
get "/groups/messages"
assert_equal "messages#index", @response.body
get "/groups/messages/1"
assert_equal "messages#show", @response.body
get "/discussions/messages"
assert_equal "messages#index", @response.body
get "/discussions/messages/1"
assert_equal "messages#show", @response.body
get "/messages"
assert_equal "messages#index", @response.body
get "/messages/1"
assert_equal "messages#show", @response.body
end
def test_router_removes_invalid_conditions
draw do
scope constraints: { id: /\d+/ } do
get "/tickets", to: "tickets#index", as: :tickets
end
end
get "/tickets"
assert_equal "tickets#index", @response.body
assert_equal "/tickets", tickets_path
end
def test_constraints_are_merged_from_scope
draw do
scope constraints: { id: /\d{4}/ } do
resources :movies do
resources :reviews
resource :trailer
end
end
end
get "/movies/0001"
assert_equal "movies#show", @response.body
assert_equal "/movies/0001", movie_path(id: "0001")
get "/movies/00001"
assert_equal "Not Found", @response.body
assert_raises(ActionController::UrlGenerationError) { movie_path(id: "00001") }
get "/movies/0001/reviews"
assert_equal "reviews#index", @response.body
assert_equal "/movies/0001/reviews", movie_reviews_path(movie_id: "0001")
get "/movies/00001/reviews"
assert_equal "Not Found", @response.body
assert_raises(ActionController::UrlGenerationError) { movie_reviews_path(movie_id: "00001") }
get "/movies/0001/reviews/0001"
assert_equal "reviews#show", @response.body
assert_equal "/movies/0001/reviews/0001", movie_review_path(movie_id: "0001", id: "0001")
get "/movies/00001/reviews/0001"
assert_equal "Not Found", @response.body
assert_raises(ActionController::UrlGenerationError) { movie_path(movie_id: "00001", id: "00001") }
get "/movies/0001/trailer"
assert_equal "trailers#show", @response.body
assert_equal "/movies/0001/trailer", movie_trailer_path(movie_id: "0001")
get "/movies/00001/trailer"
assert_equal "Not Found", @response.body
assert_raises(ActionController::UrlGenerationError) { movie_trailer_path(movie_id: "00001") }
end
def test_only_should_be_read_from_scope
draw do
scope only: [:index, :show] do
namespace :only do
resources :clubs do
resources :players
resource :chairman
end
end
end
end
get "/only/clubs"
assert_equal "only/clubs#index", @response.body
assert_equal "/only/clubs", only_clubs_path
get "/only/clubs/1/edit"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { edit_only_club_path(id: "1") }
get "/only/clubs/1/players"
assert_equal "only/players#index", @response.body
assert_equal "/only/clubs/1/players", only_club_players_path(club_id: "1")
get "/only/clubs/1/players/2/edit"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { edit_only_club_player_path(club_id: "1", id: "2") }
get "/only/clubs/1/chairman"
assert_equal "only/chairmen#show", @response.body
assert_equal "/only/clubs/1/chairman", only_club_chairman_path(club_id: "1")
get "/only/clubs/1/chairman/edit"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { edit_only_club_chairman_path(club_id: "1") }
end
def test_except_should_be_read_from_scope
draw do
scope except: [:new, :create, :edit, :update, :destroy] do
namespace :except do
resources :clubs do
resources :players
resource :chairman
end
end
end
end
get "/except/clubs"
assert_equal "except/clubs#index", @response.body
assert_equal "/except/clubs", except_clubs_path
get "/except/clubs/1/edit"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { edit_except_club_path(id: "1") }
get "/except/clubs/1/players"
assert_equal "except/players#index", @response.body
assert_equal "/except/clubs/1/players", except_club_players_path(club_id: "1")
get "/except/clubs/1/players/2/edit"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { edit_except_club_player_path(club_id: "1", id: "2") }
get "/except/clubs/1/chairman"
assert_equal "except/chairmen#show", @response.body
assert_equal "/except/clubs/1/chairman", except_club_chairman_path(club_id: "1")
get "/except/clubs/1/chairman/edit"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { edit_except_club_chairman_path(club_id: "1") }
end
def test_only_option_should_override_scope
draw do
scope only: :show do
namespace :only do
resources :sectors, only: :index
end
end
end
get "/only/sectors"
assert_equal "only/sectors#index", @response.body
assert_equal "/only/sectors", only_sectors_path
get "/only/sectors/1"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { only_sector_path(id: "1") }
end
def test_only_option_should_not_inherit
draw do
scope only: :show do
namespace :only do
resources :sectors, only: :index do
resources :companies
resource :leader
end
end
end
end
get "/only/sectors/1/companies/2"
assert_equal "only/companies#show", @response.body
assert_equal "/only/sectors/1/companies/2", only_sector_company_path(sector_id: "1", id: "2")
get "/only/sectors/1/leader"
assert_equal "only/leaders#show", @response.body
assert_equal "/only/sectors/1/leader", only_sector_leader_path(sector_id: "1")
end
def test_except_option_should_override_scope
draw do
scope except: :index do
namespace :except do
resources :sectors, except: [:show, :update, :destroy]
end
end
end
get "/except/sectors"
assert_equal "except/sectors#index", @response.body
assert_equal "/except/sectors", except_sectors_path
get "/except/sectors/1"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { except_sector_path(id: "1") }
end
def test_except_option_should_not_inherit
draw do
scope except: :index do
namespace :except do
resources :sectors, except: [:show, :update, :destroy] do
resources :companies
resource :leader
end
end
end
end
get "/except/sectors/1/companies/2"
assert_equal "except/companies#show", @response.body
assert_equal "/except/sectors/1/companies/2", except_sector_company_path(sector_id: "1", id: "2")
get "/except/sectors/1/leader"
assert_equal "except/leaders#show", @response.body
assert_equal "/except/sectors/1/leader", except_sector_leader_path(sector_id: "1")
end
def test_except_option_should_override_scoped_only
draw do
scope only: :show do
namespace :only do
resources :sectors, only: :index do
resources :managers, except: [:show, :update, :destroy]
end
end
end
end
get "/only/sectors/1/managers"
assert_equal "only/managers#index", @response.body
assert_equal "/only/sectors/1/managers", only_sector_managers_path(sector_id: "1")
get "/only/sectors/1/managers/2"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { only_sector_manager_path(sector_id: "1", id: "2") }
end
def test_only_option_should_override_scoped_except
draw do
scope except: :index do
namespace :except do
resources :sectors, except: [:show, :update, :destroy] do
resources :managers, only: :index
end
end
end
end
get "/except/sectors/1/managers"
assert_equal "except/managers#index", @response.body
assert_equal "/except/sectors/1/managers", except_sector_managers_path(sector_id: "1")
get "/except/sectors/1/managers/2"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { except_sector_manager_path(sector_id: "1", id: "2") }
end
def test_only_scope_should_override_parent_scope
draw do
scope only: :show do
namespace :only do
resources :sectors, only: :index do
resources :companies do
scope only: :index do
resources :divisions
end
end
end
end
end
end
get "/only/sectors/1/companies/2/divisions"
assert_equal "only/divisions#index", @response.body
assert_equal "/only/sectors/1/companies/2/divisions", only_sector_company_divisions_path(sector_id: "1", company_id: "2")
get "/only/sectors/1/companies/2/divisions/3"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { only_sector_company_division_path(sector_id: "1", company_id: "2", id: "3") }
end
def test_except_scope_should_override_parent_scope
draw do
scope except: :index do
namespace :except do
resources :sectors, except: [:show, :update, :destroy] do
resources :companies do
scope except: [:show, :update, :destroy] do
resources :divisions
end
end
end
end
end
end
get "/except/sectors/1/companies/2/divisions"
assert_equal "except/divisions#index", @response.body
assert_equal "/except/sectors/1/companies/2/divisions", except_sector_company_divisions_path(sector_id: "1", company_id: "2")
get "/except/sectors/1/companies/2/divisions/3"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { except_sector_company_division_path(sector_id: "1", company_id: "2", id: "3") }
end
def test_except_scope_should_override_parent_only_scope
draw do
scope only: :show do
namespace :only do
resources :sectors, only: :index do
resources :companies do
scope except: [:show, :update, :destroy] do
resources :departments
end
end
end
end
end
end
get "/only/sectors/1/companies/2/departments"
assert_equal "only/departments#index", @response.body
assert_equal "/only/sectors/1/companies/2/departments", only_sector_company_departments_path(sector_id: "1", company_id: "2")
get "/only/sectors/1/companies/2/departments/3"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { only_sector_company_department_path(sector_id: "1", company_id: "2", id: "3") }
end
def test_only_scope_should_override_parent_except_scope
draw do
scope except: :index do
namespace :except do
resources :sectors, except: [:show, :update, :destroy] do
resources :companies do
scope only: :index do
resources :departments
end
end
end
end
end
end
get "/except/sectors/1/companies/2/departments"
assert_equal "except/departments#index", @response.body
assert_equal "/except/sectors/1/companies/2/departments", except_sector_company_departments_path(sector_id: "1", company_id: "2")
get "/except/sectors/1/companies/2/departments/3"
assert_equal "Not Found", @response.body
assert_raise(NoMethodError) { except_sector_company_department_path(sector_id: "1", company_id: "2", id: "3") }
end
def test_resources_are_not_pluralized
draw do
namespace :transport do
resources :taxis
end
end
get "/transport/taxis"
assert_equal "transport/taxis#index", @response.body
assert_equal "/transport/taxis", transport_taxis_path
get "/transport/taxis/new"
assert_equal "transport/taxis#new", @response.body
assert_equal "/transport/taxis/new", new_transport_taxi_path
post "/transport/taxis"
assert_equal "transport/taxis#create", @response.body
get "/transport/taxis/1"
assert_equal "transport/taxis#show", @response.body
assert_equal "/transport/taxis/1", transport_taxi_path(id: "1")
get "/transport/taxis/1/edit"
assert_equal "transport/taxis#edit", @response.body
assert_equal "/transport/taxis/1/edit", edit_transport_taxi_path(id: "1")
put "/transport/taxis/1"
assert_equal "transport/taxis#update", @response.body
delete "/transport/taxis/1"
assert_equal "transport/taxis#destroy", @response.body
end
def test_singleton_resources_are_not_singularized
draw do
namespace :medical do
resource :taxis
end
end
get "/medical/taxis/new"
assert_equal "medical/taxis#new", @response.body
assert_equal "/medical/taxis/new", new_medical_taxis_path
post "/medical/taxis"
assert_equal "medical/taxis#create", @response.body
get "/medical/taxis"
assert_equal "medical/taxis#show", @response.body
assert_equal "/medical/taxis", medical_taxis_path
get "/medical/taxis/edit"
assert_equal "medical/taxis#edit", @response.body
assert_equal "/medical/taxis/edit", edit_medical_taxis_path
put "/medical/taxis"
assert_equal "medical/taxis#update", @response.body
delete "/medical/taxis"
assert_equal "medical/taxis#destroy", @response.body
end
def test_greedy_resource_id_regexp_doesnt_match_edit_and_custom_action
draw do
resources :sections, id: /.+/ do
get :preview, on: :member
end
end
get "/sections/1/edit"
assert_equal "sections#edit", @response.body
assert_equal "/sections/1/edit", edit_section_path(id: "1")
get "/sections/1/preview"
assert_equal "sections#preview", @response.body
assert_equal "/sections/1/preview", preview_section_path(id: "1")
end
def test_resource_constraints_are_pushed_to_scope
draw do
namespace :wiki do
resources :articles, id: /[^\/]+/ do
resources :comments, only: [:create, :new]
end
end
end
get "/wiki/articles/Ruby_on_Rails_3.0"
assert_equal "wiki/articles#show", @response.body
assert_equal "/wiki/articles/Ruby_on_Rails_3.0", wiki_article_path(id: "Ruby_on_Rails_3.0")
get "/wiki/articles/Ruby_on_Rails_3.0/comments/new"
assert_equal "wiki/comments#new", @response.body
assert_equal "/wiki/articles/Ruby_on_Rails_3.0/comments/new", new_wiki_article_comment_path(article_id: "Ruby_on_Rails_3.0")
post "/wiki/articles/Ruby_on_Rails_3.0/comments"
assert_equal "wiki/comments#create", @response.body
assert_equal "/wiki/articles/Ruby_on_Rails_3.0/comments", wiki_article_comments_path(article_id: "Ruby_on_Rails_3.0")
end
def test_resources_path_can_be_a_symbol
draw do
resources :wiki_pages, path: :pages
resource :wiki_account, path: :my_account
end
get "/pages"
assert_equal "wiki_pages#index", @response.body
assert_equal "/pages", wiki_pages_path
get "/pages/Ruby_on_Rails"
assert_equal "wiki_pages#show", @response.body
assert_equal "/pages/Ruby_on_Rails", wiki_page_path(id: "Ruby_on_Rails")
get "/my_account"
assert_equal "wiki_accounts#show", @response.body
assert_equal "/my_account", wiki_account_path
end
def test_redirect_https
draw do
get "secure", to: redirect("/secure/login")
end
with_https do
get "/secure"
verify_redirect "https://www.example.com/secure/login"
end
end
def test_path_parameters_is_not_stale
draw do
scope "/countries/:country", constraints: lambda { |params, req| %w(all France).include?(params[:country]) } do
get "/", to: "countries#index"
get "/cities", to: "countries#cities"
end
get "/countries/:country/(*other)", to: redirect { |params, req| params[:other] ? "/countries/all/#{params[:other]}" : "/countries/all" }
end
get "/countries/France"
assert_equal "countries#index", @response.body
get "/countries/France/cities"
assert_equal "countries#cities", @response.body
get "/countries/UK"
verify_redirect "http://www.example.com/countries/all"
get "/countries/UK/cities"
verify_redirect "http://www.example.com/countries/all/cities"
end
def test_constraints_block_not_carried_to_following_routes
draw do
scope "/italians" do
get "/writers", to: "italians#writers", constraints: ::TestRoutingMapper::IpRestrictor
get "/sculptors", to: "italians#sculptors"
get "/painters/:painter", to: "italians#painters", constraints: { painter: /michelangelo/ }
end
end
get "/italians/writers"
assert_equal "Not Found", @response.body
get "/italians/sculptors"
assert_equal "italians#sculptors", @response.body
get "/italians/painters/botticelli"
assert_equal "Not Found", @response.body
get "/italians/painters/michelangelo"
assert_equal "italians#painters", @response.body
end
def test_custom_resource_actions_defined_using_string
draw do
resources :customers do
resources :invoices do
get "aged/:months", on: :collection, action: :aged, as: :aged
end
get "inactive", on: :collection
post "deactivate", on: :member
get "old", on: :collection, as: :stale
end
end
get "/customers/inactive"
assert_equal "customers#inactive", @response.body
assert_equal "/customers/inactive", inactive_customers_path
post "/customers/1/deactivate"
assert_equal "customers#deactivate", @response.body
assert_equal "/customers/1/deactivate", deactivate_customer_path(id: "1")
get "/customers/old"
assert_equal "customers#old", @response.body
assert_equal "/customers/old", stale_customers_path
get "/customers/1/invoices/aged/3"
assert_equal "invoices#aged", @response.body
assert_equal "/customers/1/invoices/aged/3", aged_customer_invoices_path(customer_id: "1", months: "3")
end
def test_route_defined_in_resources_scope_level
draw do
resources :customers do
get "export"
end
end
get "/customers/1/export"
assert_equal "customers#export", @response.body
assert_equal "/customers/1/export", customer_export_path(customer_id: "1")
end
def test_named_character_classes_in_regexp_constraints
draw do
get "/purchases/:token/:filename",
to: "purchases#fetch",
token: /[[:alnum:]]{10}/,
filename: /(.+)/,
as: :purchase
end
get "/purchases/315004be7e/Ruby_on_Rails_3.pdf"
assert_equal "purchases#fetch", @response.body
assert_equal "/purchases/315004be7e/Ruby_on_Rails_3.pdf", purchase_path(token: "315004be7e", filename: "Ruby_on_Rails_3.pdf")
end
def test_nested_resource_constraints
draw do
resources :lists, id: /([A-Za-z0-9]{25})|default/ do
resources :todos, id: /\d+/
end
end
get "/lists/01234012340123401234fffff"
assert_equal "lists#show", @response.body
assert_equal "/lists/01234012340123401234fffff", list_path(id: "01234012340123401234fffff")
get "/lists/01234012340123401234fffff/todos/1"
assert_equal "todos#show", @response.body
assert_equal "/lists/01234012340123401234fffff/todos/1", list_todo_path(list_id: "01234012340123401234fffff", id: "1")
get "/lists/2/todos/1"
assert_equal "Not Found", @response.body
assert_raises(ActionController::UrlGenerationError) { list_todo_path(list_id: "2", id: "1") }
end
def test_redirect_argument_error
routes = Class.new { include ActionDispatch::Routing::Redirection }.new
assert_raises(ArgumentError) { routes.redirect Object.new }
end
def test_named_route_check
before, after = nil
draw do
before = has_named_route?(:hello)
get "/hello", as: :hello, to: "hello#world"
after = has_named_route?(:hello)
end
assert_not before, "expected to not have named route :hello before route definition"
assert after, "expected to have named route :hello after route definition"
end
def test_explicitly_avoiding_the_named_route
draw do
scope as: "routes" do
get "/c/:id", as: :collision, to: "collision#show"
get "/collision", to: "collision#show"
get "/no_collision", to: "collision#show", as: nil
end
end
assert_not respond_to?(:routes_no_collision_path)
end
def test_controller_name_with_leading_slash_raise_error
assert_raise(ArgumentError) do
draw { get "/feeds/:service", to: "/feeds#show" }
end
assert_raise(ArgumentError) do
draw { get "/feeds/:service", controller: "/feeds", action: "show" }
end
assert_raise(ArgumentError) do
draw { get "/api/feeds/:service", to: "/api/feeds#show" }
end
assert_raise(ArgumentError) do
draw { resources :feeds, controller: "/feeds" }
end
end
def test_invalid_route_name_raises_error
assert_raise(ArgumentError) do
draw { get "/products", to: "products#index", as: "products " }
end
assert_raise(ArgumentError) do
draw { get "/products", to: "products#index", as: " products" }
end
assert_raise(ArgumentError) do
draw { get "/products", to: "products#index", as: "products!" }
end
assert_raise(ArgumentError) do
draw { get "/products", to: "products#index", as: "products index" }
end
assert_raise(ArgumentError) do
draw { get "/products", to: "products#index", as: "1products" }
end
end
def test_duplicate_route_name_raises_error
assert_raise(ArgumentError) do
draw do
get "/collision", to: "collision#show", as: "collision"
get "/duplicate", to: "duplicate#show", as: "collision"
end
end
end
def test_duplicate_route_name_via_resources_raises_error
assert_raise(ArgumentError) do
draw do
resources :collisions
get "/collision", to: "collision#show", as: "collision"
end
end
end
def test_nested_route_in_nested_resource
draw do
resources :posts, only: [:index, :show] do
resources :comments, except: :destroy do
get "views" => "comments#views", :as => :views
end
end
end
get "/posts/1/comments/2/views"
assert_equal "comments#views", @response.body
assert_equal "/posts/1/comments/2/views", post_comment_views_path(post_id: "1", comment_id: "2")
end
def test_root_in_deeply_nested_scope
draw do
resources :posts, only: [:index, :show] do
namespace :admin do
root to: "index#index"
end
end
end
get "/posts/1/admin"
assert_equal "admin/index#index", @response.body
assert_equal "/posts/1/admin", post_admin_root_path(post_id: "1")
end
def test_custom_param
draw do
resources :profiles, param: :username do
get :details, on: :member
resources :messages
end
end
get "/profiles/bob"
assert_equal "profiles#show", @response.body
assert_equal "bob", @request.params[:username]
get "/profiles/bob/details"
assert_equal "bob", @request.params[:username]
get "/profiles/bob/messages/34"
assert_equal "bob", @request.params[:profile_username]
assert_equal "34", @request.params[:id]
end
def test_custom_param_constraint
draw do
resources :profiles, param: :username, username: /[a-z]+/ do
get :details, on: :member
resources :messages
end
end
get "/profiles/bob1"
assert_equal 404, @response.status
get "/profiles/bob1/details"
assert_equal 404, @response.status
get "/profiles/bob1/messages/34"
assert_equal 404, @response.status
end
def test_shallow_custom_param
draw do
resources :orders do
constraints download: /[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}/ do
resources :downloads, param: :download, shallow: true
end
end
end
get "/downloads/0c0c0b68-d24b-11e1-a861-001ff3fffe6f.zip"
assert_equal "downloads#show", @response.body
assert_equal "0c0c0b68-d24b-11e1-a861-001ff3fffe6f", @request.params[:download]
end
def test_colon_containing_custom_param
ex = assert_raises(ArgumentError) {
draw do
resources :profiles, param: "username/:is_admin"
end
}
assert_match(/:param option can't contain colon/, ex.message)
end
def test_action_from_path_is_frozen
draw do
get "search" => "search"
end
get "/search"
assert_predicate @request.params[:action], :frozen?
end
def test_multiple_positional_args_with_the_same_name
draw do
get "/downloads/:id/:id.tar" => "downloads#show", as: :download, format: false
end
expected_params = {
controller: "downloads",
action: "show",
id: "1"
}
get "/downloads/1/1.tar"
assert_equal "downloads#show", @response.body
assert_equal expected_params, @request.path_parameters
assert_equal "/downloads/1/1.tar", download_path("1")
assert_equal "/downloads/1/1.tar", download_path("1", "1")
end
def test_absolute_controller_namespace
draw do
namespace :foo do
get "/", to: "/bar#index", as: "root"
end
end
get "/foo"
assert_equal "bar#index", @response.body
assert_equal "/foo", foo_root_path
end
def test_namespace_as_controller
draw do
namespace :foo do
get "/", to: "/bar#index", as: "root"
end
end
get "/foo"
assert_equal "bar#index", @response.body
assert_equal "/foo", foo_root_path
end
def test_trailing_slash
draw do
resources :streams
end
get "/streams"
assert_predicate @response, :ok?, "route without trailing slash should work"
get "/streams/"
assert_predicate @response, :ok?, "route with trailing slash should work"
get "/streams?foobar"
assert_predicate @response, :ok?, "route without trailing slash and with QUERY_STRING should work"
get "/streams/?foobar"
assert_predicate @response, :ok?, "route with trailing slash and with QUERY_STRING should work"
end
def test_route_with_dashes_in_path
draw do
get "/contact-us", to: "pages#contact_us"
end
get "/contact-us"
assert_equal "pages#contact_us", @response.body
assert_equal "/contact-us", contact_us_path
end
def test_shorthand_route_with_dashes_in_path
draw do
get "/about-us/index"
end
get "/about-us/index"
assert_equal "about_us#index", @response.body
assert_equal "/about-us/index", about_us_index_path
end
def test_resource_routes_with_dashes_in_path
draw do
resources :photos, only: [:show] do
get "user-favorites", on: :collection
get "preview-photo", on: :member
get "summary-text"
end
end
get "/photos/user-favorites"
assert_equal "photos#user_favorites", @response.body
assert_equal "/photos/user-favorites", user_favorites_photos_path
get "/photos/1/preview-photo"
assert_equal "photos#preview_photo", @response.body
assert_equal "/photos/1/preview-photo", preview_photo_photo_path("1")
get "/photos/1/summary-text"
assert_equal "photos#summary_text", @response.body
assert_equal "/photos/1/summary-text", photo_summary_text_path("1")
get "/photos/1"
assert_equal "photos#show", @response.body
assert_equal "/photos/1", photo_path("1")
end
def test_shallow_path_inside_namespace_is_not_added_twice
draw do
namespace :admin do
shallow do
resources :posts do
resources :comments
end
end
end
end
get "/admin/posts/1/comments"
assert_equal "admin/comments#index", @response.body
assert_equal "/admin/posts/1/comments", admin_post_comments_path("1")
end
def test_mix_string_to_controller_action
draw do
get "/projects", controller: "project_files",
action: "index",
to: "comments#index"
end
get "/projects"
assert_equal "comments#index", @response.body
end
def test_mix_string_to_controller
draw do
get "/projects", controller: "project_files",
to: "comments#index"
end
get "/projects"
assert_equal "comments#index", @response.body
end
def test_mix_string_to_action
draw do
get "/projects", action: "index",
to: "comments#index"
end
get "/projects"
assert_equal "comments#index", @response.body
end
def test_shallow_path_and_prefix_are_not_added_to_non_shallow_routes
draw do
scope shallow_path: "projects", shallow_prefix: "project" do
resources :projects do
resources :files, controller: "project_files", shallow: true
end
end
end
get "/projects"
assert_equal "projects#index", @response.body
assert_equal "/projects", projects_path
get "/projects/new"
assert_equal "projects#new", @response.body
assert_equal "/projects/new", new_project_path
post "/projects"
assert_equal "projects#create", @response.body
get "/projects/1"
assert_equal "projects#show", @response.body
assert_equal "/projects/1", project_path("1")
get "/projects/1/edit"
assert_equal "projects#edit", @response.body
assert_equal "/projects/1/edit", edit_project_path("1")
patch "/projects/1"
assert_equal "projects#update", @response.body
delete "/projects/1"
assert_equal "projects#destroy", @response.body
get "/projects/1/files"
assert_equal "project_files#index", @response.body
assert_equal "/projects/1/files", project_files_path("1")
get "/projects/1/files/new"
assert_equal "project_files#new", @response.body
assert_equal "/projects/1/files/new", new_project_file_path("1")
post "/projects/1/files"
assert_equal "project_files#create", @response.body
get "/projects/files/2"
assert_equal "project_files#show", @response.body
assert_equal "/projects/files/2", project_file_path("2")
get "/projects/files/2/edit"
assert_equal "project_files#edit", @response.body
assert_equal "/projects/files/2/edit", edit_project_file_path("2")
patch "/projects/files/2"
assert_equal "project_files#update", @response.body
delete "/projects/files/2"
assert_equal "project_files#destroy", @response.body
end
def test_scope_path_is_copied_to_shallow_path
draw do
scope path: "foo" do
resources :posts do
resources :comments, shallow: true
end
end
end
assert_equal "/foo/comments/1", comment_path("1")
end
def test_scope_as_is_copied_to_shallow_prefix
draw do
scope as: "foo" do
resources :posts do
resources :comments, shallow: true
end
end
end
assert_equal "/comments/1", foo_comment_path("1")
end
def test_scope_shallow_prefix_is_not_overwritten_by_as
draw do
scope as: "foo", shallow_prefix: "bar" do
resources :posts do
resources :comments, shallow: true
end
end
end
assert_equal "/comments/1", bar_comment_path("1")
end
def test_scope_shallow_path_is_not_overwritten_by_path
draw do
scope path: "foo", shallow_path: "bar" do
resources :posts do
resources :comments, shallow: true
end
end
end
assert_equal "/bar/comments/1", comment_path("1")
end
def test_resource_where_as_is_empty
draw do
resource :post, as: ""
scope "post", as: "post" do
resource :comment, as: ""
end
end
assert_equal "/post/new", new_path
assert_equal "/post/comment/new", new_post_path
end
def test_resources_where_as_is_empty
draw do
resources :posts, as: ""
scope "posts", as: "posts" do
resources :comments, as: ""
end
end
assert_equal "/posts/new", new_path
assert_equal "/posts/comments/new", new_posts_path
end
def test_scope_where_as_is_empty
draw do
scope "post", as: "" do
resource :user
resources :comments
end
end
assert_equal "/post/user/new", new_user_path
assert_equal "/post/comments/new", new_comment_path
end
def test_head_fetch_with_mount_on_root
draw do
get "/home" => "test#index"
mount lambda { |env| [200, {}, [env["REQUEST_METHOD"]]] }, at: "/"
end
# HEAD request should match `get /home` rather than the
# lower-precedence Rack app mounted at `/`.
head "/home"
assert_response :ok
assert_equal "test#index", @response.body
# But the Rack app can still respond to its own HEAD requests.
head "/foobar"
assert_response :ok
assert_equal "HEAD", @response.body
end
def test_passing_action_parameters_to_url_helpers_raises_error_if_parameters_are_not_permitted
draw do
root to: "projects#index"
end
params = ActionController::Parameters.new(id: "1")
assert_raises ActionController::UnfilteredParameters do
root_path(params)
end
end
def test_passing_action_parameters_to_url_helpers_is_allowed_if_parameters_are_permitted
draw do
root to: "projects#index"
end
params = ActionController::Parameters.new(id: "1")
params.permit!
assert_equal "/?id=1", root_path(params)
end
def test_dynamic_controller_segments_are_deprecated
assert_deprecated(ActionDispatch.deprecator) do
draw do
get "/:controller", action: "index"
end
end
end
def test_dynamic_action_segments_are_deprecated
assert_deprecated(ActionDispatch.deprecator) do
draw do
get "/pages/:action", controller: "pages"
end
end
end
def test_multiple_roots_raises_error
ex = assert_raises(ArgumentError) {
draw do
root "pages#index", constraints: { host: "www.example.com" }
root "admin/pages#index", constraints: { host: "admin.example.com" }
end
}
assert_match(/Invalid route name, already in use: 'root'/, ex.message)
end
def test_multiple_named_roots
draw do
namespace :foo do
root "pages#index", constraints: { host: "www.example.com" }
root "admin/pages#index", constraints: { host: "admin.example.com" }, as: :admin_root
end
root "pages#index", constraints: { host: "www.example.com" }
root "admin/pages#index", constraints: { host: "admin.example.com" }, as: :admin_root
end
get "http://www.example.com/foo"
assert_equal "foo/pages#index", @response.body
get "http://admin.example.com/foo"
assert_equal "foo/admin/pages#index", @response.body
get "http://www.example.com/"
assert_equal "pages#index", @response.body
get "http://admin.example.com/"
assert_equal "admin/pages#index", @response.body
end
def test_multiple_namespaced_roots
draw do
namespace :foo do
root "test#index"
end
root "test#index"
namespace :bar do
root "test#index"
end
end
assert_equal "/foo", foo_root_path
assert_equal "/", root_path
assert_equal "/bar", bar_root_path
end
def test_nested_routes_under_format_resource
draw do
resources :formats do
resources :items
end
end
get "/formats/1/items.json"
assert_equal 200, @response.status
assert_equal "items#index", @response.body
assert_equal "/formats/1/items.json", format_items_path(1, :json)
get "/formats/1/items/2.json"
assert_equal 200, @response.status
assert_equal "items#show", @response.body
assert_equal "/formats/1/items/2.json", format_item_path(1, 2, :json)
end
private
def draw(&block)
self.class.stub_controllers do |routes|
routes.default_url_options = { host: "www.example.com" }
routes.draw(&block)
@app = RoutedRackApp.new routes
end
end
def url_for(options = {})
@app.routes.url_helpers.url_for(options)
end
def method_missing(method, ...)
if method.match?(/_(path|url)$/)
@app.routes.url_helpers.send(method, ...)
else
super
end
end
def with_https
old_https = https?
https!
yield
ensure
https!(old_https)
end
def verify_redirect(url, status = 301)
assert_equal status, @response.status
assert_equal url, @response.headers["Location"]
assert_equal "", @response.body
end
end
class TestAltApp < ActionDispatch::IntegrationTest
class AltRequest < ActionDispatch::Request
attr_accessor :path_parameters, :path_info, :script_name
attr_reader :env
def initialize(env)
@path_parameters = {}
@env = env
@path_info = "/"
@script_name = ""
super
end
def request_method
"GET"
end
def ip
"127.0.0.1"
end
def x_header
@env["HTTP_X_HEADER"] || ""
end
end
class XHeader
def call(env)
[200, { "Content-Type" => "text/html" }, ["XHeader"]]
end
end
class AltApp
def call(env)
[200, { "Content-Type" => "text/html" }, ["Alternative App"]]
end
end
AltRoutes = Class.new(ActionDispatch::Routing::RouteSet) {
def request_class
AltRequest
end
}.new
AltRoutes.draw do
get "/" => TestAltApp::XHeader.new, :constraints => { x_header: /HEADER/ }
get "/" => TestAltApp::AltApp.new
end
APP = build_app AltRoutes
def app
APP
end
def test_alt_request_without_header
get "/"
assert_equal "Alternative App", @response.body
end
def test_alt_request_with_matched_header
get "/", headers: { "HTTP_X_HEADER" => "HEADER" }
assert_equal "XHeader", @response.body
end
def test_alt_request_with_unmatched_header
get "/", headers: { "HTTP_X_HEADER" => "NON_MATCH" }
assert_equal "Alternative App", @response.body
end
end
class TestAppendingRoutes < ActionDispatch::IntegrationTest
def simple_app(resp)
lambda { |e| [ 200, { "Content-Type" => "text/plain" }, [resp] ] }
end
def setup
super
s = self
routes = ActionDispatch::Routing::RouteSet.new
routes.append do
get "/hello" => s.simple_app("fail")
get "/goodbye" => s.simple_app("goodbye")
end
routes.draw do
get "/hello" => s.simple_app("hello")
end
@app = self.class.build_app routes
end
def test_goodbye_should_be_available
get "/goodbye"
assert_equal "goodbye", @response.body
end
def test_hello_should_not_be_overwritten
get "/hello"
assert_equal "hello", @response.body
end
def test_missing_routes_are_still_missing
get "/random"
assert_equal 404, @response.status
end
end
class TestNamespaceWithControllerOption < ActionDispatch::IntegrationTest
module ::Admin
class StorageFilesController < ActionController::Base
def index
render plain: "admin/storage_files#index"
end
end
end
def draw(&block)
routes = ActionDispatch::Routing::RouteSet.new
routes.draw(&block)
@app = self.class.build_app routes
@routes = routes
end
def test_missing_controller
ex = assert_raises(ArgumentError) {
draw do
get "/foo/bar", action: :index
end
}
assert_match(/Missing :controller/, ex.message)
end
def test_missing_controller_with_to
ex = assert_raises(ArgumentError) {
draw do
get "/foo/bar", to: "foo"
end
}
assert_match(/Missing :controller/, ex.message)
end
def test_implicit_controller_with_to
draw do
controller :foo do
get "/foo/bar", to: "bar"
end
end
assert_routing "/foo/bar", controller: "foo", action: "bar"
end
def test_to_is_a_symbol
ex = assert_raises(ArgumentError) {
draw do
get "/foo/bar", to: :foo
end
}
assert_match(/:to must respond to/, ex.message)
end
def test_missing_action_with_to
ex = assert_raises(ArgumentError) {
draw do
get "/foo/bar", to: "foo#"
end
}
assert_match(/Missing :action/, ex.message)
end
def test_valid_controller_options_inside_namespace
draw do
namespace :admin do
resources :storage_files, controller: "storage_files"
end
end
get "/admin/storage_files"
assert_equal "admin/storage_files#index", @response.body
end
def test_resources_with_valid_namespaced_controller_option
draw do
resources :storage_files, controller: "admin/storage_files"
end
get "/storage_files"
assert_equal "admin/storage_files#index", @response.body
end
def test_warn_with_ruby_constant_syntax_controller_option
e = assert_raise(ArgumentError) do
draw do
namespace :admin do
resources :storage_files, controller: "StorageFiles"
end
end
end
assert_match "'admin/StorageFiles' is not a supported controller name", e.message
end
def test_warn_with_ruby_constant_syntax_namespaced_controller_option
e = assert_raise(ArgumentError) do
draw do
resources :storage_files, controller: "Admin::StorageFiles"
end
end
assert_match "'Admin::StorageFiles' is not a supported controller name", e.message
end
def test_warn_with_ruby_constant_syntax_no_colons
e = assert_raise(ArgumentError) do
draw do
resources :storage_files, controller: "Admin"
end
end
assert_match "'Admin' is not a supported controller name", e.message
end
end
class TestDefaultScope < ActionDispatch::IntegrationTest
module ::Blog
class PostsController < ActionController::Base
def index
render plain: "blog/posts#index"
end
end
end
DefaultScopeRoutes = ActionDispatch::Routing::RouteSet.new
DefaultScopeRoutes.default_scope = { module: :blog }
DefaultScopeRoutes.draw do
resources :posts
end
APP = build_app DefaultScopeRoutes
def app
APP
end
include DefaultScopeRoutes.url_helpers
def test_default_scope
get "/posts"
assert_equal "blog/posts#index", @response.body
end
end
class TestHttpMethods < ActionDispatch::IntegrationTest
RFC2616 = %w(OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT)
RFC2518 = %w(PROPFIND PROPPATCH MKCOL COPY MOVE LOCK UNLOCK)
RFC3253 = %w(VERSION-CONTROL REPORT CHECKOUT CHECKIN UNCHECKOUT MKWORKSPACE UPDATE LABEL MERGE BASELINE-CONTROL MKACTIVITY)
RFC3648 = %w(ORDERPATCH)
RFC3744 = %w(ACL)
RFC5323 = %w(SEARCH)
RFC4791 = %w(MKCALENDAR)
RFC5789 = %w(PATCH)
def simple_app(response)
lambda { |env| [ 200, { "Content-Type" => "text/plain" }, [response] ] }
end
attr_reader :app
def setup
s = self
routes = ActionDispatch::Routing::RouteSet.new
@app = RoutedRackApp.new routes
routes.draw do
(RFC2616 + RFC2518 + RFC3253 + RFC3648 + RFC3744 + RFC5323 + RFC4791 + RFC5789).each do |method|
match "/" => s.simple_app(method), :via => method.underscore.to_sym
end
end
end
(RFC2616 + RFC2518 + RFC3253 + RFC3648 + RFC3744 + RFC5323 + RFC4791 + RFC5789).each do |method|
test "request method #{method.underscore} can be matched" do
get "/", headers: { "REQUEST_METHOD" => method }
assert_equal method, @response.body
end
end
end
class TestUriPathEscaping < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
get "/:segment" => lambda { |env|
path_params = env["action_dispatch.request.path_parameters"]
[200, { "Content-Type" => "text/plain" }, [path_params[:segment]]]
}, :as => :segment
get "/*splat" => lambda { |env|
path_params = env["action_dispatch.request.path_parameters"]
[200, { "Content-Type" => "text/plain" }, [path_params[:splat]]]
}, :as => :splat
end
end
include Routes.url_helpers
APP = build_app Routes
def app; APP end
test "escapes slash in generated path segment" do
assert_equal "/a%20b%2Fc+d", segment_path(segment: "a b/c+d")
end
test "unescapes recognized path segment" do
get "/a%20b%2Fc+d"
assert_equal "a b/c+d", @response.body
end
test "does not escape slash in generated path splat" do
assert_equal "/a%20b/c+d", splat_path(splat: "a b/c+d")
end
test "unescapes recognized path splat" do
get "/a%20b/c+d"
assert_equal "a b/c+d", @response.body
end
end
class TestUnicodePaths < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
get "/ほげ" => lambda { |env|
[200, { "Content-Type" => "text/plain" }, []]
}, :as => :unicode_path
end
end
include Routes.url_helpers
APP = build_app Routes
def app; APP end
test "recognizes unicode path" do
get "/#{Rack::Utils.escape("ほげ")}"
assert_equal "200", @response.code
end
end
class TestMultipleNestedController < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
namespace :foo do
namespace :bar do
get "baz" => "baz#index"
end
end
get "pooh" => "pooh#index"
end
end
module ::Foo
module Bar
class BazController < ActionController::Base
include Routes.url_helpers
def index
render inline: "<%= url_for :controller => '/pooh', :action => 'index' %>"
end
end
end
end
APP = build_app Routes
def app; APP end
test "controller option which starts with '/' from multiple nested controller" do
get "/foo/bar/baz"
assert_equal "/pooh", @response.body
end
end
class TestTildeAndMinusPaths < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/~user" => ok
get "/young-and-fine" => ok
end
end
include Routes.url_helpers
APP = build_app Routes
def app; APP end
test "recognizes tilde path" do
get "/~user"
assert_equal "200", @response.code
end
test "recognizes minus path" do
get "/young-and-fine"
assert_equal "200", @response.code
end
end
class TestRedirectInterpolation < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/foo/:id" => redirect("/foo/bar/%{id}")
get "/bar/:id" => redirect(path: "/foo/bar/%{id}")
get "/baz/:id" => redirect("/baz?id=%{id}&foo=?&bar=1#id-%{id}")
get "/foo/bar/:id" => ok
get "/baz" => ok
end
end
APP = build_app Routes
def app; APP end
test "redirect escapes interpolated parameters with redirect proc" do
get "/foo/1%3E"
verify_redirect "http://www.example.com/foo/bar/1%3E"
end
test "redirect escapes interpolated parameters with option proc" do
get "/bar/1%3E"
verify_redirect "http://www.example.com/foo/bar/1%3E"
end
test "path redirect escapes interpolated parameters correctly" do
get "/foo/1%201"
verify_redirect "http://www.example.com/foo/bar/1%201"
get "/baz/1%201"
verify_redirect "http://www.example.com/baz?id=1+1&foo=?&bar=1#id-1%201"
end
private
def verify_redirect(url, status = 301)
assert_equal status, @response.status
assert_equal url, @response.headers["Location"]
assert_equal "", @response.body
end
end
class TestConstraintsAccessingParameters < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/:foo" => ok, :constraints => lambda { |r| r.params[:foo] == "foo" }
get "/:bar" => ok
end
end
APP = build_app Routes
def app; APP end
test "parameters are reset between constraint checks" do
get "/bar"
assert_nil @request.params[:foo]
assert_equal "bar", @request.params[:bar]
end
end
class TestGlobRoutingMapper < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/*id" => redirect("/not_cars"), :constraints => { id: /dummy/ }
get "/cars" => ok
end
end
# include Routes.url_helpers
APP = build_app Routes
def app; APP end
def test_glob_constraint
get "/dummy"
assert_equal "301", @response.code
assert_equal "/not_cars", @response.header["Location"].match("/[^/]+$")[0]
end
def test_glob_constraint_skip_route
get "/cars"
assert_equal "200", @response.code
end
def test_glob_constraint_skip_all
get "/missing"
assert_equal "404", @response.code
end
end
class TestOptimizedNamedRoutes < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/foo" => ok, as: :foo
ActionDispatch.deprecator.silence do
get "/post(/:action(/:id))" => ok, as: :posts
end
get "/:foo/:foo_type/bars/:id" => ok, as: :bar
get "/projects/:id.:format" => ok, as: :project
get "/pages/:id" => ok, as: :page
get "/wiki/*page" => ok, as: :wiki
end
end
include Routes.url_helpers
APP = build_app Routes
def app; APP end
test "enabled when not mounted and default_url_options is empty" do
assert_predicate Routes.url_helpers, :optimize_routes_generation?
end
test "named route called as singleton method" do
assert_equal "/foo", Routes.url_helpers.foo_path
end
test "named route called on included module" do
assert_equal "/foo", foo_path
end
test "nested optional segments are removed" do
assert_equal "/post", Routes.url_helpers.posts_path
assert_equal "/post", posts_path
end
test "segments with same prefix are replaced correctly" do
assert_equal "/foo/baz/bars/1", Routes.url_helpers.bar_path("foo", "baz", "1")
assert_equal "/foo/baz/bars/1", bar_path("foo", "baz", "1")
end
test "segments separated with a period are replaced correctly" do
assert_equal "/projects/1.json", Routes.url_helpers.project_path(1, :json)
assert_equal "/projects/1.json", project_path(1, :json)
end
test "segments with question marks are escaped" do
assert_equal "/pages/foo%3Fbar", Routes.url_helpers.page_path("foo?bar")
assert_equal "/pages/foo%3Fbar", page_path("foo?bar")
end
test "segments with slashes are escaped" do
assert_equal "/pages/foo%2Fbar", Routes.url_helpers.page_path("foo/bar")
assert_equal "/pages/foo%2Fbar", page_path("foo/bar")
end
test "glob segments with question marks are escaped" do
assert_equal "/wiki/foo%3Fbar", Routes.url_helpers.wiki_path("foo?bar")
assert_equal "/wiki/foo%3Fbar", wiki_path("foo?bar")
end
test "glob segments with slashes are not escaped" do
assert_equal "/wiki/foo/bar", Routes.url_helpers.wiki_path("foo/bar")
assert_equal "/wiki/foo/bar", wiki_path("foo/bar")
end
end
class TestNamedRouteUrlHelpers < ActionDispatch::IntegrationTest
class CategoriesController < ActionController::Base
def show
render plain: "categories#show"
end
end
class ProductsController < ActionController::Base
def show
render plain: "products#show"
end
end
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
scope module: "test_named_route_url_helpers" do
get "/categories/:id" => "categories#show", :as => :category
get "/products/:id" => "products#show", :as => :product
end
end
end
APP = build_app Routes
def app; APP end
include Routes.url_helpers
test "URL helpers do not ignore nil parameters when using non-optimized routes" do
Routes.stub :optimize_routes_generation?, false do
get "/categories/1"
assert_response :success
assert_raises(ActionController::UrlGenerationError) { product_path(nil) }
end
end
end
class TestUrlConstraints < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
constraints subdomain: "admin" do
get "/" => ok, :as => :admin_root
end
scope constraints: { protocol: "https://" } do
get "/" => ok, :as => :secure_root
end
get "/" => ok, :as => :alternate_root, :constraints => { port: 8080 }
get "/search" => ok, :constraints => { subdomain: false }
get "/logs" => ok, :constraints => { subdomain: true }
end
end
include Routes.url_helpers
APP = build_app Routes
def app; APP end
test "constraints are copied to defaults when using constraints method" do
assert_equal "http://admin.example.com/", admin_root_url
get "http://admin.example.com/"
assert_response :success
end
test "constraints are copied to defaults when using scope constraints hash" do
assert_equal "https://www.example.com/", secure_root_url
get "https://www.example.com/"
assert_response :success
end
test "constraints are copied to defaults when using route constraints hash" do
assert_equal "http://www.example.com:8080/", alternate_root_url
get "http://www.example.com:8080/"
assert_response :success
end
test "false constraint expressions check for absence of values" do
get "http://example.com/search"
assert_response :success
assert_equal "http://example.com/search", search_url
get "http://api.example.com/search"
assert_response :not_found
end
test "true constraint expressions check for presence of values" do
get "http://api.example.com/logs"
assert_response :success
assert_equal "http://api.example.com/logs", logs_url
get "http://example.com/logs"
assert_response :not_found
end
end
class TestInvalidUrls < ActionDispatch::IntegrationTest
class FooController < ActionController::Base
param_encoding :show, :id, Encoding::ASCII_8BIT
def show
render plain: "foo#show"
end
end
test "invalid UTF-8 encoding returns a bad request" do
with_routing do |set|
set.draw do
get "/bar/:id", to: redirect("/foo/show/%{id}")
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/foobar/:id", to: ok
ActionDispatch.deprecator.silence do
get "/:controller(/:action(/:id))"
end
end
get "/%E2%EF%BF%BD%A6"
assert_response :bad_request
get "/foo/%E2%EF%BF%BD%A6"
assert_response :bad_request
get "/bar/%E2%EF%BF%BD%A6"
assert_response :bad_request
get "/foobar/%E2%EF%BF%BD%A6"
assert_response :bad_request
end
end
test "params param_encoding uses ASCII 8bit" do
with_routing do |set|
set.draw do
get "/foo/show(/:id)", to: "test_invalid_urls/foo#show"
get "/bar/show(/:id)", controller: "test_invalid_urls/foo", action: "show"
end
get "/foo/show/%E2%EF%BF%BD%A6"
assert_response :ok
get "/bar/show/%E2%EF%BF%BD%A6"
assert_response :ok
end
end
test "does not encode params besides id" do
with_routing do |set|
set.draw do
get "/foo/show(/:id)", to: "test_invalid_urls/foo#show"
get "/bar/show(/:id)", controller: "test_invalid_urls/foo", action: "show"
end
get "/foo/show/%E2%EF%BF%BD%A6?something_else=%E2%EF%BF%BD%A6"
assert_response :bad_request
get "/foo/show/%E2%EF%BF%BD%A6?something_else=%E2%EF%BF%BD%A6"
assert_response :bad_request
end
end
end
class TestOptionalRootSegments < ActionDispatch::IntegrationTest
stub_controllers do |routes|
Routes = routes
Routes.draw do
get "/(page/:page)", to: "pages#index", as: :root
end
end
APP = build_app Routes
def app
APP
end
include Routes.url_helpers
def test_optional_root_segments
get "/"
assert_equal "pages#index", @response.body
assert_equal "/", root_path
get "/page/1"
assert_equal "pages#index", @response.body
assert_equal "1", @request.params[:page]
assert_equal "/page/1", root_path("1")
assert_equal "/page/1", root_path(page: "1")
end
end
class TestPortConstraints < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/integer", to: ok, constraints: { port: 8080 }
get "/string", to: ok, constraints: { port: "8080" }
get "/array/:idx", to: ok, constraints: { port: [8080], idx: %w[first last] }
get "/regexp", to: ok, constraints: { port: /8080/ }
end
end
include Routes.url_helpers
APP = build_app Routes
def app; APP end
def test_integer_port_constraints
get "http://www.example.com/integer"
assert_response :not_found
get "http://www.example.com:8080/integer"
assert_response :success
end
def test_string_port_constraints
get "http://www.example.com/string"
assert_response :not_found
get "http://www.example.com:8080/string"
assert_response :success
end
def test_array_port_constraints
get "http://www.example.com/array"
assert_response :not_found
get "http://www.example.com:8080/array/middle"
assert_response :not_found
get "http://www.example.com:8080/array/first"
assert_response :success
end
def test_regexp_port_constraints
get "http://www.example.com/regexp"
assert_response :not_found
get "http://www.example.com:8080/regexp"
assert_response :success
end
end
class TestFormatConstraints < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/string", to: ok, constraints: { format: "json" }
get "/regexp", to: ok, constraints: { format: /json/ }
get "/json_only", to: ok, format: true, constraints: { format: /json/ }
get "/xml_only", to: ok, format: "xml"
end
end
include Routes.url_helpers
APP = build_app Routes
def app; APP end
def test_string_format_constraints
get "http://www.example.com/string"
assert_response :success
get "http://www.example.com/string.json"
assert_response :success
get "http://www.example.com/string.html"
assert_response :not_found
end
def test_regexp_format_constraints
get "http://www.example.com/regexp"
assert_response :success
get "http://www.example.com/regexp.json"
assert_response :success
get "http://www.example.com/regexp.html"
assert_response :not_found
end
def test_enforce_with_format_true_with_constraint
get "http://www.example.com/json_only.json"
assert_response :success
get "http://www.example.com/json_only.html"
assert_response :not_found
get "http://www.example.com/json_only"
assert_response :not_found
end
def test_enforce_with_string
get "http://www.example.com/xml_only.xml"
assert_response :success
get "http://www.example.com/xml_only"
assert_response :success
get "http://www.example.com/xml_only.json"
assert_response :not_found
end
end
class TestCallableConstraintValidation < ActionDispatch::IntegrationTest
def test_constraint_with_object_not_callable
assert_raises(ArgumentError) do
ActionDispatch::Routing::RouteSet.new.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/test", to: ok, constraints: Object.new
end
end
end
end
class TestRouteDefaults < ActionDispatch::IntegrationTest
stub_controllers do |routes|
Routes = routes
Routes.draw do
resources :posts, bucket_type: "post"
resources :projects, defaults: { bucket_type: "project" }
end
end
APP = build_app Routes
def app
APP
end
include Routes.url_helpers
def test_route_options_are_required_for_url_for
assert_raises(ActionController::UrlGenerationError) do
url_for(controller: "posts", action: "show", id: 1, only_path: true)
end
assert_equal "/posts/1", url_for(controller: "posts", action: "show", id: 1, bucket_type: "post", only_path: true)
end
def test_route_defaults_are_not_required_for_url_for
assert_equal "/projects/1", url_for(controller: "projects", action: "show", id: 1, only_path: true)
end
end
class TestRackAppRouteGeneration < ActionDispatch::IntegrationTest
stub_controllers do |routes|
Routes = routes
Routes.draw do
rack_app = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
mount rack_app, at: "/account", as: "account"
mount rack_app, at: "/:locale/account", as: "localized_account"
end
end
APP = build_app Routes
def app
APP
end
include Routes.url_helpers
def test_mounted_application_doesnt_match_unnamed_route
assert_raise(ActionController::UrlGenerationError) do
assert_equal "/account?controller=products", url_for(controller: "products", action: "index", only_path: true)
end
assert_raise(ActionController::UrlGenerationError) do
assert_equal "/de/account?controller=products", url_for(controller: "products", action: "index", locale: "de", only_path: true)
end
end
end
class TestRedirectRouteGeneration < ActionDispatch::IntegrationTest
stub_controllers do |routes|
Routes = routes
Routes.draw do
get "/account", to: redirect("/myaccount"), as: "account"
get "/:locale/account", to: redirect("/%{locale}/myaccount"), as: "localized_account"
end
end
APP = build_app Routes
def app
APP
end
include Routes.url_helpers
def test_redirect_doesnt_match_unnamed_route
assert_raise(ActionController::UrlGenerationError) do
assert_equal "/account?controller=products", url_for(controller: "products", action: "index", only_path: true)
end
assert_raise(ActionController::UrlGenerationError) do
assert_equal "/de/account?controller=products", url_for(controller: "products", action: "index", locale: "de", only_path: true)
end
end
end
class TestUrlGenerationErrors < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
get "/products/:id" => "products#show", :as => :product
end
end
APP = build_app Routes
def app; APP end
include Routes.url_helpers
test "URL helpers raise a 'missing keys' error for a nil param with optimized helpers" do
url, missing = { action: "show", controller: "products", id: nil }, [:id]
message = "No route matches #{url.inspect}, missing required keys: #{missing.inspect}"
error = assert_raises(ActionController::UrlGenerationError) { product_path(nil) }
assert_equal message, error.message
end
test "URL helpers raise a 'constraint failure' error for a nil param with non-optimized helpers" do
url, missing = { action: "show", controller: "products", id: nil }, [:id]
message = "No route matches #{url.inspect}, possible unmatched constraints: #{missing.inspect}"
error = assert_raises(ActionController::UrlGenerationError, message) { product_path(id: nil) }
assert_match message, error.message
end
test "URL helpers raise message with mixed parameters when generation fails" do
url, missing = { action: "show", controller: "products", id: nil, "id" => "url-tested" }, [:id]
message = "No route matches #{url.inspect}, possible unmatched constraints: #{missing.inspect}"
# Optimized URL helper
error = assert_raises(ActionController::UrlGenerationError) { product_path(nil, "id" => "url-tested") }
assert_match message, error.message
# Non-optimized URL helper
error = assert_raises(ActionController::UrlGenerationError, message) { product_path(id: nil, "id" => "url-tested") }
assert_match message, error.message
end
test "exceptions have suggestions for fix" do
error = assert_raises(ActionController::UrlGenerationError) { product_path(nil, "id" => "url-tested") }
if error.respond_to?(:detailed_message)
assert_match "Did you mean?", error.detailed_message
else
assert_match "Did you mean?", error.message
end
end
# FIXME: we should fix all locations that raise this exception to provide
# the info DidYouMean needs and then delete this test. Just adding the
# test for now because some parameters to the constructor are optional, and
# we don't want to break other code.
test "correct for empty UrlGenerationError" do
err = ActionController::UrlGenerationError.new("oh no!")
assert_equal [], err.corrections
end
end
class TestDefaultUrlOptions < ActionDispatch::IntegrationTest
class PostsController < ActionController::Base
def archive
render plain: "posts#archive"
end
end
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
default_url_options locale: "en"
scope ":locale", format: false do
get "/posts/:year/:month/:day", to: "posts#archive", as: "archived_posts"
end
end
APP = build_app Routes
def app
APP
end
include Routes.url_helpers
def test_positional_args_with_format_false
assert_equal "/en/posts/2014/12/13", archived_posts_path(2014, 12, 13)
end
end
class TestErrorsInController < ActionDispatch::IntegrationTest
class ::PostsController < ActionController::Base
def foo
nil.i_do_not_exist
end
def bar
NonExistingClass.new
end
end
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
ActionDispatch.deprecator.silence do
get "/:controller(/:action)"
end
end
APP = build_app Routes
def app
APP
end
def test_legit_no_method_errors_are_not_caught
get "/posts/foo"
assert_equal 500, response.status
end
def test_legit_name_errors_are_not_caught
get "/posts/bar"
assert_equal 500, response.status
end
def test_legit_routing_not_found_responses
get "/posts/baz"
assert_equal 404, response.status
get "/i_do_not_exist"
assert_equal 404, response.status
end
end
class TestPartialDynamicPathSegments < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
ok = lambda { |env| [200, { "Content-Type" => "text/plain" }, []] }
get "/songs/song-:song", to: ok
get "/songs/:song-song", to: ok
get "/:artist/song-:song", to: ok
get "/:artist/:song-song", to: ok
get "/optional/songs(/song-:song)", to: ok
get "/optional/songs(/:song-song)", to: ok
get "/optional/:artist(/song-:song)", to: ok
get "/optional/:artist(/:song-song)", to: ok
end
APP = build_app Routes
def app
APP
end
def test_paths_with_partial_dynamic_segments_are_recognised
get "/david-bowie/changes-song"
assert_equal 200, response.status
assert_params artist: "david-bowie", song: "changes"
get "/david-bowie/song-changes"
assert_equal 200, response.status
assert_params artist: "david-bowie", song: "changes"
get "/songs/song-changes"
assert_equal 200, response.status
assert_params song: "changes"
get "/songs/changes-song"
assert_equal 200, response.status
assert_params song: "changes"
get "/optional/songs/song-changes"
assert_equal 200, response.status
assert_params song: "changes"
get "/optional/songs/changes-song"
assert_equal 200, response.status
assert_params song: "changes"
get "/optional/david-bowie/changes-song"
assert_equal 200, response.status
assert_params artist: "david-bowie", song: "changes"
get "/optional/david-bowie/song-changes"
assert_equal 200, response.status
assert_params artist: "david-bowie", song: "changes"
end
private
def assert_params(params)
assert_equal(params, request.path_parameters)
end
end
class TestOptionalScopesWithOrWithoutParams < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
scope module: "test_optional_scopes_with_or_without_params" do
scope "(:locale)", locale: /en|es/ do
get "home", controller: :home, action: :index
get "with_param/:foo", to: "home#with_param", as: "with_param"
get "without_param", to: "home#without_param"
end
end
end
end
class HomeController < ActionController::Base
include Routes.url_helpers
def index
render inline: "<%= with_param_path(foo: 'bar') %> | <%= without_param_path %>"
end
def with_param; end
def without_param; end
end
APP = build_app Routes
def app
APP
end
def test_stays_unscoped_with_or_without_params
get "/home"
assert_equal "/with_param/bar | /without_param", response.body
end
def test_preserves_scope_with_or_without_params
get "/es/home"
assert_equal "/es/with_param/bar | /es/without_param", response.body
end
end
class TestPathParameters < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
scope module: "test_path_parameters" do
scope ":locale", locale: /en|ar/ do
root to: "home#index"
get "/about", to: "pages#about"
end
end
ActionDispatch.deprecator.silence do
get ":controller(/:action/(:id))"
end
end
end
class HomeController < ActionController::Base
include Routes.url_helpers
def index
render inline: "<%= root_path %>"
end
end
class PagesController < ActionController::Base
include Routes.url_helpers
def about
render inline: "<%= root_path(locale: :ar) %> | <%= url_for(locale: :ar) %>"
end
end
APP = build_app Routes
def app; APP end
def test_path_parameters_are_not_mutated
get "/en/about"
assert_equal "/ar | /ar/about", @response.body
end
end
class TestInternalRoutingParams < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
get "/test_internal/:internal" => "internal#internal"
end
end
class ::InternalController < ActionController::Base
def internal
head :ok
end
end
APP = build_app Routes
def app
APP
end
def test_paths_with_partial_dynamic_segments_are_recognised
get "/test_internal/123"
assert_equal 200, response.status
assert_equal(
{ controller: "internal", action: "internal", internal: "123" },
request.path_parameters
)
end
end
class FlashRedirectTest < ActionDispatch::IntegrationTest
SessionKey = "_myapp_session"
Generator = ActiveSupport::CachingKeyGenerator.new(
ActiveSupport::KeyGenerator.new("b3c631c314c0bbca50c1b2843150fe33", iterations: 1000)
)
Rotations = ActiveSupport::Messages::RotationConfiguration.new
SIGNED_COOKIE_SALT = "signed cookie"
ENCRYPTED_SIGNED_COOKIE_SALT = "signed encrypted cookie"
class KeyGeneratorMiddleware
def initialize(app)
@app = app
end
def call(env)
env["action_dispatch.key_generator"] ||= Generator
env["action_dispatch.cookies_rotations"] ||= Rotations
env["action_dispatch.signed_cookie_salt"] = SIGNED_COOKIE_SALT
env["action_dispatch.encrypted_signed_cookie_salt"] = ENCRYPTED_SIGNED_COOKIE_SALT
@app.call(env)
end
end
class FooController < ActionController::Base
def bar
render plain: (flash[:foo] || "foo")
end
end
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
get "/foo", to: redirect { |params, req| req.flash[:foo] = "bar"; "/bar" }
get "/bar", to: "flash_redirect_test/foo#bar"
end
APP = build_app Routes do |middleware|
middleware.use KeyGeneratorMiddleware
middleware.use ActionDispatch::Session::CookieStore, key: SessionKey
middleware.use ActionDispatch::Flash
middleware.delete ActionDispatch::ShowExceptions
end
def app
APP
end
include Routes.url_helpers
def test_block_redirect_commits_flash
get "/foo", env: { "action_dispatch.key_generator" => Generator }
assert_response :redirect
follow_redirect!
assert_equal "bar", response.body
end
end
class TestRecognizePath < ActionDispatch::IntegrationTest
class PageConstraint
attr_reader :key, :pattern
def initialize(key, pattern)
@key = key
@pattern = pattern
end
def matches?(request)
pattern.match?(request.path_parameters[key])
end
end
stub_controllers do |routes|
Routes = routes
routes.draw do
get "/hash/:foo", to: "pages#show", constraints: { foo: /foo/ }
get "/hash/:bar", to: "pages#show", constraints: { bar: /bar/ }
get "/proc/:foo", to: "pages#show", constraints: proc { |r| /foo/.match?(r.path_parameters[:foo]) }
get "/proc/:bar", to: "pages#show", constraints: proc { |r| /bar/.match?(r.path_parameters[:bar]) }
get "/class/:foo", to: "pages#show", constraints: PageConstraint.new(:foo, /foo/)
get "/class/:bar", to: "pages#show", constraints: PageConstraint.new(:bar, /bar/)
end
end
APP = build_app Routes
def app
APP
end
def test_hash_constraints_dont_leak_between_routes
expected_params = { controller: "pages", action: "show", bar: "bar" }
actual_params = recognize_path("/hash/bar")
assert_equal expected_params, actual_params
end
def test_proc_constraints_dont_leak_between_routes
expected_params = { controller: "pages", action: "show", bar: "bar" }
actual_params = recognize_path("/proc/bar")
assert_equal expected_params, actual_params
end
def test_class_constraints_dont_leak_between_routes
expected_params = { controller: "pages", action: "show", bar: "bar" }
actual_params = recognize_path("/class/bar")
assert_equal expected_params, actual_params
end
private
def recognize_path(*args)
Routes.recognize_path(*args)
end
end
class TestRelativeUrlRootGeneration < ActionDispatch::IntegrationTest
config = ActionDispatch::Routing::RouteSet::Config.new("/blog", false)
stub_controllers(config) do |routes|
Routes = routes
routes.draw do
get "/", to: "posts#index", as: :posts
get "/:id", to: "posts#show", as: :post
end
end
include Routes.url_helpers
APP = build_app Routes
def app
APP
end
def test_url_helpers
assert_equal "/blog/", posts_path({})
assert_equal "/blog/", Routes.url_helpers.posts_path({})
assert_equal "/blog/1", post_path(id: "1")
assert_equal "/blog/1", Routes.url_helpers.post_path(id: "1")
end
def test_optimized_url_helpers
assert_equal "/blog/", posts_path
assert_equal "/blog/", Routes.url_helpers.posts_path
assert_equal "/blog/1", post_path("1")
assert_equal "/blog/1", Routes.url_helpers.post_path("1")
end
end
|