Skip to content

Commit 1b4f510

Browse files
authored
Rack 3.1 fixes (#2449)
* Add Grape::Http::Headers::TRANSFER_ENCODING Fix spec regarding content-length on stream * Add CHANGELOG.md
1 parent 848e97a commit 1b4f510

File tree

9 files changed

+26
-12
lines changed

9 files changed

+26
-12
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
fail-fast: false
2525
matrix:
2626
ruby: ['2.7', '3.0', '3.1', '3.2', '3.3']
27-
gemfile: [Gemfile, gemfiles/rack_2_0.gemfile, gemfiles/rack_3_0.gemfile, gemfiles/rails_6_0.gemfile, gemfiles/rails_6_1.gemfile, gemfiles/rails_7_0.gemfile, gemfiles/rails_7_1.gemfile]
27+
gemfile: [Gemfile, gemfiles/rack_2_0.gemfile, gemfiles/rack_3_0.gemfile, gemfiles/rack_3_1.gemfile, gemfiles/rails_6_0.gemfile, gemfiles/rails_6_1.gemfile, gemfiles/rails_7_0.gemfile, gemfiles/rails_7_1.gemfile]
2828
specs: ['spec --exclude-pattern=spec/integration/**/*_spec.rb']
2929
include:
3030
- ruby: '2.7'

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* [#2378](https://github.com/ruby-grape/grape/pull/2378): Do not overwrite `route_param` with a regular one if they share same name - [@arg](https://github.com/arg).
4747
* [#2444](https://github.com/ruby-grape/grape/pull/2444): Replace method_missing in endpoint - [@ericproulx](https://github.com/ericproulx).
4848
* [#2441](https://github.com/ruby-grape/grape/pull/2441): Optimize memory alloc and retained - [@ericproulx](https://github.com/ericproulx).
49+
* [#2449](https://github.com/ruby-grape/grape/pull/2449): Rack 3.1 fixes - [@ericproulx](https://github.com/ericproulx).
4950
* Your contribution here.
5051

5152
### 2.0.0 (2023/11/11)

gemfiles/rack_3_1.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
eval_gemfile '../Gemfile'
4+
5+
gem 'rack', '~> 3.1'

lib/grape/dsl/inside_route.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def stream(value = nil)
350350
return if value.nil? && @stream.nil?
351351

352352
header Rack::CONTENT_LENGTH, nil
353-
header Rack::TRANSFER_ENCODING, nil
353+
header Grape::Http::Headers::TRANSFER_ENCODING, nil
354354
header Rack::CACHE_CONTROL, 'no-cache' # Skips ETag generation (reading the response up front)
355355
if value.is_a?(String)
356356
file_body = Grape::ServeStream::FileBody.new(value)

lib/grape/http/headers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Headers
1010
ALLOW = 'Allow'
1111
LOCATION = 'Location'
1212
X_CASCADE = 'X-Cascade'
13+
TRANSFER_ENCODING = 'Transfer-Encoding'
1314

1415
SUPPORTED_METHODS = [
1516
Rack::GET,

spec/grape/api_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ def to_txt
12521252
expect(last_response.content_type).to eq('text/plain')
12531253
expect(last_response.content_length).to be_nil
12541254
expect(last_response.headers[Rack::CACHE_CONTROL]).to eq('no-cache')
1255-
expect(last_response.headers[Rack::TRANSFER_ENCODING]).to eq('chunked')
1255+
expect(last_response.headers[Grape::Http::Headers::TRANSFER_ENCODING]).to eq('chunked')
12561256

12571257
expect(last_response.body).to eq("c\r\nThis is some\r\nd\r\n file content\r\n0\r\n\r\n")
12581258
end

spec/grape/dsl/inside_route_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def initialize
247247
before do
248248
subject.header Rack::CACHE_CONTROL, 'cache'
249249
subject.header Rack::CONTENT_LENGTH, 123
250-
subject.header Rack::TRANSFER_ENCODING, 'base64'
250+
subject.header Grape::Http::Headers::TRANSFER_ENCODING, 'base64'
251251
end
252252

253253
it 'sends no deprecation warnings' do
@@ -277,7 +277,7 @@ def initialize
277277
it 'does not change the Transfer-Encoding header' do
278278
subject.sendfile file_path
279279

280-
expect(subject.header[Rack::TRANSFER_ENCODING]).to eq 'base64'
280+
expect(subject.header[Grape::Http::Headers::TRANSFER_ENCODING]).to eq 'base64'
281281
end
282282
end
283283

@@ -308,7 +308,7 @@ def initialize
308308
before do
309309
subject.header Rack::CACHE_CONTROL, 'cache'
310310
subject.header Rack::CONTENT_LENGTH, 123
311-
subject.header Rack::TRANSFER_ENCODING, 'base64'
311+
subject.header Grape::Http::Headers::TRANSFER_ENCODING, 'base64'
312312
end
313313

314314
it 'emits no deprecation warnings' do
@@ -344,7 +344,7 @@ def initialize
344344
it 'sets Transfer-Encoding header to nil' do
345345
subject.stream file_path
346346

347-
expect(subject.header[Rack::TRANSFER_ENCODING]).to be_nil
347+
expect(subject.header[Grape::Http::Headers::TRANSFER_ENCODING]).to be_nil
348348
end
349349
end
350350

@@ -358,7 +358,7 @@ def initialize
358358
before do
359359
subject.header Rack::CACHE_CONTROL, 'cache'
360360
subject.header Rack::CONTENT_LENGTH, 123
361-
subject.header Rack::TRANSFER_ENCODING, 'base64'
361+
subject.header Grape::Http::Headers::TRANSFER_ENCODING, 'base64'
362362
end
363363

364364
it 'emits no deprecation warnings' do
@@ -388,7 +388,7 @@ def initialize
388388
it 'sets Transfer-Encoding header to nil' do
389389
subject.stream stream_object
390390

391-
expect(subject.header[Rack::TRANSFER_ENCODING]).to be_nil
391+
expect(subject.header[Grape::Http::Headers::TRANSFER_ENCODING]).to be_nil
392392
end
393393
end
394394

spec/grape/middleware/formatter_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,19 @@ def to_xml
453453
let(:env) do
454454
{ Rack::PATH_INFO => '/somewhere', Grape::Http::Headers::HTTP_ACCEPT => 'application/json' }
455455
end
456+
let(:headers) do
457+
if Gem::Version.new(Rack.release) < Gem::Version.new('3.1')
458+
{ Rack::CONTENT_TYPE => 'application/json', Rack::CONTENT_LENGTH => body.bytesize.to_s }
459+
else
460+
{ Rack::CONTENT_TYPE => 'application/json' }
461+
end
462+
end
456463

457464
it 'returns a file response' do
458465
expect(file).to receive(:each).and_yield(body)
459466
r = Rack::MockResponse[*subject.call(env)]
460467
expect(r).to be_successful
461-
expect(r.headers).to eq({ Rack::CONTENT_TYPE => 'application/json', Rack::CONTENT_LENGTH => body.bytesize.to_s })
468+
expect(r.headers).to eq(headers)
462469
expect(r.body).to eq('data')
463470
end
464471
end

spec/support/chunked_response.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def call(env)
5858

5959
if !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
6060
!headers[Rack::CONTENT_LENGTH] &&
61-
!headers[Rack::TRANSFER_ENCODING]
61+
!headers[Grape::Http::Headers::TRANSFER_ENCODING]
6262

63-
headers[Rack::TRANSFER_ENCODING] = 'chunked'
63+
headers[Grape::Http::Headers::TRANSFER_ENCODING] = 'chunked'
6464
response[2] = if headers['trailer']
6565
TrailerBody.new(body)
6666
else

0 commit comments

Comments
 (0)