bare_metal_test.rb 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
require "abstract_unit"

module BareMetalTest
  class BareController < ActionController::Metal
    def index
      self.response_body = "Hello world"
    end
  end

  class BareTest < ActiveSupport::TestCase
    test "response body is a Rack-compatible response" do
      status, headers, body = BareController.action(:index).call(Rack::MockRequest.env_for("/"))
      assert_equal 200, status
      string = ""

      body.each do |part|
        assert part.is_a?(String), "Each part of the body must be a String"
        string << part
      end

21
      assert_kind_of Hash, headers, "Headers must be a Hash"
22 23 24 25
      assert headers["Content-Type"], "Content-Type must exist"

      assert_equal "Hello world", string
    end
26 27 28 29 30 31

    test "response_body value is wrapped in an array when the value is a String" do
      controller = BareController.new
      controller.index
      assert_equal ["Hello world"], controller.response_body
    end
32
  end
33 34 35 36 37 38 39 40 41 42 43

  class HeadController < ActionController::Metal
    include ActionController::Head

    def index
      head :not_found
    end
  end

  class HeadTest < ActiveSupport::TestCase
    test "head works on its own" do
44
      status = HeadController.action(:index).call(Rack::MockRequest.env_for("/")).first
45 46 47
      assert_equal 404, status
    end
  end
48 49 50 51 52 53 54

  class BareControllerTest < ActionController::TestCase
    test "GET index" do
      get :index
      assert_equal "Hello world", @response.body
    end
  end
55
end