Rails RSpec request spec fails because unexpected %2F added to redirect response

寵の児 提交于 2019-12-24 17:08:09

问题


In config/routes.rb:

get 'books(/*anything)' => redirect( "/public/%{anything}" ), :format => false

In spec/requests/store_request_spec.rb:

get '/books/aoeu/snth'
expect(response).to redirect_to( '/public/aoeu/snth' )

This fails with:

Failure/Error: expect(response).to redirect_to( '/public/aoeu/snth' )
   Expected response to be a redirect to <http://www.example.com/public/aoeu/snth> but was a redirect to <http://www.example.com/public/aoeu%2Fsnth>.
Expected "http://www.example.com/public/aoeu/snth" to be === "http://www.example.com/public/aoeu%2Fsnth".
 # ./spec/requests/store_request_spec.rb:14:in `block (3 levels) in <top (required)>'

Why is the %2F being inserted into the redirect response and how do I prevent it?

Edit:

If I use:

get 'books(/*anything)' => redirect( CGI::unescape("/public/%{anything}") ), :format => false

to create an unescaped string I still get the same error.


回答1:


Deleted old answer as not helpful. This works and is tested:

In routes.rb:

get 'books/*anything', to: redirect { |params, request|
  path = CGI.unescape(params[:anything])
  "http://#{request.host_with_port}/public/#{path}"
}

In spec/requests/store_request_spec.rb:

describe "books globbed route" do
  before { get('/books/abcd/efgh') }
  it "routes to public" do

    expect(response).to redirect_to('/public/abcd/efgh')

  end
end

Run bundle exec rspec spec/requests/store_request_spec.rb # =>

Store
  books globbed route
    routes to public

Finished in 0.15973 seconds
1 example, 0 failures

Randomized with seed 15579


来源:https://stackoverflow.com/questions/21517431/rails-rspec-request-spec-fails-because-unexpected-2f-added-to-redirect-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!