Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 801 lines (683 sloc) 19.225 kb
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
require 'spec_helper'

describe Sinatra::Cookies do
  def cookie_route(*cookies, &block)
    result = nil
    set_cookie(cookies)
    @cookie_app.get('/') do
      result = instance_eval(&block)
      "ok"
    end
    get '/'
    last_response.should be_ok
    body.should be == "ok"
    result
  end

  def cookies(*set_cookies)
    cookie_route(*set_cookies) { cookies }
  end

  before do
    app = nil
    mock_app do
      helpers Sinatra::Cookies
      app = self
    end
    @cookie_app = app
    clear_cookies
  end

  describe :cookie_route do
    it 'runs the block' do
      ran = false
      cookie_route { ran = true }
      ran.should be_true
    end

    it 'returns the block result' do
      cookie_route { 42 }.should be == 42
    end
  end

  describe :== do
    it 'is comparable to hashes' do
      cookies.should be == {}
    end

    it 'is comparable to anything that responds to to_hash' do
      other = Struct.new(:to_hash).new({})
      cookies.should be == other
    end
  end

  describe :[] do
    it 'allows access to request cookies' do
      cookies("foo=bar")["foo"].should be == "bar"
    end

    it 'takes symbols as keys' do
      cookies("foo=bar")[:foo].should be == "bar"
    end

    it 'returns nil for missing keys' do
      cookies("foo=bar")['bar'].should be_nil
    end

    it 'allows access to response cookies' do
      cookie_route do
        response.set_cookie 'foo', 'bar'
        cookies['foo']
      end.should be == 'bar'
    end

    it 'favors response cookies over request cookies' do
      cookie_route('foo=bar') do
        response.set_cookie 'foo', 'baz'
        cookies['foo']
      end.should be == 'baz'
    end


    it 'takes the last value for response cookies' do
      cookie_route do
        response.set_cookie 'foo', 'bar'
        response.set_cookie 'foo', 'baz'
        cookies['foo']
      end.should be == 'baz'
    end
  end

  describe :[]= do
    it 'sets cookies to httponly' do
      cookie_route do
        cookies['foo'] = 'bar'
        response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
      end.should include('HttpOnly')
    end

    it 'sets the domain' do
      cookie_route do
        cookies['foo'] = 'bar'
        response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
      end.should include('domain=example.org')
    end

    it 'sets path to / by default' do
      cookie_route do
        cookies['foo'] = 'bar'
        response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
      end.should include('path=/')
    end

    it 'sets path to the script_name if app is nested' do
      cookie_route do
        request.script_name = '/foo'
        cookies['foo'] = 'bar'
        response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
      end.should include('path=/foo')
    end

    it 'sets a cookie' do
      cookie_route { cookies['foo'] = 'bar' }
      cookie_jar['foo'].should be == 'bar'
    end

    it 'adds a value to the cookies hash' do
      cookie_route do
        cookies['foo'] = 'bar'
        cookies['foo']
      end.should be == 'bar'
    end
  end

  describe :assoc do
    it 'behaves like Hash#assoc' do
      cookies('foo=bar').assoc('foo') == ['foo', 'bar']
    end
  end if Hash.method_defined? :assoc

  describe :clear do
    it 'removes request cookies from cookies hash' do
      jar = cookies('foo=bar')
      jar['foo'].should be == 'bar'
      jar.clear
      jar['foo'].should be_nil
    end

    it 'removes response cookies from cookies hash' do
      cookie_route do
        cookies['foo'] = 'bar'
        cookies.clear
        cookies['foo']
      end.should be_nil
    end

    it 'expires existing cookies' do
      cookie_route("foo=bar") do
        cookies.clear
        response['Set-Cookie']
      end.should include("foo=;", "expires=", "1970 00:00:00")
    end
  end

  describe :compare_by_identity? do
    it { cookies.should_not be_compare_by_identity }
  end

  describe :default do
    it { cookies.default.should be_nil }
  end

  describe :default_proc do
    it { cookies.default_proc.should be_nil }
  end

  describe :delete do
    it 'removes request cookies from cookies hash' do
      jar = cookies('foo=bar')
      jar['foo'].should be == 'bar'
      jar.delete 'foo'
      jar['foo'].should be_nil
    end

    it 'removes response cookies from cookies hash' do
      cookie_route do
        cookies['foo'] = 'bar'
        cookies.delete 'foo'
        cookies['foo']
      end.should be_nil
    end

    it 'expires existing cookies' do
      cookie_route("foo=bar") do
        cookies.delete 'foo'
        response['Set-Cookie']
      end.should include("foo=;", "expires=", "1970 00:00:00")
    end

    it 'honours the app cookie_options' do
      @cookie_app.class_eval do
        set :cookie_options, {
          :path => '/foo',
          :domain => 'bar.com',
          :secure => true,
          :httponly => true
        }
      end
      cookie_header = cookie_route("foo=bar") do
        cookies.delete 'foo'
        response['Set-Cookie']
      end
      cookie_header.should include("path=/foo;", "domain=bar.com;", "secure;", "HttpOnly")
    end

    it 'does not touch other cookies' do
      cookie_route("foo=bar", "bar=baz") do
        cookies.delete 'foo'
        cookies['bar']
      end.should be == 'baz'
    end

    it 'returns the previous value for request cookies' do
      cookie_route("foo=bar") do
        cookies.delete "foo"
      end.should be == "bar"
    end

    it 'returns the previous value for response cookies' do
      cookie_route do
        cookies['foo'] = 'bar'
        cookies.delete "foo"
      end.should be == "bar"
    end

    it 'returns nil for non-existing cookies' do
      cookie_route { cookies.delete("foo") }.should be_nil
    end
  end

  describe :delete_if do
    it 'deletes cookies that match the block' do
      cookie_route('foo=bar') do
        cookies['bar'] = 'baz'
        cookies['baz'] = 'foo'
        cookies.delete_if { |*a| a.include? 'bar' }
        cookies.values_at 'foo', 'bar', 'baz'
      end.should be == [nil, nil, 'foo']
    end
  end

  describe :each do
    it 'loops through cookies' do
      keys = []
      foo = nil
      bar = nil

      cookie_route('foo=bar', 'bar=baz') do
        cookies.each do |key, value|
          foo = value if key == 'foo'
          bar = value if key == 'bar'
          keys << key
        end
      end

      keys.sort.should be == ['bar', 'foo']
      foo.should be == 'bar'
      bar.should be == 'baz'
    end

    it 'favors response over request cookies' do
      seen = false
      cookie_route('foo=bar') do
        cookies[:foo] = 'baz'
        cookies.each do |key, value|
          key.should == 'foo'
          value.should == 'baz'
          seen.should == false
          seen = true
        end
      end
    end

    it 'does not loop through deleted cookies' do
      cookie_route('foo=bar') do
        cookies.delete :foo
        cookies.each { fail }
      end
    end

    it 'returns an enumerator' do
      cookie_route('foo=bar') do
        enum = cookies.each
        enum.each { |key, value| key.should == 'foo' }
      end
    end
  end

  describe :each_key do
    it 'loops through cookies' do
      keys = []

      cookie_route('foo=bar', 'bar=baz') do
        cookies.each_key do |key|
          keys << key
        end
      end

      keys.sort.should be == ['bar', 'foo']
    end

    it 'only yields keys once' do
      seen = false
      cookie_route('foo=bar') do
        cookies[:foo] = 'baz'
        cookies.each_key do |key|
          seen.should == false
          seen = true
        end
      end
    end

    it 'does not loop through deleted cookies' do
      cookie_route('foo=bar') do
        cookies.delete :foo
        cookies.each_key { fail }
      end
    end

    it 'returns an enumerator' do
      cookie_route('foo=bar') do
        enum = cookies.each_key
        enum.each { |key| key.should == 'foo' }
      end
    end
  end

  describe :each_pair do
    it 'loops through cookies' do
      keys = []
      foo = nil
      bar = nil

      cookie_route('foo=bar', 'bar=baz') do
        cookies.each_pair do |key, value|
          foo = value if key == 'foo'
          bar = value if key == 'bar'
          keys << key
        end
      end

      keys.sort.should be == ['bar', 'foo']
      foo.should be == 'bar'
      bar.should be == 'baz'
    end

    it 'favors response over request cookies' do
      seen = false
      cookie_route('foo=bar') do
        cookies[:foo] = 'baz'
        cookies.each_pair do |key, value|
          key.should == 'foo'
          value.should == 'baz'
          seen.should == false
          seen = true
        end
      end
    end

    it 'does not loop through deleted cookies' do
      cookie_route('foo=bar') do
        cookies.delete :foo
        cookies.each_pair { fail }
      end
    end

    it 'returns an enumerator' do
      cookie_route('foo=bar') do
        enum = cookies.each_pair
        enum.each { |key, value| key.should == 'foo' }
      end
    end
  end

  describe :each_value do
    it 'loops through cookies' do
      values = []

      cookie_route('foo=bar', 'bar=baz') do
        cookies.each_value do |value|
          values << value
        end
      end

      values.sort.should be == ['bar', 'baz']
    end

    it 'favors response over request cookies' do
      seen = false
      cookie_route('foo=bar') do
        cookies[:foo] = 'baz'
        cookies.each_value do |value|
          value.should == 'baz'
          seen.should == false
          seen = true
        end
      end
    end

    it 'does not loop through deleted cookies' do
      cookie_route('foo=bar') do
        cookies.delete :foo
        cookies.each_value { fail }
      end
    end

    it 'returns an enumerator' do
      cookie_route('foo=bar') do
        enum = cookies.each_value
        enum.each { |value| value.should == 'bar' }
      end
    end
  end

  describe :empty? do
    it 'returns true if there are no cookies' do
      cookies.should be_empty
    end

    it 'returns false if there are request cookies' do
      cookies('foo=bar').should_not be_empty
    end

    it 'returns false if there are response cookies' do
      cookie_route do
        cookies['foo'] = 'bar'
        cookies.empty?
      end.should be_false
    end

    it 'becomes true if response cookies are removed' do
      cookie_route do
        cookies['foo'] = 'bar'
        cookies.delete :foo
        cookies.empty?
      end.should be_true
    end

    it 'becomes true if request cookies are removed' do
      cookie_route('foo=bar') do
        cookies.delete :foo
        cookies.empty?
      end.should be_true
    end

    it 'becomes true after clear' do
      cookie_route('foo=bar', 'bar=baz') do
        cookies['foo'] = 'bar'
        cookies.clear
        cookies.empty?
      end.should be_true
    end
  end

  describe :fetch do
    it 'returns values from request cookies' do
      cookies('foo=bar').fetch('foo').should be == 'bar'
    end

    it 'returns values from response cookies' do
      cookie_route do
        cookies['foo'] = 'bar'
        cookies.fetch('foo')
      end.should be == 'bar'
    end

    it 'favors response over request cookies' do
      cookie_route('foo=baz') do
        cookies['foo'] = 'bar'
        cookies.fetch('foo')
      end.should be == 'bar'
    end

    it 'raises an exception if key does not exist' do
      error = if defined? JRUBY_VERSION
        IndexError
      else
        RUBY_VERSION >= '1.9' ? KeyError : IndexError
      end
      expect { cookies.fetch('foo') }.to raise_exception(error)
    end

    it 'returns the block result if missing' do
      cookies.fetch('foo') { 'bar' }.should be == 'bar'
    end
  end

  describe :flatten do
    it { cookies('foo=bar').flatten.should be == {'foo' => 'bar'}.flatten }
  end if Hash.method_defined? :flatten

  describe :has_key? do
    it 'checks request cookies' do
      cookies('foo=bar').should have_key('foo')
    end

    it 'checks response cookies' do
      jar = cookies
      jar['foo'] = 'bar'
      jar.should have_key(:foo)
    end

    it 'does not use deleted cookies' do
      jar = cookies('foo=bar')
      jar.delete :foo
      jar.should_not have_key('foo')
    end
  end

  describe :has_value? do
    it 'checks request cookies' do
      cookies('foo=bar').should have_value('bar')
    end

    it 'checks response cookies' do
      jar = cookies
      jar[:foo] = 'bar'
      jar.should have_value('bar')
    end

    it 'does not use deleted cookies' do
      jar = cookies('foo=bar')
      jar.delete :foo
      jar.should_not have_value('bar')
    end
  end

  describe :include? do
    it 'checks request cookies' do
      cookies('foo=bar').should include('foo')
    end

    it 'checks response cookies' do
      jar = cookies
      jar['foo'] = 'bar'
      jar.should include(:foo)
    end

    it 'does not use deleted cookies' do
      jar = cookies('foo=bar')
      jar.delete :foo
      jar.should_not include('foo')
    end
  end

  describe :index do
    it 'checks request cookies' do
      cookies('foo=bar').index('bar').should be == 'foo'
    end

    it 'checks response cookies' do
      jar = cookies
      jar['foo'] = 'bar'
      jar.index('bar').should be == 'foo'
    end

    it 'returns nil when missing' do
      cookies('foo=bar').index('baz').should be_nil
    end
  end if RUBY_VERSION < '1.9'

  describe :keep_if do
    it 'removes entries' do
      jar = cookies('foo=bar', 'bar=baz')
      jar.keep_if { |*args| args == ['bar', 'baz'] }
      jar.should be == {'bar' => 'baz'}
    end
  end

  describe :key do
    it 'checks request cookies' do
      cookies('foo=bar').key('bar').should be == 'foo'
    end

    it 'checks response cookies' do
      jar = cookies
      jar['foo'] = 'bar'
      jar.key('bar').should be == 'foo'
    end

    it 'returns nil when missing' do
      cookies('foo=bar').key('baz').should be_nil
    end
  end

  describe :key? do
    it 'checks request cookies' do
      cookies('foo=bar').key?('foo').should be_true
    end

    it 'checks response cookies' do
      jar = cookies
      jar['foo'] = 'bar'
      jar.key?(:foo).should be_true
    end

    it 'does not use deleted cookies' do
      jar = cookies('foo=bar')
      jar.delete :foo
      jar.key?('foo').should be_false
    end
  end

  describe :keys do
    it { cookies('foo=bar').keys.should == ['foo'] }
  end

  describe :length do
    it { cookies.length.should == 0 }
    it { cookies('foo=bar').length.should == 1 }
  end

  describe :member? do
    it 'checks request cookies' do
      cookies('foo=bar').member?('foo').should be_true
    end

    it 'checks response cookies' do
      jar = cookies
      jar['foo'] = 'bar'
      jar.member?(:foo).should be_true
    end

    it 'does not use deleted cookies' do
      jar = cookies('foo=bar')
      jar.delete :foo
      jar.member?('foo').should be_false
    end
  end

  describe :merge do
    it 'is mergable with a hash' do
      cookies('foo=bar').merge(:bar => :baz).should be == {"foo" => "bar", :bar => :baz}
    end

    it 'does not create cookies' do
      jar = cookies('foo=bar')
      jar.merge(:bar => 'baz')
      jar.should_not include(:bar)
    end

    it 'takes a block for conflict resolution' do
      update = {'foo' => 'baz', 'bar' => 'baz'}
      merged = cookies('foo=bar').merge(update) do |key, old, other|
        key.should be == 'foo'
        old.should be == 'bar'
        other.should be == 'baz'
        'foo'
      end
      merged['foo'].should be == 'foo'
    end
  end

  describe :merge! do
    it 'creates cookies' do
      jar = cookies('foo=bar')
      jar.merge! :bar => 'baz'
      jar.should include('bar')
    end

    it 'overrides existing values' do
      jar = cookies('foo=bar')
      jar.merge! :foo => "baz"
      jar["foo"].should be == "baz"
    end

    it 'takes a block for conflict resolution' do
      update = {'foo' => 'baz', 'bar' => 'baz'}
      jar = cookies('foo=bar')
      jar.merge!(update) do |key, old, other|
        key.should be == 'foo'
        old.should be == 'bar'
        other.should be == 'baz'
        'foo'
      end
      jar['foo'].should be == 'foo'
    end
  end

  describe :rassoc do
    it 'behaves like Hash#assoc' do
      cookies('foo=bar').rassoc('bar') == ['foo', 'bar']
    end
  end if Hash.method_defined? :rassoc

  describe :reject do
    it 'removes entries from new hash' do
      jar = cookies('foo=bar', 'bar=baz')
      sub = jar.reject { |*args| args == ['bar', 'baz'] }
      sub.should be == {'foo' => 'bar'}
      jar['bar'].should be == 'baz'
    end
  end

  describe :reject! do
    it 'removes entries' do
      jar = cookies('foo=bar', 'bar=baz')
      jar.reject! { |*args| args == ['bar', 'baz'] }
      jar.should be == {'foo' => 'bar'}
    end
  end

  describe :replace do
    it 'replaces entries' do
      jar = cookies('foo=bar', 'bar=baz')
      jar.replace 'foo' => 'baz', 'baz' => 'bar'
      jar.should be == {'foo' => 'baz', 'baz' => 'bar'}
    end
  end

  describe :select do
    it 'removes entries from new hash' do
      jar = cookies('foo=bar', 'bar=baz')
      sub = jar.select { |*args| args != ['bar', 'baz'] }
      sub.should be == {'foo' => 'bar'}.select { true }
      jar['bar'].should be == 'baz'
    end
  end

  describe :select! do
    it 'removes entries' do
      jar = cookies('foo=bar', 'bar=baz')
      jar.select! { |*args| args != ['bar', 'baz'] }
      jar.should be == {'foo' => 'bar'}
    end
  end if Hash.method_defined? :select!

  describe :shift do
    it 'removes from the hash' do
      jar = cookies('foo=bar')
      jar.shift.should be == ['foo', 'bar']
      jar.should_not include('bar')
    end
  end

  describe :size do
    it { cookies.size.should == 0 }
    it { cookies('foo=bar').size.should == 1 }
  end

  describe :update do
    it 'creates cookies' do
      jar = cookies('foo=bar')
      jar.update :bar => 'baz'
      jar.should include('bar')
    end

    it 'overrides existing values' do
      jar = cookies('foo=bar')
      jar.update :foo => "baz"
      jar["foo"].should be == "baz"
    end

    it 'takes a block for conflict resolution' do
      merge = {'foo' => 'baz', 'bar' => 'baz'}
      jar = cookies('foo=bar')
      jar.update(merge) do |key, old, other|
        key.should be == 'foo'
        old.should be == 'bar'
        other.should be == 'baz'
        'foo'
      end
      jar['foo'].should be == 'foo'
    end
  end

  describe :value? do
    it 'checks request cookies' do
      cookies('foo=bar').value?('bar').should be_true
    end

    it 'checks response cookies' do
      jar = cookies
      jar[:foo] = 'bar'
      jar.value?('bar').should be_true
    end

    it 'does not use deleted cookies' do
      jar = cookies('foo=bar')
      jar.delete :foo
      jar.value?('bar').should_not be_true
    end
  end

  describe :values do
    it { cookies('foo=bar', 'bar=baz').values.sort.should be == ['bar', 'baz'] }
  end

  describe :values_at do
    it { cookies('foo=bar', 'bar=baz').values_at('foo').should be == ['bar'] }
  end
end
Something went wrong with that request. Please try again.