Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 107 lines (86 sloc) 4.064 kB
25595e1 @mpalmer Switch to using minitest
mpalmer authored
1 require 'minitest/autorun'
a92ec4d @brainopia Add tests
brainopia authored
2 require 'rack/mock'
3 require 'rack/builder'
4 require 'rack/contrib/common_cookies'
5
25595e1 @mpalmer Switch to using minitest
mpalmer authored
6 describe Rack::CommonCookies do
a92ec4d @brainopia Add tests
brainopia authored
7
25595e1 @mpalmer Switch to using minitest
mpalmer authored
8 before do
a92ec4d @brainopia Add tests
brainopia authored
9 @app = Rack::Builder.new do
10 use Rack::CommonCookies
11 run lambda {|env| [200, {'Set-Cookie' => env['HTTP_COOKIE']}, []] }
12 end
13 end
14
15 def request
16 Rack::MockRequest.new(@app)
17 end
18
19 def make_request(domain, cookies='key=value')
20 request.get '/', 'HTTP_COOKIE' => cookies, 'HTTP_HOST' => domain
21 end
22
23 specify 'should use .domain.com for cookies from domain.com' do
24 response = make_request 'domain.com'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
25 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.com'
a92ec4d @brainopia Add tests
brainopia authored
26 end
27
28 specify 'should use .domain.com for cookies from www.domain.com' do
29 response = make_request 'www.domain.com'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
30 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.com'
a92ec4d @brainopia Add tests
brainopia authored
31 end
32
33 specify 'should use .domain.com for cookies from subdomain.domain.com' do
34 response = make_request 'subdomain.domain.com'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
35 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.com'
a92ec4d @brainopia Add tests
brainopia authored
36 end
37
38 specify 'should use .domain.com for cookies from 0.subdomain1.subdomain2.domain.com' do
39 response = make_request '0.subdomain1.subdomain2.domain.com'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
40 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.com'
a92ec4d @brainopia Add tests
brainopia authored
41 end
42
43 specify 'should use .domain.local for cookies from domain.local' do
44 response = make_request '0.subdomain1.subdomain2.domain.com'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
45 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.com'
a92ec4d @brainopia Add tests
brainopia authored
46 end
47
48 specify 'should use .domain.local for cookies from subdomain.domain.local' do
49 response = make_request 'subdomain.domain.local'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
50 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.local'
a92ec4d @brainopia Add tests
brainopia authored
51 end
52
53 specify 'should use .domain.com.ua for cookies from domain.com.ua' do
54 response = make_request 'domain.com.ua'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
55 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.com.ua'
a92ec4d @brainopia Add tests
brainopia authored
56 end
57
58 specify 'should use .domain.com.ua for cookies from subdomain.domain.com.ua' do
59 response = make_request 'subdomain.domain.com.ua'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
60 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.com.ua'
a92ec4d @brainopia Add tests
brainopia authored
61 end
62
63 specify 'should use .domain.co.uk for cookies from domain.co.uk' do
64 response = make_request 'domain.co.uk'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
65 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.co.uk'
a92ec4d @brainopia Add tests
brainopia authored
66 end
67
68 specify 'should use .domain.co.uk for cookies from subdomain.domain.co.uk' do
69 response = make_request 'subdomain.domain.co.uk'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
70 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.co.uk'
a92ec4d @brainopia Add tests
brainopia authored
71 end
72
73 specify 'should use .domain.eu.com for cookies from domain.eu.com' do
74 response = make_request 'domain.eu.com'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
75 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.eu.com'
a92ec4d @brainopia Add tests
brainopia authored
76 end
77
78 specify 'should use .domain.eu.com for cookies from subdomain.domain.eu.com' do
79 response = make_request 'subdomain.domain.eu.com'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
80 response.headers['Set-Cookie'].must_equal 'key=value; domain=.domain.eu.com'
a92ec4d @brainopia Add tests
brainopia authored
81 end
82
83 specify 'should work with multiple cookies' do
84 response = make_request 'sub.domain.bz', "key=value\nkey1=value2"
25595e1 @mpalmer Switch to using minitest
mpalmer authored
85 response.headers['Set-Cookie'].must_equal "key=value; domain=.domain.bz\nkey1=value2; domain=.domain.bz"
a92ec4d @brainopia Add tests
brainopia authored
86 end
87
88 specify 'should work with cookies which have explicit domain' do
89 response = make_request 'sub.domain.bz', "key=value; domain=domain.bz"
25595e1 @mpalmer Switch to using minitest
mpalmer authored
90 response.headers['Set-Cookie'].must_equal "key=value; domain=.domain.bz"
a92ec4d @brainopia Add tests
brainopia authored
91 end
99df568 @brainopia Add a few tests for edge cases
brainopia authored
92
93 specify 'should not touch cookies if domain is localhost' do
94 response = make_request 'localhost'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
95 response.headers['Set-Cookie'].must_equal "key=value"
99df568 @brainopia Add a few tests for edge cases
brainopia authored
96 end
97
98 specify 'should not touch cookies if domain is ip address' do
99 response = make_request '127.0.0.1'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
100 response.headers['Set-Cookie'].must_equal "key=value"
99df568 @brainopia Add a few tests for edge cases
brainopia authored
101 end
102
103 specify 'should use .domain.com for cookies from subdomain.domain.com:3000' do
104 response = make_request 'subdomain.domain.com:3000'
25595e1 @mpalmer Switch to using minitest
mpalmer authored
105 response.headers['Set-Cookie'].must_equal "key=value; domain=.domain.com"
99df568 @brainopia Add a few tests for edge cases
brainopia authored
106 end
a92ec4d @brainopia Add tests
brainopia authored
107 end
Something went wrong with that request. Please try again.