Rails RSpec 请求规范失败,因为在重定向响应中添加了意外的 %2F [英] Rails RSpec request spec fails because unexpected %2F added to redirect response

查看:37
本文介绍了Rails RSpec 请求规范失败,因为在重定向响应中添加了意外的 %2F的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 config/routes.rb:

In config/routes.rb:

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

在 spec/requests/store_request_spec.rb 中:

In spec/requests/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 被插入到重定向响应中,我该如何防止?

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

如果我使用:

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

要创建一个未转义的字符串,我仍然遇到相同的错误.

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

推荐答案

删除了没有帮助的旧答案.这有效并经过测试:

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

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 请求规范失败,因为在重定向响应中添加了意外的 %2F的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆