diff --git a/Library/Homebrew/livecheck/strategy.rb b/Library/Homebrew/livecheck/strategy.rb index 56d0ecb168..92b75552fb 100644 --- a/Library/Homebrew/livecheck/strategy.rb +++ b/Library/Homebrew/livecheck/strategy.rb @@ -178,7 +178,7 @@ module Homebrew ).returns(T::Array[String]) } def self.post_args(post_form: nil, post_json: nil) - if post_form.present? + args = if post_form.present? require "uri" ["--data", URI.encode_www_form(post_form)] elsif post_json.present? @@ -187,6 +187,12 @@ module Homebrew else [] end + + if (content_length = args[1]&.length) + args << "--header" << "Content-Length: #{content_length}" + end + + args end # Collects HTTP response headers, starting with the provided URL. diff --git a/Library/Homebrew/test/livecheck/strategy_spec.rb b/Library/Homebrew/test/livecheck/strategy_spec.rb index 4ef416c331..396fb5ccb0 100644 --- a/Library/Homebrew/test/livecheck/strategy_spec.rb +++ b/Library/Homebrew/test/livecheck/strategy_spec.rb @@ -144,16 +144,22 @@ RSpec.describe Homebrew::Livecheck::Strategy do end describe "::post_args" do + let(:form_string_content_length) { "Content-Length: #{form_string.length}" } + let(:json_string_content_length) { "Content-Length: #{json_string.length}" } + it "returns an array including `--data` and an encoded form data string" do - expect(strategy.post_args(post_form: post_hash)).to eq(["--data", form_string]) + expect(strategy.post_args(post_form: post_hash)) + .to eq(["--data", form_string, "--header", form_string_content_length]) # If both `post_form` and `post_json` are present, only `post_form` will # be used. - expect(strategy.post_args(post_form: post_hash, post_json: post_hash)).to eq(["--data", form_string]) + expect(strategy.post_args(post_form: post_hash, post_json: post_hash)) + .to eq(["--data", form_string, "--header", form_string_content_length]) end it "returns an array including `--json` and a JSON string" do - expect(strategy.post_args(post_json: post_hash)).to eq(["--json", json_string]) + expect(strategy.post_args(post_json: post_hash)) + .to eq(["--json", json_string, "--header", json_string_content_length]) end it "returns an empty array if `post_form` value is blank" do