gpt4 book ai didi

ruby-on-rails - GZIPed 响应的 Rspec 测试

转载 作者:行者123 更新时间:2023-12-04 06:25:56 24 4
gpt4 key购买 nike

我最近在此 Thoughtbot blog post 之后在我的 Rails 4 应用程序上启用了 GZIP我还添加了 use Rack::Deflater按照 this post 的建议添加到我的 config.ru 文件中.我的 Rails 应用程序似乎提供压缩内容,但是当我使用 RSpec 对其进行测试时,测试失败,因为 response.headers['Content-Encoding']为零。

这是我的 application.rb:

module MyApp
class Application < Rails::Application
# Turn on GZIP compression
config.middleware.use Rack::Deflater
end
end

这是我的规范:
require 'rails_helper'

describe GeneralController, type: :controller, focus: true do
it "a visitor has a browser that supports compression" do
['deflate', 'gzip', 'deflate,gzip', 'gzip,deflate'].each do |compression_method|
get 'about', {}, {'HTTP_ACCEPT_ENCODING' => compression_method }
binding.pry
expect(response.headers['Content-Encoding']).to be
end
end

it "a visitor's browser does not support compression" do
get 'about'
expect(response.headers['Content-Encoding']).to_not be
end
end

当我跑 curl --head -H "Accept-Encoding: gzip" http://localhost:3000/我得到以下输出:
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Ua-Compatible: chrome=1
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Content-Encoding: gzip
Etag: "f7e364f21dbb81b9580cd39e308a7c15"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 3f018f27-40ab-4a87-a836-67fdd6bd5b6e
X-Runtime: 0.067748
Server: WEBrick/1.3.1 (Ruby/2.0.0/2014-02-24)

当我加载站点并查看检查器的网络选项卡时,我可以看到响应大小比以前小,但我的测试仍然失败。我不确定我的测试是否遗漏了一个步骤,或者我的 Rack::Deflater 的实现是否存在问题.

最佳答案

正如@andy-waite 所指出的,RSpec Controller 规范不知道中间件,但这就是为什么,自从 RSpec 2.6 我们有 request specs .

根据文档,请求规范是:

designed to drive behavior through the full stack



因此,使用 RSpec > 2.6 请求规范,您的代码应如下所示:
require 'rails_helper'

describe GeneralController, type: :request, focus: true do
it "a visitor has a browser that supports compression" do
['deflate', 'gzip', 'deflate,gzip', 'gzip,deflate'].each do |compression_method|
get 'about', {}, {'HTTP_ACCEPT_ENCODING' => compression_method }
binding.pry
expect(response.headers['Content-Encoding']).to be
end
end

it "a visitor's browser does not support compression" do
get 'about'
expect(response.headers['Content-Encoding']).to_not be
end
end

关于ruby-on-rails - GZIPed 响应的 Rspec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30516523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com