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
|
require_relative 'test_helper'
require 'timeout'
require 'rack/cache/context'
describe Rack::Cache::Context do
before { setup_cache_context }
after { teardown_cache_context }
it 'passes options to the underlying stores' do
app = CacheContextHelpers::FakeApp.new(200, {}, ['foo'])
context = Rack::Cache::Context.new(app, foo: 'bar')
entity_options = context.entitystore.instance_variable_get('@options')
meta_options = context.metastore.instance_variable_get('@options')
entity_options[:foo].must_equal('bar')
meta_options[:foo].must_equal('bar')
end
it 'passes on non-GET/HEAD requests' do
respond_with 200
post '/'
assert app.called?
assert response.ok?
cache.trace.must_include :pass
response.headers.wont_include 'age'
end
it 'passes on rack-cache.force-pass' do
respond_with 200
get '/', {"rack-cache.force-pass" => true}
assert app.called?
assert response.ok?
cache.trace.must_equal [:pass]
response.headers.wont_include 'age'
end
it "passes on options requests" do
respond_with 200
request "options", '/'
assert app.called?
assert response.ok?
cache.trace.must_include :pass
end
it "doesnt invalidate on options requests" do
respond_with 200
request "options", '/'
assert app.called?
assert response.ok?
cache.trace.wont_include :invalidate
end
%w[post put delete].each do |request_method|
it "invalidates on #{request_method} requests" do
respond_with 200
request request_method, '/'
assert app.called?
assert response.ok?
cache.trace.must_include :invalidate
cache.trace.must_include :pass
end
end
it 'does not cache with Authorization request header and non public response' do
respond_with 200, 'etag' => '"FOO"'
get '/', 'HTTP_AUTHORIZATION' => 'basic foobarbaz'
assert app.called?
assert response.ok?
response.headers['cache-control'].must_equal 'private'
cache.trace.must_include :miss
cache.trace.wont_include :store
response.headers.wont_include 'age'
end
it 'does cache with Authorization request header and public response' do
respond_with 200, 'cache-control' => 'public', 'etag' => '"FOO"'
get '/', 'HTTP_AUTHORIZATION' => 'basic foobarbaz'
assert app.called?
assert response.ok?
cache.trace.must_include :miss
cache.trace.must_include :store
cache.trace.wont_include :ignore
response.headers.must_include 'age'
response.headers['cache-control'].must_equal 'public'
end
it 'does not cache with Cookie header and non public response' do
respond_with 200, 'etag' => '"FOO"'
get '/', 'HTTP_COOKIE' => 'foo=bar'
assert app.called?
assert response.ok?
response.headers['cache-control'].must_equal 'private'
cache.trace.must_include :miss
cache.trace.wont_include :store
response.headers.wont_include 'age'
end
it 'does not cache requests with a Cookie header' do
respond_with 200
get '/', 'HTTP_COOKIE' => 'foo=bar'
assert response.ok?
assert app.called?
cache.trace.must_include :miss
cache.trace.wont_include :store
response.headers.wont_include 'age'
response.headers['cache-control'].must_equal 'private'
end
it 'does remove set-cookie response header from a cacheable response' do
respond_with 200, 'cache-control' => 'public', 'etag' => '"FOO"', 'set-cookie' => 'TestCookie=OK'
get '/'
assert app.called?
assert response.ok?
cache.trace.must_include :store
cache.trace.must_include :ignore
response.headers['set-cookie'].must_be_nil
end
it 'does remove all configured ignore_headers from a cacheable response' do
respond_with 200, 'cache-control' => 'public', 'etag' => '"FOO"', 'set-cookie' => 'TestCookie=OK', 'x-strip-me' => 'Secret'
get '/', 'rack-cache.ignore_headers' => ['set-cookie', 'x-strip-me']
assert app.called?
assert response.ok?
cache.trace.must_include :store
cache.trace.must_include :ignore
response.headers['set-cookie'].must_be_nil
response.headers['x-strip-me'].must_be_nil
end
it 'does not remove set-cookie response header from a private response' do
respond_with 200, 'cache-control' => 'private', 'set-cookie' => 'TestCookie=OK'
get '/'
assert app.called?
assert response.ok?
cache.trace.wont_include :store
cache.trace.wont_include :ignore
response.headers['set-cookie'].must_equal 'TestCookie=OK'
end
it 'responds with 304 when if-modified-since matches last-modified' do
timestamp = Time.now.httpdate
respond_with do |req,res|
res.status = 200
res['last-modified'] = timestamp
res['content-type'] = 'text/plain'
res.body = ['Hello World']
end
get '/',
'HTTP_IF_MODIFIED_SINCE' => timestamp
assert app.called?
response.status.must_equal 304
response.original_headers.wont_include 'content-length'
response.original_headers.wont_include 'content-type'
assert response.body.empty?
cache.trace.must_include :miss
cache.trace.must_include :store
end
it 'responds with 304 when if-none-match matches etag' do
respond_with do |req,res|
res.status = 200
res['etag'] = '12345'
res['content-type'] = 'text/plain'
res.body = ['Hello World']
end
get '/',
'HTTP_IF_NONE_MATCH' => '12345'
assert app.called?
response.status.must_equal 304
response.original_headers.wont_include 'content-length'
response.original_headers.wont_include 'content-type'
response.headers.must_include 'etag'
assert response.body.empty?
cache.trace.must_include :miss
cache.trace.must_include :store
end
it 'responds with 304 only if if-none-match and if-modified-since both match' do
timestamp = Time.now
respond_with do |req,res|
res.status = 200
res['etag'] = '12345'
res['last-modified'] = timestamp.httpdate
res['content-type'] = 'text/plain'
res.body = ['Hello World']
end
# Only etag matches
get '/',
'HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => (timestamp - 1).httpdate
assert app.called?
response.status.must_equal 200
# Only last-modified matches
get '/',
'HTTP_IF_NONE_MATCH' => '1234', 'HTTP_IF_MODIFIED_SINCE' => timestamp.httpdate
assert app.called?
response.status.must_equal 200
# Both matches
get '/',
'HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => timestamp.httpdate
assert app.called?
response.status.must_equal 304
end
it 'validates private responses cached on the client' do
respond_with do |req,res|
etags = req.env['HTTP_IF_NONE_MATCH'].to_s.split(/\s*,\s*/)
if req.env['HTTP_COOKIE'] == 'authenticated'
res['cache-control'] = 'private, no-store'
res['etag'] = '"private tag"'
if etags.include?('"private tag"')
res.status = 304
else
res.status = 200
res['content-type'] = 'text/plain'
res.body = ['private data']
end
else
res['etag'] = '"public tag"'
if etags.include?('"public tag"')
res.status = 304
else
res.status = 200
res['content-type'] = 'text/plain'
res.body = ['public data']
end
end
end
get '/'
assert app.called?
response.status.must_equal 200
response.headers['etag'].must_equal '"public tag"'
response.body.must_equal 'public data'
cache.trace.must_include :miss
cache.trace.must_include :store
get '/', 'HTTP_COOKIE' => 'authenticated'
assert app.called?
response.status.must_equal 200
response.headers['etag'].must_equal '"private tag"'
response.body.must_equal 'private data'
cache.trace.must_include :stale
cache.trace.must_include :invalid
cache.trace.wont_include :store
get '/',
'HTTP_IF_NONE_MATCH' => '"public tag"'
assert app.called?
response.status.must_equal 304
response.headers['etag'].must_equal '"public tag"'
cache.trace.must_include :stale
cache.trace.must_include :valid
cache.trace.must_include :store
get '/',
'HTTP_IF_NONE_MATCH' => '"private tag"',
'HTTP_COOKIE' => 'authenticated'
assert app.called?
response.status.must_equal 304
response.headers['etag'].must_equal '"private tag"'
cache.trace.must_include :valid
cache.trace.wont_include :store
end
it 'stores responses when no-cache request directive present' do
respond_with 200, 'expires' => (Time.now + 5).httpdate
get '/', 'HTTP_CACHE_CONTROL' => 'no-cache'
assert response.ok?
cache.trace.must_include :store
response.headers.must_include 'age'
end
it 'reloads responses when cache hits but no-cache request directive present ' +
'when allow_reload is set true' do
count = 0
respond_with 200, 'cache-control' => 'max-age=10000' do |req,res|
count+= 1
res.body = (count == 1) ? ['Hello World'] : ['Goodbye World']
end
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :store
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :fresh
get '/',
'rack-cache.allow_reload' => true,
'HTTP_CACHE_CONTROL' => 'no-cache'
assert response.ok?
response.body.must_equal 'Goodbye World'
cache.trace.must_include :reload
cache.trace.must_include :store
end
it 'does not reload responses when allow_reload is set false (default)' do
count = 0
respond_with 200, 'cache-control' => 'max-age=10000' do |req,res|
count+= 1
res.body = (count == 1) ? ['Hello World'] : ['Goodbye World']
end
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :store
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :fresh
get '/',
'rack-cache.allow_reload' => false,
'HTTP_CACHE_CONTROL' => 'no-cache'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.wont_include :reload
# test again without explicitly setting the allow_reload option to false
get '/',
'HTTP_CACHE_CONTROL' => 'no-cache'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.wont_include :reload
end
it 'revalidates fresh cache entry when max-age request directive is exceeded ' +
'when allow_revalidate option is set true' do
count = 0
respond_with do |req,res|
count+= 1
res['cache-control'] = 'max-age=10000'
res['etag'] = count.to_s
res.body = (count == 1) ? ['Hello World'] : ['Goodbye World']
end
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :store
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :fresh
get '/',
'rack-cache.allow_revalidate' => true,
'HTTP_CACHE_CONTROL' => 'max-age=0'
assert response.ok?
response.body.must_equal 'Goodbye World'
cache.trace.must_include :stale
cache.trace.must_include :invalid
cache.trace.must_include :store
end
it 'returns a stale cache entry when max-age request directive is exceeded ' +
'when allow_revalidate and fault_tolerant options are set to true and ' +
'the remote server returns a connection error' do
count = 0
respond_with do |req, res|
count += 1
raise Timeout::Error, 'Connection failed' if count == 2
res['cache-control'] = 'max-age=10000'
res['etag'] = count.to_s
res.body = (count == 1) ? ['Hello World'] : ['Goodbye World']
end
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :store
get '/',
'rack-cache.allow_revalidate' => true,
'rack-cache.fault_tolerant' => true,
'HTTP_CACHE_CONTROL' => 'max-age=0'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :stale
cache.trace.must_include :connnection_failed
# Once the server comes back, the request should be revalidated.
get '/',
'rack-cache.allow_revalidate' => true,
'HTTP_CACHE_CONTROL' => 'max-age=0'
assert response.ok?
response.body.must_equal 'Goodbye World'
cache.trace.must_include :stale
cache.trace.must_include :invalid
cache.trace.must_include :store
end
it 'does not revalidate fresh cache entry when enable_revalidate option is set false (default)' do
count = 0
respond_with do |req,res|
count+= 1
res['cache-control'] = 'max-age=10000'
res['etag'] = count.to_s
res.body = (count == 1) ? ['Hello World'] : ['Goodbye World']
end
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :store
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :fresh
get '/',
'rack-cache.allow_revalidate' => false,
'HTTP_CACHE_CONTROL' => 'max-age=0'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.wont_include :stale
cache.trace.wont_include :invalid
cache.trace.must_include :fresh
# test again without explicitly setting the allow_revalidate option to false
get '/',
'HTTP_CACHE_CONTROL' => 'max-age=0'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.wont_include :stale
cache.trace.wont_include :invalid
cache.trace.must_include :fresh
end
it 'fetches response from backend when cache misses' do
respond_with 200, 'expires' => (Time.now + 5).httpdate
get '/'
assert response.ok?
cache.trace.must_include :miss
response.headers.must_include 'age'
end
[(201..202),(204..206),(303..305),(400..403),(405..409),(411..417),(500..505)].each do |range|
range.each do |response_code|
it "does not cache #{response_code} responses" do
respond_with response_code, 'expires' => (Time.now + 5).httpdate
get '/'
cache.trace.wont_include :store
response.status.must_equal response_code
response.headers.wont_include 'age'
end
end
end
it "does not cache responses with explicit no-store directive" do
respond_with 200,
'expires' => (Time.now + 5).httpdate,
'cache-control' => 'no-store'
get '/'
assert response.ok?
cache.trace.wont_include :store
response.headers.wont_include 'age'
end
it 'does not cache responses without freshness information or a validator' do
respond_with 200
get '/'
assert response.ok?
cache.trace.wont_include :store
end
it "caches responses with explicit no-cache directive" do
respond_with 200,
'expires' => (Time.now + 5).httpdate,
'cache-control' => 'no-cache'
get '/'
assert response.ok?
cache.trace.must_include :store
response.headers.must_include 'age'
end
it 'caches responses with an Expiration header' do
respond_with 200, 'expires' => (Time.now + 5).httpdate
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
response.headers.must_include 'date'
refute response['age'].nil?
refute response['x-content-digest'].nil?
cache.trace.must_include :miss
cache.trace.must_include :store
cache.metastore.to_hash.keys.length.must_equal 1
end
it 'caches responses with a max-age directive' do
respond_with 200, 'cache-control' => 'max-age=5'
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
response.headers.must_include 'date'
refute response['age'].nil?
refute response['x-content-digest'].nil?
cache.trace.must_include :miss
cache.trace.must_include :store
cache.metastore.to_hash.keys.length.must_equal 1
end
it 'caches responses with a s-maxage directive' do
respond_with 200, 'cache-control' => 's-maxage=5'
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
response.headers.must_include 'date'
refute response['age'].nil?
refute response['x-content-digest'].nil?
cache.trace.must_include :miss
cache.trace.must_include :store
cache.metastore.to_hash.keys.length.must_equal 1
end
it 'caches responses with a last-modified validator but no freshness information' do
respond_with 200, 'last-modified' => Time.now.httpdate
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :miss
cache.trace.must_include :store
end
it 'caches responses with an etag validator but no freshness information' do
respond_with 200, 'etag' => '"123456"'
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :miss
cache.trace.must_include :store
end
it 'hits cached response with expires header' do
respond_with 200,
'date' => (Time.now - 5).httpdate,
'expires' => (Time.now + 5).httpdate
get '/'
assert app.called?
assert response.ok?
response.headers.must_include 'date'
cache.trace.must_include :miss
cache.trace.must_include :store
response.body.must_equal 'Hello World'
get '/'
assert response.ok?
refute app.called?
response['date'].must_equal responses.first['date']
response['age'].to_i.must_be :>, 0
refute response['x-content-digest'].nil?
cache.trace.must_include :fresh
cache.trace.wont_include :store
response.body.must_equal 'Hello World'
end
it 'hits cached response with max-age directive' do
respond_with 200,
'date' => (Time.now - 5).httpdate,
'cache-control' => 'max-age=10'
get '/'
assert app.called?
assert response.ok?
response.headers.must_include 'date'
cache.trace.must_include :miss
cache.trace.must_include :store
response.body.must_equal 'Hello World'
get '/'
assert response.ok?
refute app.called?
response['date'].must_equal responses.first['date']
response['age'].to_i.must_be :>, 0
refute response['x-content-digest'].nil?
cache.trace.must_include :fresh
cache.trace.wont_include :store
response.body.must_equal 'Hello World'
end
it 'hits cached response with s-maxage directive' do
respond_with 200,
'date' => (Time.now - 5).httpdate,
'cache-control' => 's-maxage=10, max-age=0'
get '/'
assert app.called?
assert response.ok?
response.headers.must_include 'date'
cache.trace.must_include :miss
cache.trace.must_include :store
response.body.must_equal 'Hello World'
get '/'
assert response.ok?
refute app.called?
response['date'].must_equal responses.first['date']
response['age'].to_i.must_be :>, 0
refute response['x-content-digest'].nil?
cache.trace.must_include :fresh
cache.trace.wont_include :store
response.body.must_equal 'Hello World'
end
it 'assigns default_ttl when response has no freshness information' do
respond_with 200
get '/', 'rack-cache.default_ttl' => 10
assert app.called?
assert response.ok?
cache.trace.must_include :miss
cache.trace.must_include :store
response.body.must_equal 'Hello World'
response['cache-control'].must_include 's-maxage=10'
get '/', 'rack-cache.default_ttl' => 10
assert response.ok?
refute app.called?
cache.trace.must_include :fresh
cache.trace.wont_include :store
response.body.must_equal 'Hello World'
end
it 'does not assign default_ttl when response has must-revalidate directive' do
respond_with 200,
'cache-control' => 'must-revalidate'
get '/', 'rack-cache.default_ttl' => 10
assert app.called?
assert response.ok?
cache.trace.must_include :miss
cache.trace.wont_include :store
response['cache-control'].wont_include 's-maxage'
response.body.must_equal 'Hello World'
end
it 'fetches full response when cache stale and no validators present' do
respond_with 200, 'expires' => (Time.now + 5).httpdate
# build initial request
get '/'
assert app.called?
assert response.ok?
response.headers.must_include 'date'
response.headers.must_include 'x-content-digest'
response.headers.must_include 'age'
cache.trace.must_include :miss
cache.trace.must_include :store
response.body.must_equal 'Hello World'
# go in and play around with the cached metadata directly ...
# XXX find some other way to do this
hash = cache.metastore.to_hash
hash.values.length.must_equal 1
entries = Marshal.load(hash.values.first)
entries.length.must_equal 1
req, res = entries.first
res['expires'] = (Time.now - 1).httpdate
hash[hash.keys.first] = Marshal.dump([[req, res]])
# build subsequent request; should be found but miss due to freshness
get '/'
assert app.called?
assert response.ok?
response['age'].to_i.must_equal 0
response.headers.must_include 'x-content-digest'
cache.trace.must_include :stale
cache.trace.wont_include :fresh
cache.trace.wont_include :miss
cache.trace.must_include :store
response.body.must_equal 'Hello World'
end
it 'validates cached responses with last-modified and no freshness information' do
timestamp = Time.now.httpdate
respond_with do |req,res|
res['last-modified'] = timestamp
if req.env['HTTP_IF_MODIFIED_SINCE'] == timestamp
res.status = 304
res.body = []
end
end
# build initial request
get '/'
assert app.called?
assert response.ok?
response.headers.must_include 'last-modified'
response.headers.must_include 'x-content-digest'
response.body.must_equal 'Hello World'
cache.trace.must_include :miss
cache.trace.must_include :store
cache.trace.wont_include :stale
# build subsequent request; should be found but miss due to freshness
get '/'
assert app.called?
assert response.ok?
response.headers.must_include 'last-modified'
response.headers.must_include 'x-content-digest'
response['age'].to_i.must_equal 0
response.body.must_equal 'Hello World'
cache.trace.must_include :stale
cache.trace.must_include :valid
cache.trace.must_include :store
cache.trace.wont_include :miss
end
it 'validates cached responses with etag and no freshness information' do
timestamp = Time.now.httpdate
respond_with do |req,res|
res['ETAG'] = '"12345"'
if req.env['HTTP_IF_NONE_MATCH'] == res['etag']
res.status = 304
res.body = []
end
end
# build initial request
get '/'
assert app.called?
assert response.ok?
response.headers.must_include 'etag'
response.headers.must_include 'x-content-digest'
response.body.must_equal 'Hello World'
cache.trace.must_include :miss
cache.trace.must_include :store
# build subsequent request; should be found but miss due to freshness
get '/'
assert app.called?
assert response.ok?
response.headers.must_include 'etag'
response.headers.must_include 'x-content-digest'
response['age'].to_i.must_equal 0
response.body.must_equal 'Hello World'
cache.trace.must_include :stale
cache.trace.must_include :valid
cache.trace.must_include :store
cache.trace.wont_include :miss
end
it 'replaces cached responses when validation results in non-304 response' do
timestamp = Time.now.httpdate
count = 0
respond_with do |req,res|
res['last-modified'] = timestamp
case (count+=1)
when 1 ; res.body = ['first response']
when 2 ; res.body = ['second response']
when 3
res.body = []
res.status = 304
end
end
# first request should fetch from backend and store in cache
get '/'
response.status.must_equal 200
response.body.must_equal 'first response'
# second request is validated, is invalid, and replaces cached entry
get '/'
response.status.must_equal 200
response.body.must_equal 'second response'
# third respone is validated, valid, and returns cached entry
get '/'
response.status.must_equal 200
response.body.must_equal 'second response'
count.must_equal 3
end
it 'stores HEAD as original_method on HEAD requests' do
respond_with do |req,res|
res.status = 200
res.body = []
req.request_method.must_equal 'GET'
req.env['rack.methodoverride.original_method'].must_equal 'HEAD'
end
head '/'
assert app.called?
response.body.must_equal ''
end
it 'passes HEAD requests through directly on pass' do
respond_with do |req,res|
res.status = 200
res.body = []
req.request_method.must_equal 'HEAD'
end
head '/', 'HTTP_EXPECT' => 'something ...'
assert app.called?
response.body.must_equal ''
end
it 'uses cache to respond to HEAD requests when fresh' do
respond_with do |req,res|
res['cache-control'] = 'max-age=10'
res.body = ['Hello World']
req.request_method.wont_equal 'HEAD'
end
get '/'
assert app.called?
response.status.must_equal 200
response.body.must_equal 'Hello World'
head '/'
refute app.called?
response.status.must_equal 200
response.body.must_equal ''
response['content-length'].must_equal 'Hello World'.length.to_s
end
it 'invalidates cached responses on POST' do
respond_with do |req,res|
if req.request_method == 'GET'
res.status = 200
res['cache-control'] = 'public, max-age=500'
res.body = ['Hello World']
elsif req.request_method == 'POST'
res.status = 303
res['Location'] = '/'
res.headers.delete('cache-control')
res.body = []
end
end
# build initial request to enter into the cache
get '/'
assert app.called?
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :miss
cache.trace.must_include :store
# make sure it is valid
get '/'
refute app.called?
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :fresh
# now POST to same URL
post '/'
assert app.called?
assert response.redirect?
response['Location'].must_equal '/'
cache.trace.must_include :invalidate
cache.trace.must_include :pass
response.body.must_equal ''
# now make sure it was actually invalidated
get '/'
assert app.called?
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :stale
cache.trace.must_include :invalid
cache.trace.must_include :store
end
describe 'with responses that include a Vary header' do
before do
count = 0
respond_with 200 do |req,res|
res['vary'] = 'accept user-agent Foo'
res['cache-control'] = 'max-age=10'
res['x-response-count'] = (count+=1).to_s
res.body = [req.env['HTTP_USER_AGENT']]
end
end
it 'serves from cache when headers match' do
get '/',
'HTTP_ACCEPT' => 'text/html',
'HTTP_USER_AGENT' => 'Bob/1.0'
assert response.ok?
response.body.must_equal 'Bob/1.0'
cache.trace.must_include :miss
cache.trace.must_include :store
get '/',
'HTTP_ACCEPT' => 'text/html',
'HTTP_USER_AGENT' => 'Bob/1.0'
assert response.ok?
response.body.must_equal 'Bob/1.0'
cache.trace.must_include :fresh
cache.trace.wont_include :store
response.headers.must_include 'x-content-digest'
end
it 'stores multiple responses when headers differ' do
get '/',
'HTTP_ACCEPT' => 'text/html',
'HTTP_USER_AGENT' => 'Bob/1.0'
assert response.ok?
response.body.must_equal 'Bob/1.0'
response['x-response-count'].must_equal '1'
get '/',
'HTTP_ACCEPT' => 'text/html',
'HTTP_USER_AGENT' => 'Bob/2.0'
cache.trace.must_include :miss
cache.trace.must_include :store
response.body.must_equal 'Bob/2.0'
response['x-response-count'].must_equal '2'
get '/',
'HTTP_ACCEPT' => 'text/html',
'HTTP_USER_AGENT' => 'Bob/1.0'
cache.trace.must_include :fresh
response.body.must_equal 'Bob/1.0'
response['x-response-count'].must_equal '1'
get '/',
'HTTP_ACCEPT' => 'text/html',
'HTTP_USER_AGENT' => 'Bob/2.0'
cache.trace.must_include :fresh
response.body.must_equal 'Bob/2.0'
response['x-response-count'].must_equal '2'
get '/',
'HTTP_USER_AGENT' => 'Bob/2.0'
cache.trace.must_include :miss
response.body.must_equal 'Bob/2.0'
response['x-response-count'].must_equal '3'
end
end
it 'passes if there was a metastore exception' do
respond_with 200, 'cache-control' => 'max-age=10000' do |req,res|
res.body = ['Hello World']
end
get '/'
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :store
get '/' do |cache|
cache.expects(:metastore).raises Timeout::Error
end
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :pass
post '/' do |cache|
cache.expects(:metastore).raises Timeout::Error
end
assert response.ok?
response.body.must_equal 'Hello World'
cache.trace.must_include :pass
end
it 'does not cache when cache-control response header changed to private (reset @cache_control on dup)' do
count = 0
respond_with do |req,res|
count+= 1
res['cache-control'] = (count == 1) ? 'public' : 'private, no-store'
res['etag'] = count.to_s
res.status = (count == 1) ? 200 : 304
end
get '/'
assert app.called?
assert response.ok?
cache.trace.must_include :miss
cache.trace.must_include :store
get '/'
assert app.called?
assert response.ok?
cache.trace.must_include :stale
cache.trace.must_include :valid
cache.trace.wont_include :store
end
it 'logs to rack.logger if available' do
logger = Class.new do
attr_reader :logged_level
def info(message)
@logged_level = "info"
end
end.new
respond_with 200
get '/', 'rack.logger' => logger
assert response.ok?
logger.logged_level.must_equal "info"
end
end
|