Skip to content

Commit

Permalink
added opts to allow overriding of double_encode_content
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Jan 6, 2019
1 parent 767ca9f commit 67980e3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
13 changes: 7 additions & 6 deletions lib/hub_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}")

Expand All @@ -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,
Expand Down
51 changes: 45 additions & 6 deletions spec/hub_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,28 +249,41 @@ 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?
stub_request(:post, HubClient.build_hub_url(HubClient.configuration.endpoint_url)).
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"
config.endpoint_url = "http://service-hub.com"
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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 67980e3

Please sign in to comment.