Skip to content

2.1.0 - Content-Length no longer present in response's specs #1472

@erwanlr

Description

@erwanlr

After updating to 2.1.0, all my tests checking for the Content-Length header in responses are failing. It seems like the header is not set anymore somehow.

Here is an example:

# config.ru
# frozen_string_literal: true

run lambda { |_| [200, {}, ['Hello, world!']] }
# spec.rb
require 'rspec'
require 'rspec/its'
require 'rack'
require 'rack/test'

RSpec.configure do |config|
  config.include Rack::Test::Methods
end

describe 'App' do
  def app
    Rack::Builder.parse_file('config.ru').first
  end

  context 'when GET /' do
    subject(:res) { get('/') }

    its(:status) { is_expected.to be 200 }
    its(:headers) { are_expected.to include('Content-Length' => '13') }
    its(:body) { is_expected.to eql 'Hello, world!' }
  end
end
# test.rb
require 'minitest/global_expectations/autorun'
require 'rack/mock'

describe 'App' do
  def app
    Rack::Builder.parse_file('config.ru').first
  end

  def request
    Rack::MockRequest.new(app)
  end

  it 'GET /' do
    response = request.get('/')

    response.status.must_equal 200
    response.header['Content-Length'].must_equal '13'
    response.body.must_equal 'Hello, world!'
  end
end

Gemfile

# frozen_string_literal: true

source 'https://rubygems.org'

gem 'rack', '~> 2.1.1'
#gem 'rack', git: 'https://github.com/rack/rack.git'

gem 'rack-test', '~> 1.1.0'
gem 'rspec', '~> 3.9.0'
gem 'rspec-its', '~> 1.3.0'

gem 'minitest', '~> 5.14.0'
gem 'minitest-global_expectations', '~> 1.0.1'

Put these files in a same directory and run rspec spec.rb if using RSpec or bundle exec ruby test.rb to use Minitest

Output with Rack 2.1.0, and Rack-Test 1.1.0:

$ rspec spec.rb 
.F.

Failures:

  1) App when GET / headers is expected to include {"Content-Length" => "10"}
     Failure/Error: its(:headers) { are_expected.to include('Content-Length' => '10') }
     
       expected {} to include {"Content-Length" => "10"}
       Diff:
       @@ -1,2 +1 @@
       -"Content-Length" => "10",
       
     # ./spec.rb:19:in `block (3 levels) in <top (required)>'

Finished in 0.04414 seconds (files took 0.16756 seconds to load)
3 examples, 1 failure

Failed examples:

rspec ./spec.rb:19 # App when GET / headers is expected to include {"Content-Length" => "10"}

Output with Rack 2.0.8 and Rack-Test 1.1.0:

$ rspec spec.rb 
...

Finished in 0.02087 seconds (files took 0.13891 seconds to load)
3 examples, 0 failures

I don't really know where the issue comes from, but if I put use Rack::ContentLength in the config.ru, specs pass with 2.1.0.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions