Rspec test for gziped response

I recently included GZIP in my Rails 4 application after this Thoughtbot blog post , and I also added the use Rack::Deflaterfile suggested by this post to my config.ru . My Rails seems to be serving compressed content, but when I test it with RSpec, the test fails because it response.headers['Content-Encoding']is zero.

Here is my .rb application:

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

Here is my specification:

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 browser does not support compression" do
        get 'about'
        expect(response.headers['Content-Encoding']).to_not be
    end
end

When I run curl --head -H "Accept-Encoding: gzip" http://localhost:3000/, I get the following output:

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)

When I load the site and look at the Inspector’s Network tab, I see that the response size is smaller than before, but my test still does not work. I'm not sure if I skipped a step here with my test or if there is a problem with my implementation Rack::Deflater.

+4
2

@andy-waite, RSpec , , RSpec 2.6 .

, :

, 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 browser does not support compression" do
    get 'about'
    expect(response.headers['Content-Encoding']).to_not be
  end
end
+2

RSpec Rails, :

Rails Rack Rails

0

All Articles