Module: Ajax::RSpec::Helpers

Defined in:
lib/ajax/rspec/helpers.rb

Instance Method Summary collapse

Instance Method Details

#call_rack(url, request_method = 'GET', env = {}, &block) ⇒ Object



10
11
12
13
14
# File 'lib/ajax/rspec/helpers.rb', line 10

def call_rack(url, request_method='GET', env={}, &block)
  env(url, request_method, env)
  @rack = Rack::Ajax.new(@app, &block)
  @response = @rack.call(@env)
end

#create_appObject



6
7
8
# File 'lib/ajax/rspec/helpers.rb', line 6

def create_app
  @app = Class.new { def call(env); true; end }.new
end

#env(uri, request_method, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/ajax/rspec/helpers.rb', line 67

def env(uri, request_method, options={})
  uri = URI.parse(uri)
  @env = {
    'REQUEST_URI' => uri.to_s,
    'PATH_INFO' => uri.path,
    'QUERY_STRING' => uri.query,
    'REQUEST_METHOD' => request_method
  }.merge!(options)
end

#response_bodyObject



77
78
79
# File 'lib/ajax/rspec/helpers.rb', line 77

def response_body
  @response.is_a?(Array) ? @response[2][0] : @response.body
end

#response_body_as_hashObject



89
90
91
# File 'lib/ajax/rspec/helpers.rb', line 89

def response_body_as_hash
  @response_body_as_hash ||= YAML.load(response_body)
end

#response_codeObject



81
82
83
# File 'lib/ajax/rspec/helpers.rb', line 81

def response_code
  @response.is_a?(Array) ? @response[0] : @response.status.to_i
end

#response_headersObject



85
86
87
# File 'lib/ajax/rspec/helpers.rb', line 85

def response_headers
  @response.is_a?(Array) ? @response[1] : @response.headers.to_hash
end

#should_be_a_valid_responseObject

Response must be [code, headers, [‘Response’]] Headers must contain the Content-Type header



56
57
58
59
60
61
62
63
64
65
# File 'lib/ajax/rspec/helpers.rb', line 56

def should_be_a_valid_response
  return if !@response.is_a?(Array) # ::ActionController::Response
  @response.should be_a(Array)
  @response.size.should == 3
  @response[0].should be_a(Integer)
  @response[1].should be_a(Hash)
  @response[1]['Content-Type'].should =~ %r[^text\/\w+]
  @response[2].should be_a(Array)
  @response[2][0].should be_a(String)
end

#should_not_modify_requestObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ajax/rspec/helpers.rb', line 42

def should_not_modify_request
  should_be_a_valid_response
  response_code.should == 200

  # If we have the original headers from a call to call_rack()
  # check that they haven't changed.  Otherwise, just make sure
  # that we don't have the custom rewrite header.
  if !@env.nil?
    @env.each { |k,v| response_body_as_hash.should == v }
  end
end

#should_redirect_to(location, code = 302) ⇒ Object



21
22
23
24
25
# File 'lib/ajax/rspec/helpers.rb', line 21

def should_redirect_to(location, code=302)
  should_be_a_valid_response
  response_code.should == code
  response_headers['Location'].should == location
end

#should_respond_with(msg) ⇒ Object



16
17
18
19
# File 'lib/ajax/rspec/helpers.rb', line 16

def should_respond_with(msg)
  should_be_a_valid_response
  response_body.should == msg
end

#should_rewrite_to(url) ⇒ Object



35
36
37
38
39
40
# File 'lib/ajax/rspec/helpers.rb', line 35

def should_rewrite_to(url)
  should_be_a_valid_response

  # Check custom headers
  response_body_as_hash['REQUEST_URI'].should == url
end

#should_set_ajax_request_header(key, value) ⇒ Object



31
32
33
# File 'lib/ajax/rspec/helpers.rb', line 31

def should_set_ajax_request_header(key, value)
  Ajax.get_header(@env, key).should == value
end

#should_set_ajax_response_header(key, value) ⇒ Object



27
28
29
# File 'lib/ajax/rspec/helpers.rb', line 27

def should_set_ajax_response_header(key, value)
  response_headers['Ajax-Info'][key].should == value
end