Sha256: e064372ae76bb64df90093fd17f7717531a41f2db5d9086086673dea5ca6f839

Contents?: true

Size: 1.98 KB

Versions: 9

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "rack/test"
require "stringio"

RSpec.describe "Hanami web app: Method Override", :app_integration do
  include Rack::Test::Methods

  let(:app) { Hanami.app }

  around do |example|
    with_tmp_directory(Dir.mktmpdir, &example)
  end

  context "enabled by default" do
    before do
      write "config/app.rb", <<~RUBY
        require "hanami"

        module TestApp
          class App < Hanami::App
            config.logger.stream = StringIO.new
          end
        end
      RUBY

      generate_app_code
    end

    it "overrides the original HTTP method" do
      post(
        "/users/:id",
        {"_method" => "PUT"},
        "CONTENT_TYPE" => "multipart/form-data"
      )

      expect(last_response).to be_successful
      expect(last_response.body).to eq("PUT")
    end
  end

  context "when disabled" do
    before do
      write "config/app.rb", <<~RUBY
        require "hanami"

        module TestApp
          class App < Hanami::App
            config.logger.stream = StringIO.new
            config.actions.method_override = false
          end
        end
      RUBY

      generate_app_code
    end

    it "overrides the original HTTP method" do
      post(
        "/users/:id",
        {"_method" => "PUT"},
        "CONTENT_TYPE" => "multipart/form-data"
      )

      expect(last_response).to_not be_successful
      expect(last_response.status).to be(404)
    end
  end

  private

  def generate_app_code
    write "config/routes.rb", <<~RUBY
      module TestApp
        class Routes < Hanami::Routes
          put "/users/:id", to: "users.update"
        end
      end
    RUBY

    write "app/actions/users/update.rb", <<~RUBY
      module TestApp
        module Actions
          module Users
            class Update < Hanami::Action
              def handle(req, res)
                res.body = req.env.fetch("REQUEST_METHOD")
              end
            end
          end
        end
      end
    RUBY

    require "hanami/boot"
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hanami-2.2.1 spec/integration/rack_app/method_override_spec.rb
hanami-2.2.0 spec/integration/rack_app/method_override_spec.rb
hanami-2.2.0.rc1 spec/integration/rack_app/method_override_spec.rb
hanami-2.2.0.beta2 spec/integration/rack_app/method_override_spec.rb
hanami-2.2.0.beta1 spec/integration/rack_app/method_override_spec.rb
hanami-2.1.0 spec/integration/rack_app/method_override_spec.rb
hanami-2.1.0.rc3 spec/integration/rack_app/method_override_spec.rb
hanami-2.1.0.rc2 spec/integration/rack_app/method_override_spec.rb
hanami-2.1.0.rc1 spec/integration/rack_app/method_override_spec.rb