Class: Relevance::Tarantula::RailsIntegrationProxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Relevance::Tarantula
Includes:
Relevance::Tarantula
Defined in:
lib/relevance/tarantula/rails_integration_proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relevance::Tarantula

log, rails_root, tarantula_home, verbose

Constructor Details

#initialize(integration_test) ⇒ RailsIntegrationProxy

Returns a new instance of RailsIntegrationProxy.



29
30
31
32
# File 'lib/relevance/tarantula/rails_integration_proxy.rb', line 29

def initialize(integration_test)
  @integration_test = integration_test
  @integration_test.meta.attr_accessor :response
end

Instance Attribute Details

#integration_testObject

Returns the value of attribute integration_test.



7
8
9
# File 'lib/relevance/tarantula/rails_integration_proxy.rb', line 7

def integration_test
  @integration_test
end

Class Method Details

.rails_integration_test(integration_test, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/relevance/tarantula/rails_integration_proxy.rb', line 9

def self.rails_integration_test(integration_test, options = {})
  t = Crawler.new
  t.max_url_length = options[:max_url_length] if options[:max_url_length] 
  t.proxy = RailsIntegrationProxy.new(integration_test)    
  t.handlers << HtmlDocumentHandler.new(t)
  t.handlers << InvalidHtmlHandler.new
  t.log_grabber = Relevance::Tarantula::LogGrabber.new(File.join(rails_root, "log/test.log"))
  t.skip_uri_patterns << /logout$/
  t.skip_uri_patterns.delete /^http/
  t.skip_uri_patterns.push /^https?:\/\/(?!#{integration_test.host})/

  t.transform_url_patterns += [
    [/\?\d+$/, ''],                               # strip trailing numbers for assets
    [/^https?:\/\/#{integration_test.host}/, '']  # strip full path down to relative
  ]
  t.test_name = t.proxy.integration_test.method_name
  t.reporters << Relevance::Tarantula::HtmlReporter.new(t.report_dir)
  t
end

Instance Method Details

#process(response, verb, url, *args) ⇒ Object



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
73
74
75
76
77
78
# File 'lib/relevance/tarantula/rails_integration_proxy.rb', line 46

def process(response, verb, url, *args)
  if verb == :get && response.code == '302'
    location = response.headers['Location']

    if location =~ /#{url}$/ # Schema change
      integration_test.https!(!!(location =~ /^https/))
      response = send(verb, url, *args)
    elsif location =~ /#!?/  # AJAX load
      response = send(:xhr, verb, url, *args)
    end

  elsif response.code == '404'
    if File.exist?(static_content_path(url))
      case ext = File.extension(url)
      when /html|te?xt|css|js|jpe?g|gif|psd|png|eps|pdf|ico/
        response.body = static_content_file(url)
        response.headers["type"] = "text/#{ext}"  # readable as response.content_type
        response.meta.attr_accessor :code
        response.code = "200"
      else
        log "Skipping unknown type #{url}"
      end
    end
  end

  # don't count on metaclass taking block, e.g.
  # http://relevancellc.com/2008/2/12/how-should-metaclass-work
  response.metaclass.class_eval do
    include Relevance::CoreExtensions::Response
  end

  return response
end

#static_content_file(url) ⇒ Object



80
81
82
# File 'lib/relevance/tarantula/rails_integration_proxy.rb', line 80

def static_content_file(url)
  File.read(static_content_path(url))
end

#static_content_path(url) ⇒ Object



84
85
86
# File 'lib/relevance/tarantula/rails_integration_proxy.rb', line 84

def static_content_path(url)
  File.expand_path(File.join(rails_root, "public", url))
end

#xhr(verb, url, *args) ⇒ Object



41
42
43
44
# File 'lib/relevance/tarantula/rails_integration_proxy.rb', line 41

def xhr(verb, url, *args)
  integration_test.send(:xhr, verb, url, *args)
  process integration_test.response, verb, url, *args
end