使用 serverspec 测试重定向

Testing redirects using serverspec

我想在 serverspec 中为 nginx 重写调用创建一个测试,如下所示(它将对 ^/folder/file.json URL 的请求重定向到@redirect_url 变量):

rewrite ^/folder/file.json <%= @redirect_url %> permanent;

对于我正在更新的 Chef 食谱。不幸的是,从我在互联网上找到的内容来看,它要么强调了我完全不了解 nginx 和 serverspec 术语,要么强调了你做不到的事实。有没有人可以帮忙?

使用 curl 命令的重定向 severspec 是这样的:

redirect_url = "..."
describe command("curl -I http://example.com/folder/file.json") do
  its(:stdout) { should match(%r|HTTP/1.1 301 Moved Permanently|) }
  its(:stdout) { should match(%r|Location: #{Regexp.escape(redirect_url)}|) }
end

您可以直接使用Ruby的Net::HTTP

查看响应代码
describe Net::HTTP.get_response(URI('http://localhost:port/path')) do
  its(:code) { should eq '301' }
  its(:msg) { should match 'Moved Permanently' }
end