From 93a4b933af907a2a1b76a111cb1d722a66c482ac Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Sun, 26 Apr 2020 23:52:06 +0100 Subject: [PATCH] added support for :expect_threshold_size, to disable expect: 100-continue based on body bytesize sent --- lib/httpx/plugins/expect.rb | 15 ++++++++++++++- test/support/requests/plugins/expect.rb | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/httpx/plugins/expect.rb b/lib/httpx/plugins/expect.rb index 7da3104b..6867c73b 100644 --- a/lib/httpx/plugins/expect.rb +++ b/lib/httpx/plugins/expect.rb @@ -18,14 +18,27 @@ module HTTPX seconds end + + def_option(:expect_threshold_size) do |bytes| + bytes = Integer(bytes) + raise Error, ":expect_threshold_size must be positive" unless bytes.positive? + + bytes + end end.new(options).merge(expect_timeout: EXPECT_TIMEOUT) end module RequestBodyMethods - def initialize(*) + def initialize(*, options) super return if @body.nil? + if (threshold = options.expect_threshold_size) + unless unbounded_body? + return if @body.bytesize < threshold + end + end + @headers["expect"] = "100-continue" end end diff --git a/test/support/requests/plugins/expect.rb b/test/support/requests/plugins/expect.rb index b9493e08..fe3238b8 100644 --- a/test/support/requests/plugins/expect.rb +++ b/test/support/requests/plugins/expect.rb @@ -13,6 +13,20 @@ module Requests verify_uploaded(body, "form", "foo" => "bar") end + def test_plugin_expect_100_form_params_under_threshold + uri = build_uri("/post") + session = HTTPX.plugin(:expect, expect_threshold_size: 4) + response = session.post(uri, body: "a" * 3) + verify_status(response, 200) + body = json_body(response) + verify_no_header(body["headers"], "Expect") + + response = session.post(uri, body: "a" * 5) + verify_status(response, 200) + body = json_body(response) + verify_header(body["headers"], "Expect", "100-continue") + end + # def test_plugin_expect_100_send_body_after_delay # uri = build_uri("/delay/3") # response = HTTPX.plugin(:expect).post(uri, form: { "foo" => "bar" })