From 791154b6502e714c05e6c6fcededa1a800f8c123 Mon Sep 17 00:00:00 2001 From: guochunzhong Date: Tue, 23 Aug 2016 17:37:13 +0800 Subject: [PATCH] To cache the HTTP object, so make Timeout global effective. #74 --- lib/wechat/http_client.rb | 10 +++++----- spec/lib/wechat/http_client_spec.rb | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/wechat/http_client.rb b/lib/wechat/http_client.rb index c7f7be56..a6bb934d 100644 --- a/lib/wechat/http_client.rb +++ b/lib/wechat/http_client.rb @@ -2,11 +2,11 @@ module Wechat class HttpClient - attr_reader :base, :ssl_context + attr_reader :base, :ssl_context, :httprb def initialize(base, timeout, skip_verify_ssl) @base = base - HTTP.timeout(:global, write: timeout, connect: timeout, read: timeout) + @httprb = HTTP.timeout(:global, write: timeout, connect: timeout, read: timeout) @ssl_context = OpenSSL::SSL::SSLContext.new @ssl_context.ssl_version = :TLSv1_client @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE if skip_verify_ssl @@ -15,21 +15,21 @@ def initialize(base, timeout, skip_verify_ssl) def get(path, get_header = {}) request(path, get_header) do |url, header| params = header.delete(:params) - HTTP.headers(header).get(url, params: params, ssl_context: ssl_context) + httprb.headers(header).get(url, params: params, ssl_context: ssl_context) end end def post(path, payload, post_header = {}) request(path, post_header) do |url, header| params = header.delete(:params) - HTTP.headers(header).post(url, params: params, body: payload, ssl_context: ssl_context) + httprb.headers(header).post(url, params: params, body: payload, ssl_context: ssl_context) end end def post_file(path, file, post_header = {}) request(path, post_header) do |url, header| params = header.delete(:params) - HTTP.headers(header) + httprb.headers(header) .post(url, params: params, form: { media: HTTP::FormData::File.new(file), hack: 'X' }, # Existing here for http-form_data 1.0.1 handle single param improperly diff --git a/spec/lib/wechat/http_client_spec.rb b/spec/lib/wechat/http_client_spec.rb index 3dd94381..034bb61b 100644 --- a/spec/lib/wechat/http_client_spec.rb +++ b/spec/lib/wechat/http_client_spec.rb @@ -21,14 +21,14 @@ describe '#get' do specify 'Will use http get method to request data' do - allow(HTTP).to receive_message_chain('headers.get') { response_json } + allow(subject.httprb).to receive_message_chain('headers.get') { response_json } subject.get('token') end end describe '#post' do specify 'Will use http post method to request data' do - allow(HTTP).to receive_message_chain('headers.post') { response_json } + allow(subject.httprb).to receive_message_chain('headers.post') { response_json } subject.post('token', 'some_data') end end