Rails RSpec request spec doesn't work because unexpected% 2F was added to redirect response

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:

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% 2F being inserted into the redirect response and how can I prevent it?

Edit:

If I use:

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

      

to create a string without saving, I still get the same error.

+1


source to share


1 answer


The deleted old answer doesn't help. 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

      

+2


source







All Articles