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
|
# frozen_string_literal: true
require "abstract_unit"
require "controller/fake_models"
class TestControllerWithExtraEtags < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new(
"test/with_implicit_template.erb" => "Hello explicitly!",
"test/hello_world.erb" => "Hello world!"
)]
def self.controller_path; "test"; end
etag { nil }
etag { "ab" }
etag { :cde }
etag { [:f] }
etag { nil }
def fresh
render plain: "stale" if stale?(etag: "123", template: false)
end
def array
render plain: "stale" if stale?(etag: %w(1 2 3), template: false)
end
def strong
render plain: "stale" if stale?(strong_etag: "strong", template: false)
end
def with_template
if stale? template: "test/hello_world"
render plain: "stale"
end
end
def with_implicit_template
fresh_when(etag: "123")
end
end
class ImplicitRenderTestController < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new(
"implicit_render_test/hello_world.erb" => "Hello world!",
"implicit_render_test/empty_action_with_template.html.erb" => "<h1>Empty action rendered this implicitly.</h1>\n"
)]
def empty_action
end
def empty_action_with_template
end
end
module Namespaced
class ImplicitRenderTestController < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new(
"namespaced/implicit_render_test/hello_world.erb" => "Hello world!"
)]
def hello_world
fresh_when(etag: "abc")
end
end
end
class InheritedRenderTestController < ImplicitRenderTestController
def hello_world
fresh_when(etag: "abc")
end
end
class TestController < ActionController::Base
protect_from_forgery
before_action :set_variable_for_layout
class LabellingFormBuilder < ActionView::Helpers::FormBuilder
end
layout :determine_layout
def name
nil
end
private :name
helper_method :name
def hello_world
end
def conditional_hello
if stale?(last_modified: Time.now.utc.beginning_of_day, etag: [:foo, 123], cache_control: { no_cache: true })
render action: "hello_world"
end
end
def conditional_hello_with_record
record = Struct.new(:updated_at, :cache_key).new(Time.now.utc.beginning_of_day, "foo/123")
if stale?(record)
render action: "hello_world"
end
end
def conditional_hello_with_array_of_records
record = Struct.new(:updated_at, :cache_key).new(Time.now.utc.beginning_of_day, "foo/123")
old_record = Struct.new(:updated_at, :cache_key).new(Time.now.utc.beginning_of_day.yesterday, "bar/123")
if stale?([record, old_record])
render action: "hello_world"
end
end
def dynamic_render
render params[:id] # => String, AC::Params
end
def dynamic_render_permit
render params[:id].permit(:file)
end
def dynamic_render_with_file
# This is extremely bad, but should be possible to do.
file = params[:id] # => String, AC::Params
render file: file
end
class Collection
def initialize(records)
@records = records
end
def maximum(attribute)
@records.max_by(&attribute).public_send(attribute)
end
end
def conditional_hello_with_collection_of_records
ts = Time.now.utc.beginning_of_day
record = Struct.new(:updated_at, :cache_key).new(ts, "foo/123")
old_record = Struct.new(:updated_at, :cache_key).new(ts - 1.day, "bar/123")
if stale?(Collection.new([record, old_record]))
render action: "hello_world"
end
end
def conditional_hello_with_expires_in
expires_in 60.1.seconds
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_public
expires_in 1.minute, public: true
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_must_revalidate
expires_in 1.minute, must_revalidate: true
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_public_and_must_revalidate
expires_in 1.minute, public: true, must_revalidate: true
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_stale_while_revalidate
expires_in 1.minute, public: true, stale_while_revalidate: 5.minutes
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_stale_if_error
expires_in 1.minute, public: true, stale_if_error: 5.minutes
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_public_with_more_keys
expires_in 1.minute, :public => true, "s-maxage" => 5.hours
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_public_with_more_keys_old_syntax
expires_in 1.minute, :public => true, :private => nil, "s-maxage" => 5.hours
render action: "hello_world"
end
def conditional_hello_with_expires_now
expires_now
render action: "hello_world"
end
def conditional_hello_with_cache_control_headers
response.headers["Cache-Control"] = "no-transform"
expires_now
render action: "hello_world"
end
def conditional_hello_with_expires_and_confliciting_cache_control_headers
response.headers["Cache-Control"] = "no-cache, must-revalidate"
expires_now
render action: "hello_world"
end
def conditional_hello_without_expires_and_confliciting_cache_control_headers
response.headers["Cache-Control"] = "no-cache, must-revalidate"
render action: "hello_world"
end
def conditional_hello_without_expires_and_public_header
response.headers["Cache-Control"] = "public, no-cache"
render action: "hello_world"
end
def conditional_hello_with_bangs
render action: "hello_world"
end
before_action :handle_last_modified_and_etags, only: :conditional_hello_with_bangs
def handle_last_modified_and_etags
fresh_when(last_modified: Time.now.utc.beginning_of_day, etag: [ :foo, 123 ], public: false, cache_control: { no_cache: true, public: true })
end
def head_created
head :created
end
def head_created_with_application_json_content_type
head :created, content_type: "application/json"
end
def head_ok_with_image_png_content_type
head :ok, content_type: "image/png"
end
def head_ok_with_string_key_content_type
head :ok, "Content-Type" => "application/pdf"
end
def head_with_location_header
head :ok, location: "/foo"
end
def head_with_location_object
head :ok, location: Customer.new("david", 1)
end
def head_with_symbolic_status
head params[:status].intern
end
def head_with_integer_status
head params[:status].to_i
end
def head_with_string_status
head params[:status]
end
def head_with_custom_header
head :ok, x_custom_header: "something"
end
def head_with_www_authenticate_header
head :ok, "WWW-Authenticate" => "something"
end
def head_with_status_code_first
head :forbidden, x_custom_header: "something"
end
def head_and_return
head(:ok) && return
raise "should not reach this line"
end
def head_with_no_content
# Fill in the headers with dummy data to make
# sure they get removed during the testing
response.headers["Content-Type"] = "dummy"
response.headers["Content-Length"] = 42
head 204
end
def head_default_content_type
# simulating path like "/1.foobar"
request.formats = []
respond_to do |format|
format.any { head 200 }
end
end
def cache_control_default_header_with_extras_partially_overridden_by_expires_in
response.headers["Cache-Control"] = "max-age=120, public, s-maxage=60, proxy-revalidate"
expires_in 300.seconds, public: true
render action: "hello_world"
end
def cache_control_no_store_overridden_by_expires_in
response.headers["Cache-Control"] = "no-store"
expires_in 60.seconds, public: true
render action: "hello_world"
end
def cache_control_no_store_overridden_by_expires_now
response.headers["Cache-Control"] = "no-store"
expires_now
render action: "hello_world"
end
private
def set_variable_for_layout
@variable_for_layout = nil
end
def determine_layout
case action_name
when "hello_world", "layout_test", "rendering_without_layout",
"rendering_nothing_on_layout", "render_text_hello_world",
"render_text_hello_world_with_layout",
"hello_world_with_layout_false",
"partial_only", "accessing_params_in_template",
"accessing_params_in_template_with_layout",
"render_with_explicit_template",
"render_with_explicit_string_template",
"update_page", "update_page_with_instance_variables"
"layouts/standard"
when "action_talk_to_layout", "layout_overriding_layout"
"layouts/talk_from_action"
when "render_implicit_html_template_from_xhr_request"
(request.xhr? ? "layouts/xhr" : "layouts/standard")
end
end
end
module TemplateModificationHelper
private
def modify_template(name)
hash = @controller.view_paths.first.instance_variable_get(:@hash)
key = name + ".erb"
original = hash[key]
hash[key] = "#{original} Modified!"
ActionView::LookupContext::DetailsKey.clear
yield
ensure
hash[key] = original
ActionView::LookupContext::DetailsKey.clear
end
end
class MetalTestController < ActionController::Metal
include AbstractController::Rendering
include ActionView::Rendering
include ActionController::Rendering
def accessing_logger_in_template
render inline: "<%= logger.class %>"
end
end
class ExpiresInRenderTest < ActionController::TestCase
tests TestController
def setup
super
ActionController::Base.view_paths.paths.each(&:clear_cache)
end
def test_dynamic_render_with_file
assert File.exist?(File.expand_path("../../test/abstract_unit.rb", __dir__))
assert_raises ArgumentError do
get :dynamic_render_with_file, params: { id: '../\\../test/abstract_unit.rb' }
end
end
def test_dynamic_render_with_absolute_path
file = Tempfile.new("name")
file.write "secrets!"
file.flush
assert_raises ActionView::MissingTemplate do
get :dynamic_render, params: { id: file.path }
end
ensure
file.close
file.unlink
end
def test_dynamic_render
assert File.exist?(File.expand_path("../../test/abstract_unit.rb", __dir__))
assert_raises ActionView::MissingTemplate do
get :dynamic_render, params: { id: '../\\../test/abstract_unit.rb' }
end
end
def test_permitted_dynamic_render_file_hash
assert File.exist?(File.expand_path("../../test/abstract_unit.rb", __dir__))
assert_raises ArgumentError do
get :dynamic_render_permit, params: { id: { file: '../\\../test/abstract_unit.rb' } }
end
end
def test_dynamic_render_file_hash
assert_raises ArgumentError do
get :dynamic_render, params: { id: { file: '../\\../test/abstract_unit.rb' } }
end
end
def test_expires_in_header
get :conditional_hello_with_expires_in
assert_equal "max-age=60, private", @response.headers["Cache-Control"]
end
def test_expires_in_header_with_public
get :conditional_hello_with_expires_in_with_public
assert_equal "max-age=60, public", @response.headers["Cache-Control"]
end
def test_expires_in_header_with_must_revalidate
get :conditional_hello_with_expires_in_with_must_revalidate
assert_equal "max-age=60, private, must-revalidate", @response.headers["Cache-Control"]
end
def test_expires_in_header_with_public_and_must_revalidate
get :conditional_hello_with_expires_in_with_public_and_must_revalidate
assert_equal "max-age=60, public, must-revalidate", @response.headers["Cache-Control"]
end
def test_expires_in_header_with_stale_while_revalidate
get :conditional_hello_with_expires_in_with_stale_while_revalidate
assert_equal "max-age=60, public, stale-while-revalidate=300", @response.headers["Cache-Control"]
end
def test_expires_in_header_with_stale_if_error
get :conditional_hello_with_expires_in_with_stale_if_error
assert_equal "max-age=60, public, stale-if-error=300", @response.headers["Cache-Control"]
end
def test_expires_in_header_with_additional_headers
get :conditional_hello_with_expires_in_with_public_with_more_keys
assert_equal "max-age=60, public, s-maxage=18000", @response.headers["Cache-Control"]
end
def test_expires_in_old_syntax
get :conditional_hello_with_expires_in_with_public_with_more_keys_old_syntax
assert_equal "max-age=60, public, s-maxage=18000", @response.headers["Cache-Control"]
end
def test_expires_now
get :conditional_hello_with_expires_now
assert_equal "no-cache", @response.headers["Cache-Control"]
end
def test_expires_now_with_cache_control_headers
get :conditional_hello_with_cache_control_headers
assert_match(/no-cache/, @response.headers["Cache-Control"])
assert_match(/no-transform/, @response.headers["Cache-Control"])
end
def test_expires_now_with_conflicting_cache_control_headers
get :conditional_hello_with_expires_and_confliciting_cache_control_headers
assert_equal "no-cache", @response.headers["Cache-Control"]
end
def test_no_expires_now_with_conflicting_cache_control_headers
get :conditional_hello_without_expires_and_confliciting_cache_control_headers
assert_equal "no-cache", @response.headers["Cache-Control"]
end
def test_no_expires_now_with_public
get :conditional_hello_without_expires_and_public_header
assert_equal "public, no-cache", @response.headers["Cache-Control"]
end
def test_date_header_when_expires_in
time = Time.mktime(2011, 10, 30)
Time.stub :now, time do
get :conditional_hello_with_expires_in
assert_equal Time.now.httpdate, @response.headers["Date"]
end
end
def test_cache_control_default_header_with_extras_partially_overridden_by_expires_in
get :cache_control_default_header_with_extras_partially_overridden_by_expires_in
assert_equal "max-age=300, public, s-maxage=60, proxy-revalidate", @response.headers["Cache-Control"]
end
def test_cache_control_no_store_overridden_by_expires_in
get :cache_control_no_store_overridden_by_expires_in
assert_equal "max-age=60, public", @response.headers["Cache-Control"]
end
def test_cache_control_no_store_overridden_by_expires_now
get :cache_control_no_store_overridden_by_expires_now
assert_equal "no-cache", @response.headers["Cache-Control"]
end
end
class LastModifiedRenderTest < ActionController::TestCase
tests TestController
def setup
super
@last_modified = Time.now.utc.beginning_of_day.httpdate
end
def test_responds_with_last_modified
get :conditional_hello
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_not_modified
@request.if_modified_since = @last_modified
get :conditional_hello
assert_equal 304, @response.status.to_i
assert_predicate @response.body, :blank?
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_not_modified_but_etag_differs
@request.if_modified_since = @last_modified
@request.if_none_match = '"234"'
get :conditional_hello
assert_response :success
end
def test_request_modified
@request.if_modified_since = "Thu, 16 Jul 2008 00:00:00 GMT"
get :conditional_hello
assert_equal 200, @response.status.to_i
assert_predicate @response.body, :present?
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_responds_with_custom_cache_control_headers
get :conditional_hello
assert_equal "no-cache", @response.headers["Cache-Control"]
end
def test_responds_with_last_modified_with_record
get :conditional_hello_with_record
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_not_modified_with_record
@request.if_modified_since = @last_modified
get :conditional_hello_with_record
assert_equal 304, @response.status.to_i
assert_predicate @response.body, :blank?
assert_not_nil @response.etag
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_not_modified_but_etag_differs_with_record
@request.if_modified_since = @last_modified
@request.if_none_match = '"234"'
get :conditional_hello_with_record
assert_response :success
end
def test_request_modified_with_record
@request.if_modified_since = "Thu, 16 Jul 2008 00:00:00 GMT"
get :conditional_hello_with_record
assert_equal 200, @response.status.to_i
assert_predicate @response.body, :present?
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_responds_with_last_modified_with_array_of_records
get :conditional_hello_with_array_of_records
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_not_modified_with_array_of_records
@request.if_modified_since = @last_modified
get :conditional_hello_with_array_of_records
assert_equal 304, @response.status.to_i
assert_predicate @response.body, :blank?
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_not_modified_but_etag_differs_with_array_of_records
@request.if_modified_since = @last_modified
@request.if_none_match = '"234"'
get :conditional_hello_with_array_of_records
assert_response :success
end
def test_request_modified_with_array_of_records
@request.if_modified_since = "Thu, 16 Jul 2008 00:00:00 GMT"
get :conditional_hello_with_array_of_records
assert_equal 200, @response.status.to_i
assert_predicate @response.body, :present?
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_responds_with_last_modified_with_collection_of_records
get :conditional_hello_with_collection_of_records
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_not_modified_with_collection_of_records
@request.if_modified_since = @last_modified
get :conditional_hello_with_collection_of_records
assert_equal 304, @response.status.to_i
assert_predicate @response.body, :blank?
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_not_modified_but_etag_differs_with_collection_of_records
@request.if_modified_since = @last_modified
@request.if_none_match = '"234"'
get :conditional_hello_with_collection_of_records
assert_response :success
end
def test_request_modified_with_collection_of_records
@request.if_modified_since = "Thu, 16 Jul 2008 00:00:00 GMT"
get :conditional_hello_with_collection_of_records
assert_equal 200, @response.status.to_i
assert_predicate @response.body, :present?
assert_equal @last_modified, @response.headers["Last-Modified"]
end
def test_request_with_bang_gets_last_modified
get :conditional_hello_with_bangs
assert_equal @last_modified, @response.headers["Last-Modified"]
assert_response :success
end
def test_request_with_bang_obeys_last_modified
@request.if_modified_since = @last_modified
get :conditional_hello_with_bangs
assert_response :not_modified
end
def test_last_modified_works_with_less_than_too
@request.if_modified_since = 5.years.ago.httpdate
get :conditional_hello_with_bangs
assert_response :success
end
def test_last_modified_with_custom_cache_control_headers
get :conditional_hello_with_bangs
assert_equal "public, no-cache", @response.headers["Cache-Control"]
assert_response :success
end
end
class EtagRenderTest < ActionController::TestCase
tests TestControllerWithExtraEtags
include TemplateModificationHelper
def test_strong_etag
@request.if_none_match = strong_etag(["strong", "ab", :cde, [:f]])
get :strong
assert_response :not_modified
@request.if_none_match = "*"
get :strong
assert_response :not_modified
@request.if_none_match = '"strong"'
get :strong
assert_response :ok
@request.if_none_match = weak_etag(["strong", "ab", :cde, [:f]])
get :strong
assert_response :ok
end
def test_multiple_etags
@request.if_none_match = weak_etag(["123", "ab", :cde, [:f]])
get :fresh
assert_response :not_modified
@request.if_none_match = %("nomatch")
get :fresh
assert_response :success
end
def test_array
@request.if_none_match = weak_etag([%w(1 2 3), "ab", :cde, [:f]])
get :array
assert_response :not_modified
@request.if_none_match = %("nomatch")
get :array
assert_response :success
end
def test_etag_reflects_template_digest
get :with_template
assert_response :ok
assert_not_nil etag = @response.etag
request.if_none_match = etag
get :with_template
assert_response :not_modified
modify_template("test/hello_world") do
request.if_none_match = etag
get :with_template
assert_response :ok
assert_not_equal etag, @response.etag
end
end
def test_etag_reflects_implicit_template_digest
get :with_implicit_template
assert_response :ok
assert_not_nil etag = @response.etag
request.if_none_match = etag
get :with_implicit_template
assert_response :not_modified
modify_template("test/with_implicit_template") do
request.if_none_match = etag
get :with_implicit_template
assert_response :ok
assert_not_equal etag, @response.etag
end
end
private
def weak_etag(record)
"W/#{strong_etag record}"
end
def strong_etag(record)
%("#{ActiveSupport::Digest.hexdigest(ActiveSupport::Cache.expand_cache_key(record))}")
end
end
class NamespacedEtagRenderTest < ActionController::TestCase
tests Namespaced::ImplicitRenderTestController
include TemplateModificationHelper
def test_etag_reflects_template_digest
get :hello_world
assert_response :ok
assert_not_nil etag = @response.etag
request.if_none_match = etag
get :hello_world
assert_response :not_modified
modify_template("namespaced/implicit_render_test/hello_world") do
request.if_none_match = etag
get :hello_world
assert_response :ok
assert_not_equal etag, @response.etag
end
end
end
class InheritedEtagRenderTest < ActionController::TestCase
tests InheritedRenderTestController
include TemplateModificationHelper
def test_etag_reflects_template_digest
get :hello_world
assert_response :ok
assert_not_nil etag = @response.etag
request.if_none_match = etag
get :hello_world
assert_response :not_modified
modify_template("implicit_render_test/hello_world") do
request.if_none_match = etag
get :hello_world
assert_response :ok
assert_not_equal etag, @response.etag
end
end
end
class MetalRenderTest < ActionController::TestCase
tests MetalTestController
def test_access_to_logger_in_view
get :accessing_logger_in_template
assert_equal "NilClass", @response.body
end
end
class ActionControllerRenderTest < ActionController::TestCase
class MinimalController < ActionController::Metal
include AbstractController::Rendering
include ActionController::Rendering
end
def test_direct_render_to_string_with_body
mc = MinimalController.new
assert_equal "Hello world!", mc.render_to_string(body: ["Hello world!"])
end
end
class ActionControllerBaseRenderTest < ActionController::TestCase
def test_direct_render_to_string
ac = ActionController::Base.new()
assert_equal "Hello world!", ac.render_to_string(template: "test/hello_world")
end
end
class ImplicitRenderTest < ActionController::TestCase
tests ImplicitRenderTestController
def test_implicit_no_content_response_as_browser
assert_raises(ActionController::MissingExactTemplate) do
get :empty_action
end
end
def test_implicit_no_content_response_as_xhr
get :empty_action, xhr: true
assert_response :no_content
end
def test_implicit_success_response_with_right_format
get :empty_action_with_template
assert_equal "<h1>Empty action rendered this implicitly.</h1>\n", @response.body
assert_response :success
end
def test_implicit_unknown_format_response
assert_raises(ActionController::UnknownFormat) do
get :empty_action_with_template, format: "json"
end
end
end
class HeadRenderTest < ActionController::TestCase
tests TestController
def setup
@request.host = "www.nextangle.com"
end
def test_head_created
post :head_created
assert_predicate @response.body, :blank?
assert_response :created
end
def test_head_created_with_application_json_content_type
post :head_created_with_application_json_content_type
assert_predicate @response.body, :blank?
assert_equal "application/json", @response.headers["Content-Type"]
assert_response :created
end
def test_head_ok_with_image_png_content_type
post :head_ok_with_image_png_content_type
assert_predicate @response.body, :blank?
assert_equal "image/png", @response.headers["Content-Type"]
assert_response :ok
end
def test_head_respect_string_content_type
get :head_ok_with_string_key_content_type
assert_equal "application/pdf", @response.headers["Content-Type"]
end
def test_head_with_location_header
get :head_with_location_header
assert_predicate @response.body, :blank?
assert_equal "/foo", @response.headers["Location"]
assert_response :ok
end
def test_head_with_location_object
with_routing do |set|
set.draw do
resources :customers
ActionDispatch.deprecator.silence do
get ":controller/:action"
end
end
get :head_with_location_object
assert_predicate @response.body, :blank?
assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
assert_response :ok
end
end
def test_head_with_custom_header
get :head_with_custom_header
assert_predicate @response.body, :blank?
assert_equal "something", @response.headers["X-Custom-Header"]
assert_response :ok
end
def test_head_with_www_authenticate_header
get :head_with_www_authenticate_header
assert_predicate @response.body, :blank?
assert_equal "something", @response.headers["WWW-Authenticate"]
assert_response :ok
end
def test_head_with_symbolic_status
get :head_with_symbolic_status, params: { status: "ok" }
assert_equal 200, @response.status
assert_response :ok
get :head_with_symbolic_status, params: { status: "not_found" }
assert_equal 404, @response.status
assert_response :not_found
get :head_with_symbolic_status, params: { status: "no_content" }
assert_equal 204, @response.status
assert_not_includes @response.headers, "Content-Length"
assert_response :no_content
Rack::Utils::SYMBOL_TO_STATUS_CODE.each do |status, code|
get :head_with_symbolic_status, params: { status: status.to_s }
assert_equal code, @response.response_code
assert_response status
end
end
def test_head_with_integer_status
Rack::Utils::HTTP_STATUS_CODES.each do |code, message|
get :head_with_integer_status, params: { status: code.to_s }
assert_equal message, @response.message
end
end
def test_head_with_no_content
get :head_with_no_content
assert_equal 204, @response.status
assert_nil @response.headers["Content-Type"]
assert_nil @response.headers["Content-Length"]
end
def test_head_with_string_status
get :head_with_string_status, params: { status: "404 Eat Dirt" }
assert_equal 404, @response.response_code
assert_equal "Not Found", @response.message
assert_response :not_found
end
def test_head_with_status_code_first
get :head_with_status_code_first
assert_equal 403, @response.response_code
assert_equal "Forbidden", @response.message
assert_equal "something", @response.headers["X-Custom-Header"]
assert_response :forbidden
end
def test_head_returns_truthy_value
assert_nothing_raised do
get :head_and_return
end
end
def test_head_default_content_type
post :head_default_content_type
assert_equal "text/html", @response.headers["Content-Type"]
end
end
class LiveTestController < ActionController::Base
include ActionController::Live
def test_action
head :ok
end
end
class LiveHeadRenderTest < ActionController::TestCase
tests LiveTestController
def setup
super
def @controller.new_controller_thread(&block)
Thread.new(&block)
end
def @controller.response_body=(body)
super
sleep 0.1
end
end
def test_live_head_ok
get :test_action, format: "json"
@response.stream.on_error { flunk "action should not raise any errors" }
sleep 0.2
pass
end
end
class HttpCacheForeverTest < ActionController::TestCase
class HttpCacheForeverController < ActionController::Base
def cache_me_forever
http_cache_forever(public: params[:public]) do
render plain: "hello"
end
end
end
tests HttpCacheForeverController
def test_cache_with_public
get :cache_me_forever, params: { public: true }
assert_response :ok
assert_equal "max-age=#{100.years}, public", @response.headers["Cache-Control"]
assert_not_nil @response.etag
assert_predicate @response, :weak_etag?
end
def test_cache_with_private
get :cache_me_forever
assert_response :ok
assert_equal "max-age=#{100.years}, private", @response.headers["Cache-Control"]
assert_not_nil @response.etag
assert_predicate @response, :weak_etag?
end
def test_cache_response_code_with_if_modified_since
get :cache_me_forever
assert_response :ok
@request.if_modified_since = @response.headers["Last-Modified"]
get :cache_me_forever
assert_response :not_modified
end
def test_cache_response_code_with_etag
get :cache_me_forever
assert_response :ok
@request.if_none_match = @response.etag
get :cache_me_forever
assert_response :not_modified
end
end
class HttpCacheNoStoreTest < ActionController::TestCase
class HttpCacheNoStoreController < ActionController::Base
def standalone_no_store_call
no_store
render plain: "hello world"
end
before_action(only: :no_store_overridden_by_expires_in) { no_store }
def no_store_overridden_by_expires_in
expires_in 30.seconds
render plain: "hello world"
end
before_action(only: :expires_in_overridden_by_no_store) { expires_in 30.seconds }
def expires_in_overridden_by_no_store
no_store
render plain: "hello world"
end
before_action(only: :no_store_overridden_by_fresh_when) { no_store }
def no_store_overridden_by_fresh_when
fresh_when(etag: "123abc")
render plain: "hello world"
end
before_action(only: :fresh_when_overridden_by_no_store) { fresh_when etag: "abc123" }
def fresh_when_overridden_by_no_store
no_store
render plain: "hello world"
end
before_action(only: :expires_now_overridden_by_no_store) { expires_now }
def expires_now_overridden_by_no_store
no_store
render plain: "hello world"
end
before_action(only: :no_store_overridden_by_expires_now) { no_store }
def no_store_overridden_by_expires_now
expires_now
render plain: "hello world"
end
def cache_control_no_cache_overridden_by_no_store
response.headers["Cache-Control"] = "no-cache"
no_store
render plain: "hello world"
end
def cache_control_public_with_max_age_overridden_by_no_store
response.headers["Cache-Control"] = "public, max-age=604800"
no_store
render plain: "hello world"
end
end
tests HttpCacheNoStoreController
def test_standalone_no_store_call
get :standalone_no_store_call
assert_equal "no-store", @response.headers["Cache-Control"]
end
def test_no_store_overridden_by_expires_in
get :no_store_overridden_by_expires_in
assert_equal "max-age=30, private", @response.headers["Cache-Control"]
end
def test_expires_in_overridden_by_no_store
get :expires_in_overridden_by_no_store
assert_equal "no-store", @response.headers["Cache-Control"]
end
def test_no_store_overridden_by_fresh_when
get :no_store_overridden_by_fresh_when
assert_equal "max-age=0, private, must-revalidate", @response.headers["Cache-Control"]
end
def test_fresh_when_overridden_by_no_store
get :fresh_when_overridden_by_no_store
assert_equal "no-store", @response.headers["Cache-Control"]
end
def test_expires_now_overridden_by_no_store
get :expires_now_overridden_by_no_store
assert_equal "no-store", @response.headers["Cache-Control"]
end
def test_no_store_overridden_by_expires_now
get :no_store_overridden_by_expires_now
assert_equal "no-cache", @response.headers["Cache-Control"]
end
def test_cache_control_no_cache_header_can_be_overridden_by_no_store
get :cache_control_no_cache_overridden_by_no_store
assert_equal "no-store", @response.headers["Cache-Control"]
end
def test_cache_control_public_with_expiration_header_can_be_overridden_by_no_store
get :cache_control_public_with_max_age_overridden_by_no_store
assert_equal "no-store", @response.headers["Cache-Control"]
end
end
|