Class: Rack::MockRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rackbox/rack/content_length_fix.rb,
lib/rackbox/rack/sticky_sessions.rb

Overview

little extension to Rack::MockRequest to track cookies

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookiesObject

cookies is a hash of persistent cookies (by domain) that let you test cookies for your app

cookies = {

'example.org' => {
   'cookie-name' => 'cookie-value',
   'chunky' => 'bacon'
}

}



14
15
16
# File 'lib/rackbox/rack/sticky_sessions.rb', line 14

def cookies
  @cookies
end

Class Method Details

.env_for_with_content_length_fix(uri = '', opts = {}) ⇒ Object Also known as: env_for



11
12
13
14
15
# File 'lib/rackbox/rack/content_length_fix.rb', line 11

def env_for_with_content_length_fix uri = '', opts = {}
  env = env_for_without_content_length_fix uri, opts
  env['CONTENT_LENGTH'] ||= env['rack.input'].length
  env
end

Instance Method Details

#cookies_for(domain) ⇒ Object

shortcut to get cookies for a particular domain



17
18
19
20
# File 'lib/rackbox/rack/sticky_sessions.rb', line 17

def cookies_for domain
  @cookies ||= {}
  @cookies[ domain ]
end

#request(method = "GET", uri = "", opts = { }) ⇒ Object

oh geez … it looks like i basically copy/pasted this. there’s gotta be a way to do this that’s

more resilient to Rack changes to this method.  i don't like overriding the whole method!


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
# File 'lib/rackbox/rack/sticky_sessions.rb', line 25

def request method = "GET", uri = "", opts = { }

  env = self.class.env_for(uri, opts.merge(:method => method))
  
  unless @cookies.nil? or @cookies.empty? or @cookies[env['SERVER_NAME']].nil? or @cookies[env['SERVER_NAME']].empty?
    env['HTTP_COOKIE'] = @cookies[env['SERVER_NAME']].map{ |k,v| "#{ k }=#{ v }" }.join('; ')
  end

  if opts[:lint]
    app = Rack::Lint.new(@app)
  else
    app = @app
  end 

  errors = env["rack.errors"]
  response = Rack::MockResponse.new(*(app.call(env) + [errors]))
  
  if response.original_headers['Set-Cookie']
    @cookies ||= {}
    @cookies[ env['SERVER_NAME'] ] ||= {}
    response.original_headers['Set-Cookie'].map{ |str| /(.*); path/.match(str)[1] }.each do |cookie|
      name, value = cookie.split('=').first, cookie.split('=')[1]
      @cookies[ env['SERVER_NAME'] ][ name ] = value
    end
  end

  response
end