Move Transfer-Encoding chunked test to Action Pack

While working on [another PR][1], I found that removing the
Transfer-Encoding conditionals did not result in any failing tests in
Action Pack. This was surprising to me until I found that there was a
test for this behavior in Railties. However, nothing about the test
really depends on having a full Rails application or the Railties test
suite.

This commit moves the test into Action Pack to simplify/speedup the test
(no need to build a full app) as well as keeping the test closer to the
actual behavior being tested.

[1]: 0c334b48fdc5d70b0c8406ba184fbfd26750b049
This commit is contained in:
Hartley McGuire 2024-01-07 13:09:42 -05:00
parent 7f341134ca
commit a03a854193
No known key found for this signature in database
GPG Key ID: E823FC1403858A82
2 changed files with 32 additions and 54 deletions

@ -0,0 +1,32 @@
# frozen_string_literal: true
require "stringio"
require "abstract_unit"
class ChunkedTest < ActionDispatch::IntegrationTest
class ChunkedController < ApplicationController
def chunk
render json: {
raw_post: request.raw_post,
content_length: request.content_length
}
end
end
# The TestInput class prevents Rack::MockRequest from adding a Content-Length when the method `size` is defined
class TestInput < StringIO
undef_method :size
end
test "parses request raw_post correctly when request has Transfer-Encoding header without a Content-Length value" do
@app = self.class.build_app
@app.routes.draw do
post "chunked", to: ChunkedController.action(:chunk)
end
post "/chunked", params: TestInput.new("foo=bar"), headers: { "Transfer-Encoding" => "gzip, chunked;foo=bar" }
assert_equal 7, response.parsed_body["content_length"]
assert_equal "foo=bar", response.parsed_body["raw_post"]
end
end

@ -1,54 +0,0 @@
# frozen_string_literal: true
require "isolation/abstract_unit"
require "stringio"
require "rack/test"
require "active_support/core_ext/module/delegation"
module RailtiesTest
class HttpRequestTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
include Rack::Test::Methods
def setup
build_app
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
post "posts", to: "posts#create"
end
RUBY
controller "posts", <<-RUBY
class PostsController < ApplicationController
def create
render json: {
raw_post: request.raw_post,
content_length: request.content_length
}
end
end
RUBY
end
def teardown
teardown_app
end
# The TestInput class prevents Rack::MockRequest from adding a Content-Length when the method `size` is defined
class TestInput < StringIO
undef_method :size
end
test "parses request raw_post correctly when request has Transfer-Encoding header without a Content-Length value" do
require "#{app_path}/config/environment"
header "Transfer-Encoding", "gzip, chunked;foo=bar"
post "/posts", TestInput.new("foo=bar")
json_response = JSON.parse(last_response.body)
assert_equal 7, json_response["content_length"]
assert_equal "foo=bar", json_response["raw_post"]
end
end
end