RSpec - 如何测试响应头的键和值?

时间:2014-05-14 15:58:23

标签: ruby-on-rails rspec http-headers response

我正在我的Rails中添加配置API以使用GZIP压缩并且正在运行,但我在使用rspec时遇到了麻烦。

config.ru

use Rack::Deflater

application.rb中

config.middleware.use Rack::Deflater

users_controller_spec.rb

context "GZIP" do
    it "produces an identical eTag whether content is deflated or not" do
      get :users, format: 'json', 'HTTP_AUTHORIZATION'=>"Token token=\"#{user.token}\""

      expect(response.headers).to have_key("Content-Length")
      expect(response.headers).to have_key("Etag")
      expect(response.headers).to_not have_key("Content-Encoding")
      expect(response.headers["Content-Encoding"]).to be_nil

      etag = response.headers["Etag"]
      content_length = response.headers["Content-Length"].to_i

      get :users, format: 'json', 'HTTP_AUTHORIZATION'=>"Token token=\"#{user.token}\"", "HTTP_ACCEPT_ENCODING" => "gzip"

      expect(response.headers["Etag"]).to eql(etag)
      expect(response.headers["Content-Length"].to_i).to be < content_length
      expect(response.headers).to have_key("Content-Encoding")
      expect(response.headers["Content-Encoding"]).to eql("gzip")
    end
  end

当我在response.headers上测试密钥时,规范失败了。我想我的回答没有任何关键。但是当我在浏览器中测试时,我在响应标题

中看到了这个结果
Cache-Control:max-age=0, private, must-revalidate
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
ETag:"e45c1e970b7fb10b35c9acf52bfb64e7"
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Request-Id:04ae0c72-21e5-4ada-bdf3-63d2815254f0
X-Runtime:0.734614
X-UA-Compatible:chrome=1
X-XSS-Protection:1; mode=block

有没有办法测试响应头的键和值?我错过了什么?

谢谢。

0 个答案:

没有答案