因为意想不到%2F加入到重定向响应Rails的RSpec的要求规范失败(Rails RSpec re

2019-09-29 06:39发布

在配置/ routes.rb中:

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

在规格/请求/ store_request_spec.rb:

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

这种失败:

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)>'

为何%2F被插入到重定向响应,如何预防呢?

编辑:

如果我使用:

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

创建一个转义字符串我仍然得到同样的错误。

Answer 1:

已删除旧的答案没有帮助。 这个工程并进行测试:

routes.rb

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

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

运行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


文章来源: Rails RSpec request spec fails because unexpected %2F added to redirect response