Permalink
Browse files

make the session keep track of cookies on every request

  • Loading branch information...
1 parent d24194a commit 74e13cb61535391cca244b686d70584d25c65e3c @jamesgolick committed Apr 30, 2009
Showing with 35 additions and 5 deletions.
  1. +6 −2 lib/trample/session.rb
  2. +1 −1 test/integration/trample_a_single_url_test.rb
  3. +21 −2 test/session_test.rb
  4. +7 −0 test/test_helper.rb
View
8 lib/trample/session.rb
@@ -3,11 +3,12 @@ class Session
include Logging
include Timer
- attr_reader :config, :response_times
+ attr_reader :config, :response_times, :cookies
def initialize(config)
@config = config
@response_times = []
+ @cookies = {}
end
def trample
@@ -21,7 +22,10 @@ def trample
protected
def request(page)
length = time do
- RestClient.send(page.request_method, page.url)
+ response = RestClient.send(page.request_method, page.url, :cookies => cookies)
+ # this is ugly, but it's the only way that I could get the test to pass
+ # because rr keeps a reference to the arguments, not a copy. ah well.
+ @cookies = cookies.merge(response.cookies)
end
response_times << length
logger.info "#{page.request_method.to_s.upcase} #{page.url} #{length}s"
View
2 test/integration/trample_a_single_url_test.rb
@@ -2,7 +2,7 @@
class TrampleASingleUrlTest < Test::Unit::TestCase
def test_trampling_a_single_url
- mock(RestClient).get("http://google.com").times(2) { "" }
+ mock_get("http://google.com", :times => 2)
trample "test/fixtures/basic_config.rb"
end
end
View
23 test/session_test.rb
@@ -16,8 +16,8 @@ def setup
end
before_should "visit the pages iterations times each" do
- mock(RestClient).get("http://google.com/").times(2)
- mock(RestClient).get("http://amazon.com/").times(2)
+ mock_get("http://google.com/", :times => 2)
+ mock_get("http://amazon.com/", :times => 2)
end
end
@@ -31,5 +31,24 @@ def setup
assert_equal [1.4, 1.4, 1.4, 1.4], @session.response_times
end
end
+
+ context "If a page responds with a cookie" do
+ should "pass that cookie on to the next page" do
+ stub(RestClient).get(anything, anything) do
+ response = RestClient::Response.new("", stub!)
+ stub(response).cookies { {"xyz" => "abc"} }
+ end
+
+ @config = Trample::Configuration.new do |t|
+ t.iterations 2
+ t.get("http://amazon.com/")
+ end
+
+ @session = Trample::Session.new(@config)
+ @session.trample
+
+ assert_received(RestClient) { |c| c.get("http://amazon.com/", :cookies => {"xyz" => "abc"}) }
+ end
+ end
end
View
7 test/test_helper.rb
@@ -14,5 +14,12 @@ class Test::Unit::TestCase
def trample(config)
Trample::Cli.new.start(config)
end
+
+ def mock_get(url, opts={})
+ mock(RestClient).get(url, :cookies => opts[:cookies] || {}).times(opts[:times]) do
+ response = RestClient::Response.new("", stub!)
+ stub(response).cookies { opts[:return_cookies] || {} }
+ end
+ end
end

0 comments on commit 74e13cb

Please sign in to comment.