Class Rack::MockRequest
In: lib/rackbox/rack/sticky_sessions.rb
lib/rackbox/rack/content_length_fix.rb
Parent: Object

An evil fix

The actual fix has been pulled upstream into Rack but hasn‘t made it into the Rack gem yet, so we need this fix until the new gem is released

Methods

External Aliases

env_for -> env_for_without_content_length_fix
env_for_with_content_length_fix -> env_for

Attributes

cookies  [RW]  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'
   }

}

Public Class methods

[Source]

    # File lib/rackbox/rack/content_length_fix.rb, line 11
11:     def env_for_with_content_length_fix uri = '', opts = {}
12:       env = env_for_without_content_length_fix uri, opts
13:       env['CONTENT_LENGTH'] ||= env['rack.input'].length
14:       env
15:     end

Public Instance methods

shortcut to get cookies for a particular domain

[Source]

    # File lib/rackbox/rack/sticky_sessions.rb, line 17
17:   def cookies_for domain
18:     @cookies ||= {}
19:     @cookies[ domain ]
20:   end

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!

[Source]

    # File lib/rackbox/rack/sticky_sessions.rb, line 25
25:   def request method = "GET", uri = "", opts = { }
26: 
27:     env = self.class.env_for(uri, opts.merge(:method => method))
28:     
29:     unless @cookies.nil? or @cookies.empty? or @cookies[env['SERVER_NAME']].nil? or @cookies[env['SERVER_NAME']].empty?
30:       env['HTTP_COOKIE'] = @cookies[env['SERVER_NAME']].map{ |k,v| "#{ k }=#{ v }" }.join('; ')
31:     end
32: 
33:     if opts[:lint]
34:       app = Rack::Lint.new(@app)
35:     else
36:       app = @app
37:     end 
38: 
39:     errors = env["rack.errors"]
40:     response = Rack::MockResponse.new(*(app.call(env) + [errors]))
41:     
42:     if response.original_headers['Set-Cookie']
43:       @cookies ||= {}
44:       @cookies[ env['SERVER_NAME'] ] ||= {}
45:       response.original_headers['Set-Cookie'].map{ |str| /(.*); path/.match(str)[1] }.each do |cookie|
46:         name, value = cookie.split('=').first, cookie.split('=')[1]
47:         @cookies[ env['SERVER_NAME'] ][ name ] = value
48:       end
49:     end
50: 
51:     response
52:   end

[Validate]