Class: Nitrous::IntegrationTest
- Inherits:
-
RailsTest
show all
- Includes:
- ActionController::Assertions::SelectorAssertions
- Defined in:
- lib/nitrous/integration_test.rb
Defined Under Namespace
Classes: DummyFile
Constant Summary
collapse
- SERVER_PORT =
4022
- BOUNDARY =
'multipart-boundary000'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#assert_form_redisplayed! ⇒ Object
-
#assert_form_values!(id, data = {}) ⇒ Object
-
#assert_not_page_contains!(string) ⇒ Object
-
#assert_page_contains!(string) ⇒ Object
-
#assert_viewing(request_uri, message = nil) ⇒ Object
-
#click_link(url, method = :get) ⇒ Object
-
#delete(path, parameters = nil, headers = {}) ⇒ Object
-
#error? ⇒ Boolean
was there a server-side error?.
-
#existing_values(form) ⇒ Object
-
#field_value(name) ⇒ Object
-
#follow_redirect! ⇒ Object
-
#get(path, parameters = nil, headers = {}) ⇒ Object
-
#html_document ⇒ Object
-
#missing? ⇒ Boolean
-
#multipart_encode(fields) ⇒ Object
-
#navigate_to(path, headers = {}) ⇒ Object
-
#post(path, parameters = nil, headers = {}) ⇒ Object
-
#post_form(url, data = {}, method = :post) ⇒ Object
-
#process(headers, path = nil) ⇒ Object
-
#put(path, parameters = nil, headers = {}) ⇒ Object
-
#redirect? ⇒ Boolean
-
#submit_form(id, data = {}) ⇒ Object
-
#success? ⇒ Boolean
was the response successful?.
-
#url_for(options) ⇒ Object
-
#validate_form_fields(form, data) ⇒ Object
-
#view(email) ⇒ Object
Methods inherited from RailsTest
#assert_created!, #assert_destroyed!, #assert_email_sent!, #assert_no_email_sent!, #assert_not_created!, #created, #destroyed, #invalid
Methods inherited from Test
callbacks, #callbacks, #collect_errors, exclude, inherited, #initialize, #nitrous_setup, #nitrous_teardown, #register_callback, register_callback, #run, run, #running, setup, #setup, stest, teardown, #teardown, test, test_classes, tests, ztest
Methods included from Assertions
#assert!, #assert_equal!, #assert_match!, #assert_nil!, #assert_not_equal!, #assert_not_raised!, #assert_raise!, #fail, included, method_added
Constructor Details
This class inherits a constructor from Nitrous::Test
Instance Attribute Details
#cookies ⇒ Object
Returns the value of attribute cookies.
12
13
14
|
# File 'lib/nitrous/integration_test.rb', line 12
def cookies
@cookies
end
|
#current_uri ⇒ Object
Returns the value of attribute current_uri.
12
13
14
|
# File 'lib/nitrous/integration_test.rb', line 12
def current_uri
@current_uri
end
|
Returns the value of attribute headers.
12
13
14
|
# File 'lib/nitrous/integration_test.rb', line 12
def
@headers
end
|
#response ⇒ Object
Returns the value of attribute response.
12
13
14
|
# File 'lib/nitrous/integration_test.rb', line 12
def response
@response
end
|
#status ⇒ Object
Returns the value of attribute status.
12
13
14
|
# File 'lib/nitrous/integration_test.rb', line 12
def status
@status
end
|
Class Method Details
.start_server ⇒ Object
15
16
17
18
19
20
21
22
23
24
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/nitrous/integration_test.rb', line 15
def self.start_server
@server_thread = Thread.start do
options = {
:Port => SERVER_PORT,
:Host => "0.0.0.0",
:environment => (ENV['RAILS_ENV'] || "development").dup,
:config => RAILS_ROOT + "/config.ru",
:detach => false,
:debugger => false,
:path => nil
}
server = Rack::Handler::WEBrick
ENV["RAILS_ENV"] = options[:environment]
RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)
if File.exist?(options[:config])
config = options[:config]
if config =~ /\.ru$/
cfgfile = File.read(config)
if cfgfile[/^#\\(.*)/]
opts.parse!($1.split(/\s+/))
end
inner_app = eval("Rack::Builder.new {( " + cfgfile + "\n )}.to_app", nil, config)
else
require config
inner_app = Object.const_get(File.basename(config, '.rb').capitalize)
end
else
require RAILS_ROOT + "/config/environment"
inner_app = ActionController::Dispatcher.new
end
app = Rack::Builder.new {
use Rails::Rack::Debugger if options[:debugger]
map '/' do
use Rails::Rack::Static
run inner_app
end
}.to_app
trap(:INT) { exit }
server.run(app, options.merge(:AccessLog => [], :Logger => WEBrick::Log.new("/dev/null")))
end
sleep 0.001 until @server_thread.status == "sleep"
end
|
Instance Method Details
155
156
157
|
# File 'lib/nitrous/integration_test.rb', line 155
def assert_form_redisplayed!
fail("Expected form to redisplay. Redirected to <#{current_uri}>") unless @redisplay
end
|
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/nitrous/integration_test.rb', line 176
def assert_form_values!(id, data={})
id, data = nil, id if id.is_a?(Hash)
form = css_select(id ? "form##{id}" : "form").first
fail(id ? "Form not found with id <#{id}>" : "No form found") unless form
data.to_fields.each do |name, value|
form_fields = css_select form, "input, select, textarea"
matching_fields = form_fields.select {|field| (field["name"] == name || field["name"] == "#{name}[]") && (!%w(radio checkbox).include?(field['type']) || field['checked'] == 'checked')}
matching_field = matching_fields.detect {|f| f['checked'] == 'checked'} || matching_fields.first
fail "Could not find a form field having the name #{name}" unless matching_field
case matching_field.name.downcase
when 'input'
fail "Expected value of field #{name} to be #{value} but was #{matching_field['value']}" unless value.to_s == matching_field['value']
when 'textarea'
assert_equal value.to_s, matching_field.children.first.to_s
when 'select'
selected_option = css_select(matching_field, 'option[selected]').first
fail("No option selected for #{name}. Expected #{value} to be selected.") unless selected_option
assert_equal value.to_s, selected_option['value']
end
end
end
|
#assert_not_page_contains!(string) ⇒ Object
172
173
174
|
# File 'lib/nitrous/integration_test.rb', line 172
def assert_not_page_contains!(string)
fail("Expected page not to contain <#{string}> but it did. Page:\n#{response.body}") if response.body.include?(string.to_s)
end
|
#assert_page_contains!(string) ⇒ Object
168
169
170
|
# File 'lib/nitrous/integration_test.rb', line 168
def assert_page_contains!(string)
fail("Expected page to contain <#{string}> but it did not. Page:\n#{response.body}") unless response.body.include?(string.to_s)
end
|
#assert_viewing(request_uri, message = nil) ⇒ Object
163
164
165
166
|
# File 'lib/nitrous/integration_test.rb', line 163
def assert_viewing(request_uri, message=nil)
fail("Expected page but recieved redirect. <#{current_uri}>") unless success?
assert_match %r(#{Regexp.escape(request_uri)}(\?|&|$)), current_uri, message
end
|
#click_link(url, method = :get) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/nitrous/integration_test.rb', line 141
def click_link(url, method=:get)
if method == :delete
elements = css_select("*[href=#{url}][onclick]")
fail("No link found with url <#{url}> and method delete") if elements.empty? || !elements.any?{|element| element["onclick"] =~ /m.setAttribute\('name', '_method'\);.*?m.setAttribute\('value', 'delete'\);/}
delete url
follow_redirect! if redirect?
puts response.body if error?
assert !error?
else
fail("No link found with url <#{url}>") unless css_select("*[href=#{url}]").first
navigate_to(url)
end
end
|
#delete(path, parameters = nil, headers = {}) ⇒ Object
270
271
272
273
274
275
|
# File 'lib/nitrous/integration_test.rb', line 270
def delete(path, parameters=nil, ={})
['QUERY_STRING'] = requestify(parameters) || ""
process(, path) do
http_session.delete(path, )
end
end
|
#error? ⇒ Boolean
was there a server-side error?
309
310
311
|
# File 'lib/nitrous/integration_test.rb', line 309
def error?
(500..599).include?(status)
end
|
#existing_values(form) ⇒ Object
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/nitrous/integration_test.rb', line 201
def existing_values(form)
inputs = css_select(form, 'input').reject {|i| %w(checkbox radio).include?(i['type']) && (i['checked'].blank? || i['checked'].downcase != 'checked')}
values = {}
inputs.each do |input|
values[input['name']] = input['value']
end
css_select(form, 'textarea').each do |textarea|
values[textarea['name']] = textarea.children.map(&:to_s).join
end
css_select(form, 'select').each do |select|
selected = css_select(select, 'option[selected]').first || css_select(select, 'option').first
values[select['name']] = selected['value'] if selected
end
values.each{|k, v| values[k] = '' if v.nil?}
end
|
#field_value(name) ⇒ Object
159
160
161
|
# File 'lib/nitrous/integration_test.rb', line 159
def field_value(name)
css_select(html_document.root, "input, select, textarea").detect {|field| field["name"] == name}["value"]
end
|
#follow_redirect! ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/nitrous/integration_test.rb', line 233
def follow_redirect!
raise "not a redirect! #{@status} #{@status_message}" unless redirect?
location = URI.parse(['location'].first)
path = location.query ? "#{location.path}?#{location.query}" : location.path
domains = location.host.split('.')
subdomain = domains.length > 2 ? domains.first : nil
set_subdomain(subdomain) if subdomain != @subdomain
get(location.host.include?('localhost') ? path : ['location'].first)
status
end
|
#get(path, parameters = nil, headers = {}) ⇒ Object
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/nitrous/integration_test.rb', line 251
def get(path, parameters=nil, ={})
['QUERY_STRING'] = requestify(parameters) || ""
process(, path) do
if(!['QUERY_STRING'].blank?)
http_session.get(path + "?#{['QUERY_STRING']}", )
else
http_session.get(path, )
end
end
end
|
#html_document ⇒ Object
246
247
248
249
|
# File 'lib/nitrous/integration_test.rb', line 246
def html_document
xml = @response.content_type =~ /xml$/
@html_document ||= HTML::Document.new(@response.body, false, xml)
end
|
#missing? ⇒ Boolean
299
300
301
|
# File 'lib/nitrous/integration_test.rb', line 299
def missing?
status == 404
end
|
#multipart_encode(fields) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/nitrous/integration_test.rb', line 121
def multipart_encode(fields)
data = ""
fields.to_fields.each do |key, value|
data << "--#{BOUNDARY}\r\n"
if value.respond_to?(:read)
filename = File.basename(value.path)
data << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{filename}\"\r\n"
data << "Content-Transfer-Encoding: binary\r\n"
data << "Content-Type: #{MIME::Types.type_for(filename)}\r\n\r\n"
data << value.read
else
data << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
data << value.to_s
end
data << "\r\n"
end
data << "--#{BOUNDARY}--"
data
end
|
#navigate_to(path, headers = {}) ⇒ Object
83
84
85
86
87
88
|
# File 'lib/nitrous/integration_test.rb', line 83
def navigate_to(path, ={})
get path, nil,
follow_redirect! if redirect?
puts response.body if error?
assert !error?
end
|
#post(path, parameters = nil, headers = {}) ⇒ Object
262
263
264
265
266
267
268
|
# File 'lib/nitrous/integration_test.rb', line 262
def post(path, parameters=nil, ={})
data = requestify(parameters) || ""
['CONTENT_LENGTH'] = data.length.to_s
process(, path) do
http_session.post(path, data, )
end
end
|
#post_form(url, data = {}, method = :post) ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/nitrous/integration_test.rb', line 112
def post_form(url, data={}, method = :post)
fields = data.to_fields
if fields.values.any? {|v| v.respond_to?(:read)}
self.send(method, url, multipart_encode(fields), {'Content-Type' => "multipart/form-data, boundary=#{BOUNDARY}"})
else
self.send(method, url, fields)
end
end
|
#process(headers, path = nil) ⇒ Object
285
286
287
288
289
290
291
|
# File 'lib/nitrous/integration_test.rb', line 285
def process(, path=nil)
['Cookie'] = encode_cookies unless encode_cookies.blank?
self.response = yield
self.current_uri = path
@html_document = nil
parse_result
end
|
#put(path, parameters = nil, headers = {}) ⇒ Object
277
278
279
280
281
282
283
|
# File 'lib/nitrous/integration_test.rb', line 277
def put(path, parameters=nil, ={})
data = requestify(parameters) || ""
['CONTENT_LENGTH'] = data.length.to_s
process(, path) do
http_session.put(path, data, )
end
end
|
#redirect? ⇒ Boolean
304
305
306
|
# File 'lib/nitrous/integration_test.rb', line 304
def redirect?
(300..399).include?(status)
end
|
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/nitrous/integration_test.rb', line 91
def submit_form(id, data = {})
@redisplay = false
id, data = nil, id if id.is_a?(Hash)
form = css_select(id ? "form##{id}" : "form").first
fail(id ? "Form not found with id <#{id}>" : "No form found") unless form
validate = data.delete(:validate)
validate_form_fields(form, data) unless validate == false
fields = data.to_fields.reverse_merge(existing_values(form))
if form['enctype'] == 'multipart/form-data'
self.send(form["method"], form["action"], multipart_encode(fields), {'Content-Type' => "multipart/form-data, boundary=#{BOUNDARY}"})
else
self.send(form["method"], form["action"], fields)
end
puts response.body if error?
assert !error?
@redisplay = true if !redirect? && (id ? css_select("form##{id}").first : true)
follow_redirect! if redirect?
puts response.body if error?
assert !error?
end
|
#success? ⇒ Boolean
was the response successful?
294
295
296
|
# File 'lib/nitrous/integration_test.rb', line 294
def success?
status == 200
end
|
#url_for(options) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/nitrous/integration_test.rb', line 75
def url_for(options)
if options.delete(:only_path)
ActionController::Routing::Routes.generate(options)
else
"http://localhost:#{SERVER_PORT}" + ActionController::Routing::Routes.generate(options)
end
end
|
217
218
219
220
221
222
223
224
225
|
# File 'lib/nitrous/integration_test.rb', line 217
def validate_form_fields(form, data)
data.to_fields.each do |name, value|
form_fields = css_select form, "input, select, textarea"
matching_field = form_fields.detect {|field| field["name"] == name || field["name"] == "#{name}[]"}
fail "Could not find a form field having the name #{name}" unless matching_field
assert_equal 'file', matching_field['type'] if value.is_a?(File)
assert_equal "multipart/form-data", form["enctype"], "Form <#{form['id']}> has a file field <#{name}>, but the enctype is not multipart/form-data" if matching_field["type"] == "file"
end
end
|
#view(email) ⇒ Object
227
228
229
230
231
|
# File 'lib/nitrous/integration_test.rb', line 227
def view(email)
@response = ActionController::TestResponse.new
@response.body = email.body
@html_document = nil
end
|