diff options
author | Peter Cline <peter.cline@gmail.com> | 2015-05-12 16:59:21 -0400 |
---|---|---|
committer | Peter Cline <peter.cline@gmail.com> | 2015-06-15 10:21:07 -0400 |
commit | 9c9b6548ecbafdb22c238d2682b53f2fb5461930 (patch) | |
tree | 609949fcb8ec16a6d5745aeb3ca57e20c4ad4c7b /test | |
parent | 5f8cad8c9f0e53a55c439f12d13b8eef90cd0e66 (diff) | |
download | rack-9c9b6548ecbafdb22c238d2682b53f2fb5461930.tar.gz |
API to extract cookies from mock response
The Rack::MockResponse class did not expose any API to provide access to
cookies. This change adds that to facilitate testing and prevent all
clients using the MockResponse class from having to parse response
headers in order to get at the cookies present in the response.
Diffstat (limited to 'test')
-rw-r--r-- | test/spec_mock.rb | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/test/spec_mock.rb b/test/spec_mock.rb index 5405775e..7218e4d8 100644 --- a/test/spec_mock.rb +++ b/test/spec_mock.rb @@ -14,9 +14,13 @@ app = Rack::Lint.new(lambda { |env| end body = req.head? ? "" : env.to_yaml - Rack::Response.new(body, + response = Rack::Response.new(body, req.GET["status"] || 200, - "Content-Type" => "text/yaml").finish + "Content-Type" => "text/yaml") + response.set_cookie("session_test", {:value => "session_test", :domain => ".test.com", :path=> "/"}) + response.set_cookie("secure_test", {:value => "secure_test", :domain => ".test.com", :path => "/", :secure => true}) + response.set_cookie("persistent_test", {:value => "persistent_test", :max_age => 15552000, :path => "/"}) + response.finish }) describe Rack::MockRequest do @@ -247,6 +251,42 @@ describe Rack::MockResponse do res.location.should.be.nil end + should "provide access to session cookies" do + res = Rack::MockRequest.new(app).get("") + session_cookie = res.cookie("session_test") + session_cookie.value[0].should.equal "session_test" + session_cookie.domain.should.equal ".test.com" + session_cookie.path.should.equal "/" + session_cookie.secure.should.equal false + session_cookie.expires.should.be.nil + end + + should "provide access to persistent cookies" do + res = Rack::MockRequest.new(app).get("") + persistent_cookie = res.cookie("persistent_test") + persistent_cookie.value[0].should.equal "persistent_test" + persistent_cookie.domain.should.be.nil + persistent_cookie.path.should.equal "/" + persistent_cookie.secure.should.equal false + persistent_cookie.expires.should.not.be.nil + persistent_cookie.expires.should.be < (Time.now + 15552000) + end + + should "provide access to secure cookies" do + res = Rack::MockRequest.new(app).get("") + secure_cookie = res.cookie("secure_test") + secure_cookie.value[0].should.equal "secure_test" + secure_cookie.domain.should.equal ".test.com" + secure_cookie.path.should.equal "/" + secure_cookie.secure.should.equal true + secure_cookie.expires.should.be.nil + end + + should "return nil if a non existent cookie is requested" do + res = Rack::MockRequest.new(app).get("") + res.cookie("i_dont_exist").should.be.nil + end + should "provide access to the HTTP body" do res = Rack::MockRequest.new(app).get("") res.body.should =~ /rack/ |