aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/metal_test.rb
blob: 225bede117e54ac0fa16226867a3484b768262a2 (plain) (blame)
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
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
79
80
81
82
83
84
85
86
require 'isolation/abstract_unit'

module ApplicationTests
  class MetalTest < Test::Unit::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails

      require 'rack/test'
      extend Rack::Test::Methods
    end

    def app
      @app ||= begin
        require "#{app_path}/config/environment"
        Rails.application
      end
    end

    test "single metal endpoint" do
      app_file 'app/metal/foo_metal.rb', <<-RUBY
        class FooMetal
          def self.call(env)
            [200, { "Content-Type" => "text/html"}, ["FooMetal"]]
          end
        end
      RUBY

      get "/"
      assert_equal 200, last_response.status
      assert_equal "FooMetal", last_response.body
    end

    test "multiple metal endpoints" do
      app_file 'app/metal/metal_a.rb', <<-RUBY
        class MetalA
          def self.call(env)
            [404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Metal A"]]
          end
        end
      RUBY

      app_file 'app/metal/metal_b.rb', <<-RUBY
        class MetalB
          def self.call(env)
            [200, { "Content-Type" => "text/html"}, ["Metal B"]]
          end
        end
      RUBY

      get "/"
      assert_equal 200, last_response.status
      assert_equal "Metal B", last_response.body
    end

    test "pass through to application" do
      app_file 'app/metal/foo_metal.rb', <<-RUBY
        class FooMetal
          def self.call(env)
            [404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Not Found"]]
          end
        end
      RUBY

      controller :foo, <<-RUBY
        class FooController < ActionController::Base
          def index
            render :text => "foo"
          end
        end
      RUBY

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do |map|
          match ':controller(/:action)'
        end
      RUBY

      get "/foo"
      assert_equal 200, last_response.status
      assert_equal "foo", last_response.body
    end
  end
end