Skip to content
Newer
Older
100644 79 lines (59 sloc) 1.62 KB
e864ff7 @pixeltrix Add backward compatibility for testing cookies
pixeltrix authored
1 require 'active_support/core_ext/hash/indifferent_access'
31f09f9 @pixeltrix Improve testing of cookies in functional tests:
pixeltrix authored
2 require 'rack/utils'
76f024a @fxn adds missing requires for Object#blank? and Object#present?
fxn authored
3
00d1a57 @josh Start moving TestRequest and TestResponse into ActionDispatch
josh authored
4 module ActionDispatch
5 class TestRequest < Request
a6fff94 @josh Move TestRequest cookies accessor into AD TestRequest
josh authored
6 DEFAULT_ENV = Rack::MockRequest.env_for('/')
7
00d1a57 @josh Start moving TestRequest and TestResponse into ActionDispatch
josh authored
8 def self.new(env = {})
9 super
10 end
11
12 def initialize(env = {})
89ebd28 @marcandre Fix bug when Rails.application is defined but is nil. See #881
marcandre authored
13 env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application
142d50e @tenderlove make the default environment have actual defaults
tenderlove authored
14 super(default_env.merge(env))
00d1a57 @josh Start moving TestRequest and TestResponse into ActionDispatch
josh authored
15
16 self.host = 'test.host'
17 self.remote_addr = '0.0.0.0'
18 self.user_agent = 'Rails Testing'
19 end
20
21 def request_method=(method)
22 @env['REQUEST_METHOD'] = method.to_s.upcase
23 end
24
25 def host=(host)
26 @env['HTTP_HOST'] = host
27 end
28
29 def port=(number)
30 @env['SERVER_PORT'] = number.to_i
31 end
32
33 def request_uri=(uri)
34 @env['REQUEST_URI'] = uri
35 end
36
37 def path=(path)
38 @env['PATH_INFO'] = path
39 end
40
1fcc7db @josh Move TestRequest#query_parameters into AD TestRequest
josh authored
41 def action=(action_name)
42 path_parameters["action"] = action_name.to_s
43 end
44
00d1a57 @josh Start moving TestRequest and TestResponse into ActionDispatch
josh authored
45 def if_modified_since=(last_modified)
46 @env['HTTP_IF_MODIFIED_SINCE'] = last_modified
47 end
48
49 def if_none_match=(etag)
50 @env['HTTP_IF_NONE_MATCH'] = etag
51 end
52
53 def remote_addr=(addr)
54 @env['REMOTE_ADDR'] = addr
55 end
56
57 def user_agent=(user_agent)
58 @env['HTTP_USER_AGENT'] = user_agent
59 end
60
61 def accept=(mime_types)
a6fff94 @josh Move TestRequest cookies accessor into AD TestRequest
josh authored
62 @env.delete('action_dispatch.request.accepts')
dafb4bd @miloops Don't shadow outer local variables.
miloops authored
63 @env['HTTP_ACCEPT'] = Array(mime_types).collect { |mime_type| mime_type.to_s }.join(",")
00d1a57 @josh Start moving TestRequest and TestResponse into ActionDispatch
josh authored
64 end
e864ff7 @pixeltrix Add backward compatibility for testing cookies
pixeltrix authored
65
66 alias :rack_cookies :cookies
67
68 def cookies
69 @cookies ||= {}.with_indifferent_access
70 end
142d50e @tenderlove make the default environment have actual defaults
tenderlove authored
71
72 private
73
74 def default_env
75 DEFAULT_ENV
76 end
00d1a57 @josh Start moving TestRequest and TestResponse into ActionDispatch
josh authored
77 end
78 end
Something went wrong with that request. Please try again.