Module: ActionDispatch::Assertions::ResponseAssertions

Includes:
ExpectationSerializer
Defined in:
lib/expected_responses.rb

Overview

A small suite of assertions that test responses from Rails applications.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExpectationSerializer

#expected_response_root, #file_path, #meth_name

Instance Attribute Details

#batchObject

Returns the value of attribute batch.



11
12
13
# File 'lib/expected_responses.rb', line 11

def batch
  @batch
end

#outputObject

Returns the value of attribute output.



11
12
13
# File 'lib/expected_responses.rb', line 11

def output
  @output
end

Instance Method Details

#assert_response(type, message = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/expected_responses.rb', line 43

def assert_response(type, message = nil)
  save_response(type, message)
  validate_request!

  if type.in?([:success, :missing, :redirect, :error]) && @response.send("#{type}?")
    assert_block("") { true } # to count the assertion

  elsif type.is_a?(Fixnum) && @response.response_code == type
    assert_block("") { true } # to count the assertion

  elsif type.is_a?(Symbol) && @response.response_code == Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
    assert_block("") { true } # to count the assertion

  else
    flunk(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code))
  end
end

#binary?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/expected_responses.rb', line 8

def binary?
  @response.headers['Content-Transfer-Encoding'] == 'binary'
end

#save_response(type, message = nil, path = '') ⇒ Object



12
13
14
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
# File 'lib/expected_responses.rb', line 12

def save_response(type, message = nil, path = '')
  format = @request.parameters[:format] if @request and @request.parameters
  self.output||= 'expected_responses' # GetText.locale.to_s == "en" ? 'expected_views' : 'html'

  format ||= @response.headers['Content-Type'].split(';').first.split('/').first if @response.headers['Content-Type'] and @response.headers['Content-Type'].split('/').last == 'plain'
  format ||= @response.headers['Content-Disposition'].gsub('"','').split('.').last if @response.headers['Content-Disposition']
  format ||= @response.headers['type'].split(';').first.split('/').last if binary? and @response.headers['type']
  format ||= @response.headers['Content-Type'].split(';').first.split('/').last if @response.headers['Content-Type']
  format ||= 'html'
  binary = binary? ? "wb" : "w"
  if type == :success #and ((format == 'application/pdf' and @request.path_parameters['action'] == 'show') or (@request.path_parameters['controller'] == 'sites/registrations'))

    FileUtils.mkdir_p "#{expected_response_root}/#{output}/#{self.file_path}/" unless File.exists? "#{expected_response_root}/#{output}/#{self.file_path}/"
    unless batch
      file_name = "#{expected_response_root}/#{output}/#{file_path}/#{self.meth_name}.#{format}"
    else
      file_name = "#{expected_response_root}/#{output}/#{file_path}/#{self.meth_name}_#{@request.path_parameters['id']||@request.path_parameters['payment_id']||@request.path_parameters['congress_id']}.#{format}"
    end
    f = File.new(file_name, binary)
    if format == 'html' and defined? RailsTidy and RailsTidy.tidy_path
      RailsTidy.filter(@response)
    end
    if @response.body.is_a? Proc
      @response.body.call(@response, f)
    else
      f.write(@response.body)
    end
    f.close
  elsif type == :redirect or type == :missing
    file_name = "#{expected_response_root}/#{output}/#{file_path}/#{self.meth_name}.#{format}"
    File.delete(file_name) if File.exists? file_name
  end
end