File: helper.rb

package info (click to toggle)
ruby-rack-google-analytics 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 164 kB
  • sloc: ruby: 287; makefile: 9
file content (47 lines) | stat: -rwxr-xr-x 1,357 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
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
43
44
45
46
47
require 'rubygems'
require 'test/unit'
require 'shoulda'
require 'rack/test'
require 'active_support/core_ext/hash/slice'
require 'rack/google-analytics'
require 'tracking/event'

class Test::Unit::TestCase
  include Rack::Test::Methods

  def app;
    Rack::Lint.new(@app);
  end

  def main_app(options)
    lambda { |env|

      env["google_analytics.event_tracking"] = options[:events] if options[:events]
      env["google_analytics.custom_vars"] = options[:custom_vars] if options[:custom_vars]
      env["misc"] = options[:misc] if options[:misc]

      request = Rack::Request.new(env)
      case request.path
        when '/' then
          [200, {'Content-Type' => 'application/html'}, ['<head>Hello world</head>']]
        when '/test.xml' then
          [200, {'Content-Type' => 'application/xml'}, ['Xml here']]
        when '/bob' then
          [200, {'Content-Type' => 'application/html'}, ['<body>bob here</body>']]
        when '/redirect' then
          [302, {'Content-Type' => 'application/html'}, ['<body>redirection</body>']]
        else
          [404, 'Nothing here']
      end
    }
  end

  def mock_app(options)
    app_options = options.slice(:events, :custom_vars, :misc)

    builder = Rack::Builder.new
    builder.use Rack::GoogleAnalytics, options
    builder.run main_app(app_options)
    @app = builder.to_app
  end
end