diff --git a/lib/faraday/response_body_size_limit/middleware.rb b/lib/faraday/response_body_size_limit/middleware.rb index b4599aa..dee28db 100644 --- a/lib/faraday/response_body_size_limit/middleware.rb +++ b/lib/faraday/response_body_size_limit/middleware.rb @@ -33,6 +33,6 @@ def call(env) # rubocop:disable Metrics/MethodLength end end -Faraday::Request.register_middleware( +Faraday::Response.register_middleware( response_body_size_limit: Faraday::ResponseBodySizeLimit::Middleware ) diff --git a/lib/twingly/http.rb b/lib/twingly/http.rb index 1bc6dc0..ec1ec29 100644 --- a/lib/twingly/http.rb +++ b/lib/twingly/http.rb @@ -200,8 +200,6 @@ def create_http_client # rubocop:disable Metrics/MethodLength exceptions: @retryable_exceptions, methods: [], # empty [] forces Faraday to run retry_if retry_if: retry_if - faraday.request :response_body_size_limit, - max_size_bytes: @max_response_body_size_bytes faraday.response :logfmt_logger, @logger.dup, headers: true, bodies: true, @@ -210,6 +208,8 @@ def create_http_client # rubocop:disable Metrics/MethodLength faraday.use FaradayMiddleware::FollowRedirects, limit: @follow_redirects_limit end + faraday.response :response_body_size_limit, + max_size_bytes: @max_response_body_size_bytes faraday.adapter Faraday.default_adapter faraday.headers[:user_agent] = user_agent end diff --git a/spec/lib/twingly/http_spec.rb b/spec/lib/twingly/http_spec.rb index 7031288..9937192 100644 --- a/spec/lib/twingly/http_spec.rb +++ b/spec/lib/twingly/http_spec.rb @@ -133,10 +133,11 @@ class CustomError < StandardError; end stub_request(:any, redir_url) .to_return(status: 302, - headers: { "Location" => "#{base_redir_url}#{n + 1}" }) + headers: { "Location" => "#{base_redir_url}#{n + 1}" }, + body: "redirect...") end - stub_request(:any, "#{base_redir_url}#{times}").to_return(status: 200) + stub_request(:any, "#{base_redir_url}#{times}").to_return(status: 200, body: "finished!") end end @@ -150,9 +151,13 @@ class CustomError < StandardError; end it do is_expected.to match(headers: {}, status: 200, - body: "", + body: "finished!", final_url: "http://redirect.1") end + + it "only returns the body of the final request" do + expect(response.fetch(:body)).to eq("finished!") + end end context "when not following redirects" do @@ -165,7 +170,7 @@ class CustomError < StandardError; end it do is_expected.to match(headers: { "location" => "http://redirect.1" }, status: 302, - body: "", + body: "redirect...", final_url: url) end end @@ -182,7 +187,7 @@ class CustomError < StandardError; end it do is_expected.to match(headers: {}, status: 200, - body: "", + body: "finished!", final_url: "http://redirect.5") end end