diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-11-08 17:58:49 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-11-08 17:58:49 +0900 |
commit | 5628c38cc54aeef01f7ff7e6c4600d3baea87b87 (patch) | |
tree | 9df8501f8447af7f798f1e8e2d2f0f77dbcd7262 | |
parent | 88db491bc536d11c8dae5f20fce94f83b508063d (diff) | |
download | plum-5628c38cc54aeef01f7ff7e6c4600d3baea87b87.tar.gz |
client: make #request accept more arguments
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | lib/plum/client.rb | 43 | ||||
-rw-r--r-- | lib/plum/client/client_session.rb | 2 | ||||
-rw-r--r-- | lib/plum/client/legacy_client_session.rb | 2 | ||||
-rw-r--r-- | test/plum/client/test_client.rb | 6 | ||||
-rw-r--r-- | test/plum/client/test_legacy_client_session.rb | 8 |
6 files changed, 38 insertions, 27 deletions
@@ -16,8 +16,8 @@ If the server does't support HTTP/2, `Plum::Client` tries to use HTTP/1.x instea ##### Sequential request ```ruby -client = Plum::Client.start("http2.rhe.jp", 443) -res1 = client.get("/") +client = Plum::Client.start("http2.rhe.jp", 443, user_agent: "nyaan") +res1 = client.get("/", headers: { "accept" => "*/*" }) puts res1.body # => "..." res2 = client.post("/post", "data") puts res2.body # => "..." diff --git a/lib/plum/client.rb b/lib/plum/client.rb index 28f0cdd..64452f3 100644 --- a/lib/plum/client.rb +++ b/lib/plum/client.rb @@ -8,6 +8,7 @@ module Plum ssl_context: nil, hostname: nil, http2_settings: {}, + user_agent: "plum/#{Plum::VERSION}", }.freeze attr_reader :host, :port, :config @@ -80,61 +81,63 @@ module Plum # Creates a new HTTP request. # @param headers [Hash<String, String>] the request headers # @param body [String] the request body + # @param options [Hash<Symbol, Object>] request options # @param block [Proc] if passed, it will be called when received response headers. - def request_async(headers, body = nil, &block) + def request_async(headers, body, options = {}, &block) raise ArgumentError, ":method and :path headers are required" unless headers[":method"] && headers[":path"] - @session.request(headers, body, &block) + @session.request(headers, body, options, &block) end # Creates a new HTTP request and waits for the response # @param headers [Hash<String, String>] the request headers # @param body [String] the request body - def request(headers, body = nil, &block) - wait request_async(headers, body, &block) + # @param options [Hash<Symbol, Object>] the request options + def request(headers, body, options = {}, &block) + wait request_async(headers, body, options, &block) end # @!method get # @!method head # @!method delete # @param path [String] the absolute path to request (translated into :path header) - # @param headers [Hash] the request headers + # @param options [Hash<Symbol, Object>] the request options # Shorthand method for `#request` # @!method get_async # @!method head_async # @!method delete_async # @param path [String] the absolute path to request (translated into :path header) - # @param headers [Hash] the request headers + # @param options [Hash<Symbol, Object>] the request options # @param block [Proc] if specified, calls the block when finished # Shorthand method for `#request_async` %w(GET HEAD DELETE).each { |method| - define_method(:"#{method.downcase}") do |path, headers = {}, &block| - request({ ":method" => method, ":path" => path }.merge(headers), &block) + define_method(:"#{method.downcase}") do |path, options = {}, &block| + wait _request_helper(method, path, nil, options, &block) end - define_method(:"#{method.downcase}_async") do |path, headers = {}, &block| - request_async({ ":method" => method, ":path" => path }.merge(headers), nil, &block) + define_method(:"#{method.downcase}_async") do |path, options = {}, &block| + _request_helper(method, path, nil, options, &block) end } # @!method post # @!method put # @param path [String] the absolute path to request (translated into :path header) # @param body [String] the request body - # @param headers [Hash] the request headers + # @param options [Hash<Symbol, Object>] the request options # Shorthand method for `#request` # @!method post_async # @!method put_async # @param path [String] the absolute path to request (translated into :path header) # @param body [String] the request body - # @param headers [Hash] the request headers + # @param options [Hash<Symbol, Object>] the request options # @param block [Proc] if specified, calls the block when finished # Shorthand method for `#request_async` %w(POST PUT).each { |method| - define_method(:"#{method.downcase}") do |path, body = nil, headers = {}, &block| - request({ ":method" => method, ":path" => path }.merge(headers), body, &block) + define_method(:"#{method.downcase}") do |path, body, options = {}, &block| + wait _request_helper(method, path, body, options, &block) end - define_method(:"#{method.downcase}_async") do |path, body = nil, headers = {}, &block| - request_async({ ":method" => method, ":path" => path }.merge(headers), body, &block) + define_method(:"#{method.downcase}_async") do |path, body, options = {}, &block| + _request_helper(method, path, body, options, &block) end } @@ -188,5 +191,13 @@ module Plum end ctx end + + def _request_helper(method, path, body, options, &block) + base = { ":method" => method, + ":path" => path, + "user-agent" => @config[:user_agent] } + base.merge!(options[:headers]) if options[:headers] + request_async(base, body, options, &block) + end end end diff --git a/lib/plum/client/client_session.rb b/lib/plum/client/client_session.rb index c637aa5..2964769 100644 --- a/lib/plum/client/client_session.rb +++ b/lib/plum/client/client_session.rb @@ -33,7 +33,7 @@ module Plum @plum.close end - def request(headers, body = nil, &headers_cb) + def request(headers, body, options, &headers_cb) headers = { ":method" => nil, ":path" => nil, ":authority" => @config[:hostname], diff --git a/lib/plum/client/legacy_client_session.rb b/lib/plum/client/legacy_client_session.rb index bc0c2bb..2e52be2 100644 --- a/lib/plum/client/legacy_client_session.rb +++ b/lib/plum/client/legacy_client_session.rb @@ -29,7 +29,7 @@ module Plum @response._fail if @response end - def request(headers, body = nil, &headers_cb) + def request(headers, body, options, &headers_cb) response = Response.new @requests << [response, headers, body, headers_cb] consume_queue diff --git a/test/plum/client/test_client.rb b/test/plum/client/test_client.rb index ef93e40..1cc0930 100644 --- a/test/plum/client/test_client.rb +++ b/test/plum/client/test_client.rb @@ -7,7 +7,7 @@ class ClientTest < Minitest::Test client = Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) res1 = client.request({ ":path" => "/", ":method" => "POST", ":scheme" => "https", "header" => "ccc" }, "abc") assert_equal("POSTcccabc", res1.body) - res2 = client.put("/", "aaa", { "header" => "ccc" }) + res2 = client.put("/", "aaa", headers: { "header" => "ccc" }) assert_equal("PUTcccaaa", res2.body) client.close ensure @@ -20,12 +20,12 @@ class ClientTest < Minitest::Test server_thread = start_tls_server Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |c| client = c - res1 = client.request_async({ ":path" => "/", ":method" => "GET", ":scheme" => "https", "header" => "ccc" }) { |res1| + res1 = client.request_async({ ":path" => "/", ":method" => "GET", ":scheme" => "https", "header" => "ccc" }, nil) { |res1| assert(res1.headers) } assert_nil(res1.headers) - res2 = client.get_async("/", "header" => "ccc") + res2 = client.get_async("/", headers: { "header" => "ccc" }) } assert(res2.headers) assert_equal("GETccc", res2.body) diff --git a/test/plum/client/test_legacy_client_session.rb b/test/plum/client/test_legacy_client_session.rb index 16111ff..2c8f146 100644 --- a/test/plum/client/test_legacy_client_session.rb +++ b/test/plum/client/test_legacy_client_session.rb @@ -6,7 +6,7 @@ class LegacyClientSessionTest < Minitest::Test io = StringIO.new session = LegacyClientSession.new(io, Client::DEFAULT_CONFIG) assert(session.empty?) - res = session.request({}, "aa") + res = session.request({}, "aa", {}) assert(!session.empty?) io.string << "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" session.succ @@ -16,7 +16,7 @@ class LegacyClientSessionTest < Minitest::Test def test_close_fails_req session = LegacyClientSession.new(StringIO.new, Client::DEFAULT_CONFIG) - res = session.request({}) + res = session.request({}, nil, {}) assert(!res.failed?) session.close assert(res.failed?) @@ -25,7 +25,7 @@ class LegacyClientSessionTest < Minitest::Test def test_fail io = StringIO.new session = LegacyClientSession.new(io, Client::DEFAULT_CONFIG) - res = session.request({}, "aa") + res = session.request({}, "aa", {}) assert_raises { session.succ } @@ -36,7 +36,7 @@ class LegacyClientSessionTest < Minitest::Test def test_request io = StringIO.new session = LegacyClientSession.new(io, Client::DEFAULT_CONFIG.merge(hostname: "aa")) - res = session.request({ ":method" => "GET", ":path" => "/aa" }, "aa") + res = session.request({ ":method" => "GET", ":path" => "/aa" }, "aa", {}) assert("GET /aa HTTP/1.1\r\nhost: aa\r\n\r\naa") io.string << "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\naaa" session.succ until res.finished? |