Skip to content

Commit

Permalink
Merge pull request #91 from aetrion/bugfix/json-serializing
Browse files Browse the repository at this point in the history
Serialize request body to JSON
  • Loading branch information
jacegu committed Mar 11, 2016
2 parents 3632066 + 949036e commit 974c912
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
22 changes: 19 additions & 3 deletions lib/dnsimple/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ def execute(method, path, data = nil, options = {})
# @param [Hash] options The query and header params for the request
# @return [HTTParty::Response]
def request(method, path, data = nil, options = {})
options[:body] = data if data
request_options = Extra.deep_merge!(base_options, options)

HTTParty.send(method, base_url + path, Extra.deep_merge!(base_options, options))
if data
request_options[:headers]["Content-Type"] = content_type(request_options[:headers])
request_options[:body] = content_data(request_options[:headers], data)
end

HTTParty.send(method, base_url + path, request_options)
end


Expand All @@ -167,7 +172,10 @@ def request(method, path, data = nil, options = {})
def base_options
options = {
format: :json,
headers: { 'Accept' => 'application/json', 'User-Agent' => user_agent },
headers: {
'Accept' => 'application/json',
'User-Agent' => user_agent
},
}

if proxy
Expand All @@ -188,5 +196,13 @@ def base_options
options
end

def content_type(headers)
headers["Content-Type"] || "application/json"
end

def content_data(headers, data)
headers["Content-Type"] == "application/json" ? JSON.dump(data) : data
end

end
end
19 changes: 16 additions & 3 deletions spec/dnsimple/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,32 @@
subject.request(:get, 'foo')
end

it "properly extracts options from data" do
it "properly extracts processes options and encodes data" do
expect(HTTParty).to receive(:put).
with("#{subject.base_url}foo",
format: :json,
body: { something: "else" },
body: JSON.dump({ something: "else" }),
query: { foo: "bar" },
basic_auth: { username: "user", password: "pass" },
headers: { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{Dnsimple::VERSION}", "Custom" => "Header" }
headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{Dnsimple::VERSION}", "Custom" => "Header" }
).
and_return(double('response', code: 200))

subject.request(:put, 'foo', { something: "else" }, { query: { foo: "bar" }, headers: { "Custom" => "Header" } })
end

it "handles non application/json content types" do
expect(HTTParty).to receive(:post).
with("#{subject.base_url}foo",
format: :json,
body: { something: "else" },
basic_auth: { username: "user", password: "pass" },
headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => "dnsimple-ruby/#{Dnsimple::VERSION}" }
).
and_return(double('response', code: 200))

subject.request(:post, 'foo', { something: "else" }, headers: { "Content-Type" => "application/x-www-form-urlencoded" })
end
end

end

0 comments on commit 974c912

Please sign in to comment.