2

Rspec に https を使用してルートを要求してもらいたい。これは私が現在持っているものです:

it "requesting root (/) with HTTPS should return 200" do
  get "https://test.host/"
  last_response.should be_ok
end

RSpec Rails で HTTPS (SSL) リクエストをテストするで提案されている解決策を試みると、次のエラーが返されます。

wrong number of arguments (0 for 1)

私は次のようなことができると思います:

get "/", :https => "on"

ホストを指定せずにルートを要求する方法はありますか?

4

2 に答える 2

5

When using Sinatra and RSpec, you are using the #get/#post methods provided by Rack::Test, so I suggest you look there to see how they work: https://github.com/brynary/rack-test/blob/master/lib/rack/test.rb

As you can see, they take an optional env Hash, where you can set "HTTPS" to "on", like this:

get '/', {}, {'HTTPS' => 'on'}

If you want to set it by default for all your requests, you need to override the Rack::Test::Session#default_env method, and had 'HTTPS' => 'on' to it (I suggest doing it in your spec_helper.rb), like this:

class Rack::Test::Session
  def default_env
    { "rack.test" => true, "REMOTE_ADDR" => "127.0.0.1", "HTTPS" => 'on' }.merge(headers_for_env)
  end
end
于 2013-04-18T18:26:35.673 に答える