Skip to content

Commit

Permalink
send {} instead on null for GET and other method
Browse files Browse the repository at this point in the history
  • Loading branch information
bartes committed Sep 2, 2021
1 parent d19a14c commit 9dfcd11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/castle/core/send_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def build(command, headers, config)
url = "#{config.base_url.path}/#{command.path}"
request_obj = Net::HTTP.const_get(command.method.to_s.capitalize).new(url, headers)

unless command.method == :get
request_obj.body = ::Castle::Utils::CleanInvalidChars.call(command.data).to_json
end
request_obj.body = ::Castle::Utils::CleanInvalidChars.call(command.data || {}).to_json

Castle::Logger.call("#{url}:", request_obj.body, config)

Expand Down
24 changes: 24 additions & 0 deletions spec/lib/castle/core/send_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,29 @@
it { expect(build.to_hash).to have_key('sample-header') }
it { expect(build.to_hash['sample-header']).to eql(['1']) }
end

context 'when get' do
let(:time) { Time.now.utc.iso8601(3) }
let(:command) { Castle::Commands::GetDevice.build(device_token: '1') }
let(:expected_body) { {} }

before { allow(Castle::Utils::GetTimestamp).to receive(:call).and_return(time) }

it { expect(build.body).to be_eql(expected_body.to_json) }
it { expect(build.method).to eql('GET') }
it { expect(build.path).to eql("/v1/#{command.path}") }
end

context 'when put' do
let(:time) { Time.now.utc.iso8601(3) }
let(:command) { Castle::Commands::ApproveDevice.build(device_token: '1') }
let(:expected_body) { {} }

before { allow(Castle::Utils::GetTimestamp).to receive(:call).and_return(time) }

it { expect(build.body).to be_eql(expected_body.to_json) }
it { expect(build.method).to eql('PUT') }
it { expect(build.path).to eql("/v1/#{command.path}") }
end
end
end

0 comments on commit 9dfcd11

Please sign in to comment.