问题
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