hanami / controller

Complete, fast and testable actions for Rack and Hanami

Home Page:http://hanamirb.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test returns 405 when using `send_file`

nerdinand opened this issue · comments

When using send_file in an action, then running the corresponding test, the test fails with 405 Method Not Allowed.

Minimal example:

# apps/web/controllers/books/show.rb

module Web::Controllers::Books
  class Show
    include Web::Action

    def call(params)
      send_file('.gitkeep')
    end
  end
end
# spec/web/controllers/books/show_spec.rb

require 'spec_helper'

describe Web::Controllers::Books::Show do
  let(:action) { Web::Controllers::Books::Show.new }
  let(:params) { Hash[] }

  it 'is successful' do
    response = action.call(params)
    expect(response[0]).to eq(200)
  end
end

Running be rspec spec/web/controllers/books/show_spec.rb returns:

/Users/ferdi/.rbenv/versions/jruby-9.1.13.0/lib/ruby/gems/shared/gems/rom-sql-1.3.3/lib/rom/sql/extensions/postgres/inferrer.rb:37: warning: constant Sequel::Postgres::PGArray::EMPTY_BRACKET is deprecated
F

Failures:

  1) Web::Controllers::Books::Show is successful
     Failure/Error: expect(response[0]).to eq(200)
     
       expected: 200
            got: 405
     
       (compared using ==)
     # ./spec/web/controllers/books/show_spec.rb:9:in `block in (root)'

Finished in 0.05953 seconds (files took 4.1 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/web/controllers/books/show_spec.rb:7 # Web::Controllers::Books::Show is successful

I did some digging. It seems the @env variable in


looks different in tests than in the normal code. Adding

          if env['REQUEST_METHOD'].nil?
            env['REQUEST_METHOD'] = 'GET'
          end

in that method fixes the test.

Could anyone have a look at this, please?

@nerdinand Thanks for reporting this, can you please review if the fix works for you? #244