Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47336 from pedro108/fix-raw-post-for-chunked-request
Fix request.raw_post empty bug when the Transfer-Encoding header is present
- Loading branch information
Showing
2 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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 | ||
|
||
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") # prevents Rack::MockRequest from adding a Content-Length | ||
|
||
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 |