diff --git a/lib/hub_client.rb b/lib/hub_client.rb index 3b1fc2d..ba89d1c 100644 --- a/lib/hub_client.rb +++ b/lib/hub_client.rb @@ -10,7 +10,7 @@ module HubClient accept: :json, } - def self.publish(metadata, content, env = nil) + def self.publish(metadata, content, env = nil, opts = {}) config = HubClient.configuration raise ConfigArgumentMissing, "endpoint_url missing" unless config.endpoint_url @@ -22,7 +22,7 @@ def self.publish(metadata, content, env = nil) retries = 0 begin - RestClient::Request.execute(request_opts(config, payload)) + RestClient::Request.execute(request_opts(config, payload, opts)) rescue RestClient::Exception => e HubClient.logger.warn("HubClient Exception #{e.class}: #{e.message} Code: #{e.http_code} Response: #{e.response} Request: #{payload}") @@ -36,16 +36,17 @@ def self.publish(metadata, content, env = nil) private - def self.encode_content_if_specified(config, payload) - (payload[:content] = payload[:content].to_json) if config.double_encode_content && payload[:content] + def self.encode_content_if_specified(config, payload, opts) + double_encode_content = opts.fetch(:double_encode_content, config.double_encode_content) + (payload[:content] = payload[:content].to_json) if double_encode_content && payload[:content] payload end - def self.request_opts(config, payload) + def self.request_opts(config, payload, opts) { method: :post, url: build_hub_url(config.endpoint_url), - payload: encode_content_if_specified(config, payload).to_json, + payload: encode_content_if_specified(config, payload, opts).to_json, headers: REQUEST_HEADERS, timeout: config.timeout, open_timeout: config.open_timeout, diff --git a/spec/hub_client_spec.rb b/spec/hub_client_spec.rb index 1112704..99a7e29 100644 --- a/spec/hub_client_spec.rb +++ b/spec/hub_client_spec.rb @@ -249,7 +249,7 @@ def stub_publish_request end describe 'double_encode_payload option' do - shared_examples_for 'builds request correctly' do |description, content, expected_content| + shared_examples_for 'builds request correctly' do |description, opts, content, expected_content| it description do expected_body = { env: "il-qa2", type: "order_created" } (expected_body[:content] = expected_content) unless expected_content.nil? @@ -257,12 +257,12 @@ def stub_publish_request with(body: expected_body). to_return(status: 204) - HubClient.publish(:order_created, content) + HubClient.publish(:order_created, content, nil, opts) assert_requested :post, HubClient.build_hub_url(HubClient.configuration.endpoint_url) end end - context 'when not specified' do + context 'when not specified in config' do before do HubClient.configure do |config| config.env = "il-qa2" @@ -270,7 +270,20 @@ def stub_publish_request end end - include_examples 'builds request correctly', 'does not double encode', {some: 'content'}, {some: 'content'} + context 'when not specified in opts' do + include_examples 'builds request correctly', + 'does not double encode', {}, {some: 'content'}, {some: 'content'} + end + + context 'when specified in opts as true' do + include_examples 'builds request correctly', + 'double encodes', {double_encode_content: true}, {some: 'content'}, {some: 'content'}.to_json + end + + context 'when specified in opts as false' do + include_examples 'builds request correctly', + 'does not double encode', {double_encode_content: false}, {some: 'content'}, {some: 'content'} + end end context 'when specified as false' do @@ -282,7 +295,20 @@ def stub_publish_request end end - include_examples 'builds request correctly', 'does not double encode', {some: 'content'}, {some: 'content'} + context 'when not specified in opts' do + include_examples 'builds request correctly', + 'does not double encode', {double_encode_content: false}, {some: 'content'}, {some: 'content'} + end + + context 'when specified in opts as true' do + include_examples 'builds request correctly', + 'double encodes', {double_encode_content: true}, {some: 'content'}, {some: 'content'}.to_json + end + + context 'when specified in opts as false' do + include_examples 'builds request correctly', + 'does not double encode', {double_encode_content: false}, {some: 'content'}, {some: 'content'} + end end context 'when specified as true' do @@ -294,7 +320,20 @@ def stub_publish_request end end - include_examples 'builds request correctly', 'double encodes', {some: 'content'}, {some: 'content'}.to_json + context 'when not specified in opts' do + include_examples 'builds request correctly', + 'double encodes', {double_encode_content: true}, {some: 'content'}, {some: 'content'}.to_json + end + + context 'when specified in opts as true' do + include_examples 'builds request correctly', + 'double encodes', {double_encode_content: true}, {some: 'content'}, {some: 'content'}.to_json + end + + context 'when specified in opts as false' do + include_examples 'builds request correctly', + 'does not double encode', {double_encode_content: false}, {some: 'content'}, {some: 'content'} + end end end end