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
|
# frozen_string_literal: true
require "abstract_unit"
require "controller/fake_controllers"
require "rails/engine"
require "launchy"
class SessionTest < ActiveSupport::TestCase
StubApp = lambda { |env|
[200, { "Content-Type" => "text/html", "Content-Length" => "13" }, ["Hello, World!"]]
}
def setup
@session = ActionDispatch::Integration::Session.new(StubApp)
end
def test_https_bang_works_and_sets_truth_by_default
assert_not_predicate @session, :https?
@session.https!
assert_predicate @session, :https?
@session.https! false
assert_not_predicate @session, :https?
end
def test_host!
assert_not_equal "glu.ttono.us", @session.host
@session.host! "rubyonrails.com"
assert_equal "rubyonrails.com", @session.host
end
def test_follow_redirect_raises_when_no_redirect
@session.stub :redirect?, false do
assert_raise(RuntimeError) { @session.follow_redirect! }
end
end
def test_get
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:get, path], params: params, headers: headers do
@session.get(path, params: params, headers: headers)
end
end
def test_get_with_env_and_headers
path = "/index"; params = "blah"; headers = { location: "blah" }; env = { "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest" }
assert_called_with @session, :process, [:get, path], params: params, headers: headers, env: env do
@session.get(path, params: params, headers: headers, env: env)
end
end
def test_post
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:post, path], params: params, headers: headers do
@session.post(path, params: params, headers: headers)
end
end
def test_patch
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:patch, path], params: params, headers: headers do
@session.patch(path, params: params, headers: headers)
end
end
def test_put
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:put, path], params: params, headers: headers do
@session.put(path, params: params, headers: headers)
end
end
def test_delete
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:delete, path], params: params, headers: headers do
@session.delete(path, params: params, headers: headers)
end
end
def test_head
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:head, path], params: params, headers: headers do
@session.head(path, params: params, headers: headers)
end
end
def test_xml_http_request_get
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:get, path], params: params, headers: headers, xhr: true do
@session.get(path, params: params, headers: headers, xhr: true)
end
end
def test_xml_http_request_post
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:post, path], params: params, headers: headers, xhr: true do
@session.post(path, params: params, headers: headers, xhr: true)
end
end
def test_xml_http_request_patch
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:patch, path], params: params, headers: headers, xhr: true do
@session.patch(path, params: params, headers: headers, xhr: true)
end
end
def test_xml_http_request_put
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:put, path], params: params, headers: headers, xhr: true do
@session.put(path, params: params, headers: headers, xhr: true)
end
end
def test_xml_http_request_delete
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:delete, path], params: params, headers: headers, xhr: true do
@session.delete(path, params: params, headers: headers, xhr: true)
end
end
def test_xml_http_request_head
path = "/index"; params = "blah"; headers = { location: "blah" }
assert_called_with @session, :process, [:head, path], params: params, headers: headers, xhr: true do
@session.head(path, params: params, headers: headers, xhr: true)
end
end
end
class IntegrationTestTest < ActiveSupport::TestCase
def setup
@test = ::ActionDispatch::IntegrationTest.new(:app)
end
def test_opens_new_session
session1 = @test.open_session { |sess| }
session2 = @test.open_session # implicit session
assert_not session1.equal?(session2)
end
def test_child_session_assertions_bubble_up_to_root
assertions_before = @test.assertions
@test.open_session.assert(true)
assertions_after = @test.assertions
assert_equal 1, assertions_after - assertions_before
end
# RSpec mixes Matchers (which has a #method_missing) into
# IntegrationTest's superclass. Make sure IntegrationTest does not
# try to delegate these methods to the session object.
def test_does_not_prevent_method_missing_passing_up_to_ancestors
mixin = Module.new do
def method_missing(name, ...)
name == :foo ? "pass" : super
end
end
@test.class.superclass.include(mixin)
begin
assert_equal "pass", @test.foo
ensure
# leave other tests as unaffected as possible
mixin.remove_method :method_missing
end
end
end
class RackLintIntegrationTest < ActionDispatch::IntegrationTest
test "integration test follows rack SPEC" do
with_routing do |set|
set.draw do
get "/", to: ->(_) { [200, {}, [""]] }
end
get "/"
assert_equal 200, status
end
end
def app
@app ||= self.class.build_app do |middleware|
middleware.unshift Rack::Lint
end
end
end
# Tests that integration tests don't call Controller test methods for processing.
# Integration tests have their own setup and teardown.
class IntegrationTestUsesCorrectClass < ActionDispatch::IntegrationTest
def test_integration_methods_called
reset!
headers = { "Origin" => "*" }
%w( get post head patch put delete options ).each do |verb|
assert_nothing_raised { __send__(verb, "/", headers: headers) }
end
end
end
class IntegrationProcessTest < ActionDispatch::IntegrationTest
class IntegrationController < ActionController::Base
def get
respond_to do |format|
format.html { render plain: "OK", status: 200 }
format.js { render plain: "JS OK", status: 200 }
format.json { render json: "JSON OK", status: 200 }
format.xml { render xml: "<root></root>", status: 200 }
format.rss { render xml: "<root></root>", status: 200 }
format.atom { render xml: "<root></root>", status: 200 }
end
end
def get_with_vary_set_x_requested_with
respond_to do |format|
format.json do
response.headers["Vary"] = "X-Requested-With"
render json: "JSON OK", status: 200
end
end
end
def get_with_params
render plain: "foo: #{params[:foo]}", status: 200
end
def post
render plain: "Created", status: 201
end
def method
render plain: "method: #{request.method.downcase}"
end
def cookie_monster
cookies["cookie_1"] = nil
cookies["cookie_3"] = "chocolate"
render plain: "Gone", status: 410
end
def set_cookie
cookies["foo"] = "bar"
head :ok
end
def get_cookie
render plain: cookies["foo"]
end
def redirect
redirect_to action_url("get")
end
def redirect_307
redirect_to action_url("post"), status: 307
end
def redirect_308
redirect_to action_url("post"), status: 308
end
def remove_header
response.headers.delete params[:header]
head :ok, "c" => "3"
end
end
def test_get
with_test_route_set do
get "/get"
assert_equal 200, status
assert_equal "OK", status_message
assert_response 200
assert_response :success
assert_response :ok
assert_equal({}, cookies.to_hash)
assert_equal "OK", body
assert_equal "OK", response.body
assert_kind_of Nokogiri::HTML::Document, html_document
assert_equal 1, request_count
end
end
def test_get_xml_rss_atom
%w[ application/xml application/rss+xml application/atom+xml ].each do |mime_string|
with_test_route_set do
get "/get", headers: { "HTTP_ACCEPT" => mime_string }
assert_equal 200, status
assert_equal "OK", status_message
assert_response 200
assert_response :success
assert_response :ok
assert_equal({}, cookies.to_hash)
assert_equal "<root></root>", body
assert_equal "<root></root>", response.body
assert_instance_of Nokogiri::XML::Document, html_document
assert_equal 1, request_count
end
end
end
def test_post
with_test_route_set do
post "/post"
assert_equal 201, status
assert_equal "Created", status_message
assert_response 201
assert_response :success
assert_response :created
assert_equal({}, cookies.to_hash)
assert_equal "Created", body
assert_equal "Created", response.body
assert_kind_of Nokogiri::HTML::Document, html_document
assert_equal 1, request_count
end
end
include CookieAssertions
test "response cookies are added to the cookie jar for the next request" do
with_test_route_set do
cookies["cookie_1"] = "sugar"
cookies["cookie_2"] = "oatmeal"
get "/cookie_monster"
assert_set_cookie_header "cookie_1=; path=/\ncookie_3=chocolate; path=/", headers["Set-Cookie"]
assert_equal({ "cookie_1" => "", "cookie_2" => "oatmeal", "cookie_3" => "chocolate" }, cookies.to_hash)
end
end
test "cookie persist to next request" do
with_test_route_set do
get "/set_cookie"
assert_response :success
assert_equal "foo=bar; path=/", headers["Set-Cookie"]
assert_equal({ "foo" => "bar" }, cookies.to_hash)
get "/get_cookie"
assert_response :success
assert_equal "bar", body
assert_nil headers["Set-Cookie"]
assert_equal({ "foo" => "bar" }, cookies.to_hash)
end
end
test "cookie persist to next request on another domain" do
with_test_route_set do
host! "37s.backpack.test"
get "/set_cookie"
assert_response :success
assert_equal "foo=bar; path=/", headers["Set-Cookie"]
assert_equal({ "foo" => "bar" }, cookies.to_hash)
get "/get_cookie"
assert_response :success
assert_equal "bar", body
assert_nil headers["Set-Cookie"]
assert_equal({ "foo" => "bar" }, cookies.to_hash)
end
end
def test_redirect
with_test_route_set do
get "/redirect"
assert_equal 302, status
assert_equal "Found", status_message
assert_response 302
assert_response :redirect
assert_response :found
assert_equal "", response.body
assert_kind_of Nokogiri::HTML::Document, html_document
assert_equal 1, request_count
follow_redirect!
assert_equal "http://www.example.com/redirect", request.referer
assert_response :success
assert_equal "/get", path
get "/moved"
assert_response :redirect
assert_redirected_to "/method"
end
end
def test_307_redirect_uses_the_same_http_verb
with_test_route_set do
post "/redirect_307"
assert_equal 307, status
follow_redirect!
assert_equal "POST", request.method
end
end
def test_308_redirect_uses_the_same_http_verb
with_test_route_set do
post "/redirect_308"
assert_equal 308, status
follow_redirect!
assert_equal "POST", request.method
end
end
def test_redirect_reset_html_document
with_test_route_set do
get "/redirect"
previous_html_document = html_document
follow_redirect!
assert_response :ok
assert_not_same previous_html_document, html_document
end
end
def test_redirect_with_arguments
with_test_route_set do
get "/redirect"
follow_redirect! params: { foo: :bar }
assert_response :ok
assert_equal "bar", request.parameters["foo"]
end
end
def test_xml_http_request_get
with_test_route_set do
get "/get", xhr: true
assert_equal 200, status
assert_equal "OK", status_message
assert_response 200
assert_response :success
assert_response :ok
assert_equal "JS OK", response.body
end
end
def test_request_with_bad_format
with_test_route_set do
get "/get.php", xhr: true
assert_equal 406, status
assert_response 406
assert_response :not_acceptable
end
end
test "creation of multiple integration sessions" do
integration_session # initialize first session
a = open_session
b = open_session
assert_not_same(a.integration_session, b.integration_session)
end
def test_get_with_query_string
with_test_route_set do
get "/get_with_params?foo=bar"
assert_equal "/get_with_params?foo=bar", request.env["REQUEST_URI"]
assert_equal "/get_with_params?foo=bar", request.fullpath
assert_equal "foo=bar", request.env["QUERY_STRING"]
assert_equal "foo=bar", request.query_string
assert_equal "bar", request.parameters["foo"]
assert_equal 200, status
assert_equal "foo: bar", response.body
end
end
def test_get_with_parameters
with_test_route_set do
get "/get_with_params", params: { foo: "bar" }
assert_equal "/get_with_params", request.env["PATH_INFO"]
assert_equal "/get_with_params", request.path_info
assert_equal "foo=bar", request.env["QUERY_STRING"]
assert_equal "foo=bar", request.query_string
assert_equal "bar", request.parameters["foo"]
assert_equal 200, status
assert_equal "foo: bar", response.body
end
end
def test_post_then_get_with_parameters_do_not_leak_across_requests
with_test_route_set do
post "/post", params: { leaks: "does-leak?" }
get "/get_with_params", params: { foo: "bar" }
input = request.env["rack.input"]
assert(input.nil? || input.read == "")
assert_equal "foo=bar", request.env["QUERY_STRING"]
assert_equal "foo=bar", request.query_string
assert_equal "bar", request.parameters["foo"]
assert_predicate request.parameters["leaks"], :nil?
end
end
def test_head
with_test_route_set do
head "/get"
assert_equal 200, status
assert_equal "", body
head "/post"
assert_equal 201, status
assert_equal "", body
get "/get/method"
assert_equal 200, status
assert_equal "method: get", body
head "/get/method"
assert_equal 200, status
assert_equal "", body
end
end
def test_generate_url_with_controller
assert_equal "http://www.example.com/foo", url_for(controller: "foo")
end
def test_port_via_host!
with_test_route_set do
host! "www.example.com:8080"
get "/get"
assert_equal 8080, request.port
end
end
def test_port_via_process
with_test_route_set do
get "http://www.example.com:8080/get"
assert_equal 8080, request.port
end
end
def test_https_and_port_via_host_and_https!
with_test_route_set do
host! "www.example.com"
https! true
get "/get"
assert_equal 443, request.port
assert_equal true, request.ssl?
host! "www.example.com:443"
https! true
get "/get"
assert_equal 443, request.port
assert_equal true, request.ssl?
host! "www.example.com:8443"
https! true
get "/get"
assert_equal 8443, request.port
assert_equal true, request.ssl?
end
end
def test_https_and_port_via_process
with_test_route_set do
get "https://www.example.com/get"
assert_equal 443, request.port
assert_equal true, request.ssl?
get "https://www.example.com:8443/get"
assert_equal 8443, request.port
assert_equal true, request.ssl?
end
end
def test_respect_removal_of_default_headers_by_a_controller_action
with_test_route_set do
with_default_headers "a" => "1", "b" => "2" do
get "/remove_header", params: { header: "a" }
end
end
assert_not_includes @response.headers, "a", "Response should not include default header removed by the controller action"
assert_includes @response.headers, "b"
assert_includes @response.headers, "c"
end
def test_accept_not_overridden_when_xhr_true
with_test_route_set do
get "/get", headers: { "Accept" => "application/json" }, xhr: true
assert_equal "application/json", request.accept
assert_equal "application/json", response.media_type
get "/get", headers: { "HTTP_ACCEPT" => "application/json" }, xhr: true
assert_equal "application/json", request.accept
assert_equal "application/json", response.media_type
end
end
def test_setting_vary_header_when_request_is_xhr_with_accept_header
with_test_route_set do
get "/get", headers: { "Accept" => "application/json" }, xhr: true
assert_equal "Accept", response.headers["Vary"]
end
end
def test_not_setting_vary_header_when_format_is_provided
with_test_route_set do
get "/get", params: { format: "json" }
assert_nil response.headers["Vary"]
end
end
def test_not_setting_vary_header_when_it_has_already_been_set
with_test_route_set do
get "/get_with_vary_set_x_requested_with", headers: { "Accept" => "application/json" }, xhr: true
assert_equal "X-Requested-With", response.headers["Vary"]
end
end
def test_not_setting_vary_header_when_ignore_accept_header_is_set
original_ignore_accept_header = ActionDispatch::Request.ignore_accept_header
ActionDispatch::Request.ignore_accept_header = true
with_test_route_set do
get "/get", headers: { "Accept" => "application/json" }, xhr: true
assert_nil response.headers["Vary"]
end
ensure
ActionDispatch::Request.ignore_accept_header = original_ignore_accept_header
end
private
def with_default_headers(headers)
original = ActionDispatch::Response.default_headers
ActionDispatch::Response.default_headers = headers
yield
ensure
ActionDispatch::Response.default_headers = original
end
def with_test_route_set
with_routing do |set|
controller = ::IntegrationProcessTest::IntegrationController.clone
controller.class_eval do
include set.url_helpers
end
set.draw do
get "moved" => redirect("/method")
ActionDispatch.deprecator.silence do
match ":action", to: controller, via: [:get, :post], as: :action
get "get/:action", to: controller, as: :get_action
end
end
singleton_class.include(set.url_helpers)
yield
end
end
end
class MetalIntegrationTest < ActionDispatch::IntegrationTest
include SharedTestRoutes.url_helpers
class Poller
def self.call(env)
if /^\/success/.match?(env["PATH_INFO"])
[200, { "Content-Type" => "text/plain", "Content-Length" => "12" }, ["Hello World!"]]
else
[404, { "Content-Type" => "text/plain", "Content-Length" => "0" }, []]
end
end
end
def setup
@app = Poller
end
def test_successful_get
get "/success"
assert_response 200
assert_response :success
assert_response :ok
assert_equal "Hello World!", response.body
end
def test_failed_get
get "/failure"
assert_response 404
assert_response :not_found
assert_equal "", response.body
end
def test_generate_url_without_controller
assert_equal "http://www.example.com/foo", url_for(controller: "foo")
end
def test_pass_headers
get "/success", headers: { "Referer" => "http://www.example.com/foo", "Host" => "http://nohost.com" }
assert_equal "http://nohost.com", @request.env["HTTP_HOST"]
assert_equal "http://www.example.com/foo", @request.env["HTTP_REFERER"]
end
def test_pass_headers_and_env
get "/success", headers: { "X-Test-Header" => "value" }, env: { "HTTP_REFERER" => "http://test.com/", "HTTP_HOST" => "http://test.com" }
assert_equal "http://test.com", @request.env["HTTP_HOST"]
assert_equal "http://test.com/", @request.env["HTTP_REFERER"]
assert_equal "value", @request.env["HTTP_X_TEST_HEADER"]
end
def test_pass_env
get "/success", env: { "HTTP_REFERER" => "http://test.com/", "HTTP_HOST" => "http://test.com" }
assert_equal "http://test.com", @request.env["HTTP_HOST"]
assert_equal "http://test.com/", @request.env["HTTP_REFERER"]
end
def test_ignores_common_ports_in_host
get "http://test.com"
assert_equal "test.com", @request.env["HTTP_HOST"]
get "https://test.com"
assert_equal "test.com", @request.env["HTTP_HOST"]
end
def test_keeps_uncommon_ports_in_host
get "http://test.com:123"
assert_equal "test.com:123", @request.env["HTTP_HOST"]
get "http://test.com:443"
assert_equal "test.com:443", @request.env["HTTP_HOST"]
get "https://test.com:80"
assert_equal "test.com:80", @request.env["HTTP_HOST"]
end
end
class ApplicationIntegrationTest < ActionDispatch::IntegrationTest
class MetalController < ActionController::Metal
def new
self.status = 200
end
end
class TestController < ActionController::Base
def index
render plain: "index"
end
end
def self.call(env)
routes.call(env)
end
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
class MountedApp < Rails::Engine
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
routes.draw do
get "baz", to: "application_integration_test/test#index", as: :baz
end
def self.call(*)
end
end
routes.draw do
get "", to: "application_integration_test/test#index", as: :empty_string
get "metal", to: "application_integration_test/metal#new", as: :new_metal
get "foo", to: "application_integration_test/test#index", as: :foo
get "bar", to: "application_integration_test/test#index", as: :bar
mount MountedApp => "/mounted", :as => "mounted"
get "fooz" => proc { |env| [ 200, { ActionDispatch::Constants::X_CASCADE => "pass" }, [ "omg" ] ] },
:anchor => false
get "fooz", to: "application_integration_test/test#index"
end
def app
self.class
end
test "includes route helpers" do
assert_equal "/", empty_string_path
assert_equal "/foo", foo_path
assert_equal "/bar", bar_path
end
test "includes mounted helpers" do
assert_equal "/mounted/baz", mounted.baz_path
end
test "path after cascade pass" do
get "/fooz"
assert_equal "index", response.body
assert_equal "/fooz", path
end
test "route helpers after controller access" do
get "/"
assert_equal "/", empty_string_path
get "/foo"
assert_equal "/foo", foo_path
get "/bar"
assert_equal "/bar", bar_path
end
test "route helpers after metal controller access" do
get "/metal"
assert_equal "/foo?q=solution", foo_path(q: "solution")
end
test "missing route helper before controller access" do
assert_raise(NameError) { missing_path }
end
test "missing route helper after controller access" do
get "/foo"
assert_raise(NameError) { missing_path }
end
test "process do not modify the env passed as argument" do
env = { :SERVER_NAME => "server", "action_dispatch.custom" => "custom" }
old_env = env.dup
get "/foo", env: env
assert_equal old_env, env
end
end
class EnvironmentFilterIntegrationTest < ActionDispatch::IntegrationTest
class TestController < ActionController::Base
def post
render plain: "Created", status: 201
end
end
def self.call(env)
env["action_dispatch.parameter_filter"] = [:password]
routes.call(env)
end
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
routes.draw do
match "/post", to: "environment_filter_integration_test/test#post", via: :post
end
def app
self.class
end
test "filters rack request form vars" do
post "/post", params: { username: "cjolly", password: "secret" }
assert_equal "cjolly", request.filtered_parameters["username"]
assert_equal "[FILTERED]", request.filtered_parameters["password"]
assert_equal "[FILTERED]", request.filtered_env["rack.request.form_vars"]
end
end
class ControllerWithHeadersMethodIntegrationTest < ActionDispatch::IntegrationTest
class TestController < ActionController::Base
def index
render plain: "ok"
end
def headers
{}.freeze
end
end
test "doesn't call controller's headers method" do
with_routing do |routes|
routes.draw do
get "/ok" => "controller_with_headers_method_integration_test/test#index"
end
get "/ok"
assert_response 200
end
end
end
class UrlOptionsIntegrationTest < ActionDispatch::IntegrationTest
class FooController < ActionController::Base
def index
render plain: "foo#index"
end
def show
render plain: "foo#show"
end
def edit
render plain: "foo#show"
end
end
class BarController < ActionController::Base
def default_url_options
{ host: "bar.com" }
end
def index
render plain: "foo#index"
end
end
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
def self.call(env)
routes.call(env)
end
def app
self.class
end
routes.draw do
default_url_options host: "foo.com"
scope module: "url_options_integration_test" do
get "/foo" => "foo#index", :as => :foos
get "/foo/:id" => "foo#show", :as => :foo
get "/foo/:id/edit" => "foo#edit", :as => :edit_foo
get "/bar" => "bar#index", :as => :bars
end
end
test "session uses default URL options from routes" do
assert_equal "http://foo.com/foo", foos_url
end
test "current host overrides default URL options from routes" do
get "/foo"
assert_response :success
assert_equal "http://www.example.com/foo", foos_url
end
test "controller can override default URL options from request" do
get "/bar"
assert_response :success
assert_equal "http://bar.com/foo", foos_url
end
def test_can_override_default_url_options
original_host = default_url_options.dup
default_url_options[:host] = "foobar.com"
assert_equal "http://foobar.com/foo", foos_url
get "/bar"
assert_response :success
assert_equal "http://foobar.com/foo", foos_url
ensure
ActionDispatch::Integration::Session.default_url_options = self.default_url_options = original_host
end
test "current request path parameters are recalled" do
get "/foo/1"
assert_response :success
assert_equal "/foo/1/edit", url_for(action: "edit", only_path: true)
end
end
class HeadWithStatusActionIntegrationTest < ActionDispatch::IntegrationTest
class FooController < ActionController::Base
def status
head :ok
end
end
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
def self.call(env)
routes.call(env)
end
def app
self.class
end
routes.draw do
get "/foo/status" => "head_with_status_action_integration_test/foo#status"
end
test "get /foo/status with head result does not cause stack overflow error" do
assert_nothing_raised do
get "/foo/status"
end
assert_response :ok
end
end
class IntegrationWithRoutingTest < ActionDispatch::IntegrationTest
class FooController < ActionController::Base
def index
render plain: "ok"
end
end
def test_with_routing_resets_session
klass_namespace = self.class.name.underscore
with_routing do |routes|
routes.draw do
namespace klass_namespace do
resources :foo, path: "/with"
end
end
get "/integration_with_routing_test/with"
assert_response 200
assert_equal "ok", response.body
end
with_routing do |routes|
routes.draw do
namespace klass_namespace do
resources :foo, path: "/routing"
end
end
get "/integration_with_routing_test/routing"
assert_response 200
assert_equal "ok", response.body
end
end
end
# to work in contexts like rspec before(:all)
class IntegrationRequestsWithoutSetup < ActionDispatch::IntegrationTest
self._setup_callbacks = []
self._teardown_callbacks = []
class FooController < ActionController::Base
def ok
cookies[:key] = "ok"
render plain: "ok"
end
end
def test_request
with_routing do |routes|
routes.draw do
ActionDispatch.deprecator.silence do
get ":action" => FooController
end
end
get "/ok"
assert_response 200
assert_equal "ok", response.body
assert_equal "ok", cookies["key"]
end
end
end
# to ensure that session requirements in setup are persisted in the tests
class IntegrationRequestsWithSessionSetup < ActionDispatch::IntegrationTest
setup do
cookies["user_name"] = "david"
end
def test_cookies_set_in_setup_are_persisted_through_the_session
get "/foo"
assert_equal({ "user_name" => "david" }, cookies.to_hash)
end
end
class IntegrationRequestEncodersTest < ActionDispatch::IntegrationTest
class FooController < ActionController::Base
def foos
render plain: "ok"
end
def foos_json
render json: params.permit(:foo)
end
def foos_wibble
render plain: "ok"
end
def foos_json_api
render plain: "ok"
end
end
def test_standard_json_encoding_works
with_routing do |routes|
routes.draw do
ActionDispatch.deprecator.silence do
post ":action" => FooController
end
end
post "/foos_json.json", params: { foo: "fighters" }.to_json,
headers: { "Content-Type" => "application/json" }
assert_response :success
assert_equal({ "foo" => "fighters" }, response.parsed_body)
end
end
def test_encoding_as_json
post_to_foos as: :json do
assert_response :success
assert_equal "application/json", request.media_type
assert_equal "application/json", request.accepts.first.to_s
assert_equal :json, request.format.ref
assert_equal({ "foo" => "fighters" }, request.request_parameters)
assert_equal({ "foo" => "fighters" }, response.parsed_body)
end
end
def test_doesnt_mangle_request_path
with_routing do |routes|
routes.draw do
ActionDispatch.deprecator.silence do
post ":action" => FooController
end
end
post "/foos"
assert_equal "/foos", request.path
post "/foos_json", as: :json
assert_equal "/foos_json", request.path
end
end
def test_encoding_as_without_mime_registration
assert_raise ArgumentError do
ActionDispatch::IntegrationTest.register_encoder :wibble
end
end
def test_registering_custom_encoder
Mime::Type.register "text/wibble", :wibble
ActionDispatch::IntegrationTest.register_encoder(:wibble,
param_encoder: -> params { params })
post_to_foos as: :wibble do
assert_response :success
assert_equal "/foos_wibble", request.path
assert_equal "text/wibble", request.media_type
assert_equal "text/wibble", request.accepts.first.to_s
assert_equal :wibble, request.format.ref
assert_equal Hash.new, request.request_parameters # Unregistered MIME Type can't be parsed.
assert_equal "ok", response.parsed_body
end
ensure
Mime::Type.unregister :wibble
end
def test_registering_custom_encoder_including_parameters
accept_header = 'application/vnd.api+json; profile="https://jsonapi.org/profiles/ethanresnick/cursor-pagination/"; ext="https://jsonapi.org/ext/atomic"'
Mime::Type.register accept_header, :json_api
ActionDispatch::IntegrationTest.register_encoder(:json_api,
param_encoder: -> params { params })
post_to_foos as: :json_api do
assert_response :success
assert_equal "/foos_json_api", request.path
assert_equal "application/vnd.api+json", request.media_type
assert_equal accept_header, request.accepts.first.to_s
assert_equal :json_api, request.format.ref
assert_equal Hash.new, request.request_parameters # Unregistered MIME Type can't be parsed.
assert_equal "ok", response.parsed_body
end
ensure
Mime::Type.unregister :json_api
end
def test_parsed_body_without_as_option
with_routing do |routes|
routes.draw do
ActionDispatch.deprecator.silence do
get ":action" => FooController
end
end
get "/foos_json.json", params: { foo: "heyo" }
assert_equal({ "foo" => "heyo" }, response.parsed_body)
end
end
def test_get_parameters_with_as_option
with_routing do |routes|
routes.draw do
ActionDispatch.deprecator.silence do
get ":action" => FooController
end
end
get "/foos_json?foo=heyo", as: :json
assert_equal({ "foo" => "heyo" }, response.parsed_body)
end
end
def test_get_request_with_json_uses_method_override_and_sends_a_post_request
with_routing do |routes|
routes.draw do
ActionDispatch.deprecator.silence do
get ":action" => FooController
end
end
get "/foos_json", params: { foo: "heyo" }, as: :json
assert_equal "POST", request.method
assert_equal "GET", request.headers["X-Http-Method-Override"]
assert_equal({ "foo" => "heyo" }, response.parsed_body)
end
end
def test_get_request_with_json_excludes_null_query_string
with_routing do |routes|
routes.draw do
ActionDispatch.deprecator.silence do
get ":action" => FooController
end
end
get "/foos_json", as: :json
assert_equal "http://www.example.com/foos_json", request.url
end
end
private
def post_to_foos(as:)
with_routing do |routes|
routes.draw do
ActionDispatch.deprecator.silence do
post ":action" => FooController
end
end
post "/foos_#{as}", params: { foo: "fighters" }, as: as
yield
end
end
end
class IntegrationFileUploadTest < ActionDispatch::IntegrationTest
class IntegrationController < ActionController::Base
def test_file_upload
render plain: params[:file].size
end
end
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
def self.call(env)
routes.call(env)
end
def app
self.class
end
def self.file_fixture_path
File.expand_path("../fixtures/multipart", __dir__)
end
routes.draw do
post "test_file_upload", to: "integration_file_upload_test/integration#test_file_upload"
end
def test_fixture_file_upload
post "/test_file_upload",
params: {
file: fixture_file_upload("/ruby_on_rails.jpg", "image/jpeg")
}
assert_equal "45142", @response.body
end
end
# rubocop:disable Lint/Debugger
class PageDumpIntegrationTest < ActionDispatch::IntegrationTest
class FooController < ActionController::Base
def index
render plain: "Hello world"
end
def redirect
redirect_to action: :index
end
end
def with_root(&block)
Rails.stub(:root, Pathname.getwd.join("test"), &block)
end
def setup
with_root do
remove_dumps
end
end
def teardown
with_root do
remove_dumps
end
end
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
def self.call(env)
routes.call(env)
end
def app
self.class
end
def dump_path
Pathname.new(Dir["#{Rails.root}/tmp/html_dump/#{method_name}*"].sole)
end
def remove_dumps
Dir["#{Rails.root}/tmp/html_dump/#{method_name}*"].each(&File.method(:delete))
end
routes.draw do
get "/" => "page_dump_integration_test/foo#index"
get "/redirect" => "page_dump_integration_test/foo#redirect"
end
test "save_and_open_page saves a copy of the page and call to Launchy" do
launchy_called = false
get "/"
with_root do
Launchy.stub(:open, ->(path) { launchy_called = (path == dump_path) }) do
save_and_open_page
end
assert launchy_called
assert_equal File.read(dump_path), response.body
end
end
test "prints a warning to install launchy if it can't be loaded" do
get "/"
with_root do
Launchy.stub(:open, ->(path) { raise LoadError.new }) do
self.stub(:warn, ->(warning) { warning.include?("Please install the launchy gem to open the file automatically.") }) do
save_and_open_page
end
end
assert_equal File.read(dump_path), response.body
end
end
test "raises when called after a redirect" do
with_root do
get "/redirect"
assert_raise(InvalidResponse) { save_and_open_page }
end
end
end
# rubocop:enable Lint/Debugger
|