From a03a8541933b24158d947f111f031695896a7124 Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Sun, 7 Jan 2024 13:09:42 -0500 Subject: [PATCH] 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 --- actionpack/test/controller/chunked_test.rb | 32 ++++++++++++ railties/test/railties/http_request_test.rb | 54 --------------------- 2 files changed, 32 insertions(+), 54 deletions(-) create mode 100644 actionpack/test/controller/chunked_test.rb delete mode 100644 railties/test/railties/http_request_test.rb diff --git a/actionpack/test/controller/chunked_test.rb b/actionpack/test/controller/chunked_test.rb new file mode 100644 index 0000000000..3402daa515 --- /dev/null +++ b/actionpack/test/controller/chunked_test.rb @@ -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 diff --git a/railties/test/railties/http_request_test.rb b/railties/test/railties/http_request_test.rb deleted file mode 100644 index 2c762fa3fb..0000000000 --- a/railties/test/railties/http_request_test.rb +++ /dev/null @@ -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