Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for PUT requests #170

Merged
merged 1 commit into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions lib/hubspot/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def get_json(path, opts)
url = generate_url(path, opts)
response = get(url, format: :json)
log_request_and_response url, response
raise(Hubspot::RequestError.new(response)) unless response.success?
response.parsed_response
handle_response(response)
end

def post_json(path, opts)
Expand All @@ -22,12 +21,18 @@ def post_json(path, opts)
no_parse ? response : response.parsed_response
end

def put_json(path, opts)
url = generate_url(path, opts[:params])
response = put(url, body: opts[:body].to_json, headers: { 'Content-Type' => 'application/json' }, format: :json)
log_request_and_response url, response, opts[:body]
raise(Hubspot::RequestError.new(response)) unless response.success?
response.parsed_response
def put_json(path, options)
url = generate_url(path, options[:params])

response = put(
url,
body: options[:body].to_json,
headers: { "Content-Type" => "application/json" },
format: :json
)

log_request_and_response(url, response, options[:body])
handle_response(response)
end

def delete_json(path, opts)
Expand All @@ -40,8 +45,20 @@ def delete_json(path, opts)

protected

def handle_response(response)
if response.success?
response.parsed_response
else
raise(Hubspot::RequestError.new(response))
end
end

def log_request_and_response(uri, response, body=nil)
Hubspot::Config.logger.info "Hubspot: #{uri}.\nBody: #{body}.\nResponse: #{response.code} #{response.body}"
Hubspot::Config.logger.info(<<~MSG)
Hubspot: #{uri}.
Body: #{body}.
Response: #{response.code} #{response.body}
MSG
end

def generate_url(path, params={}, options={})
Expand Down
59 changes: 59 additions & 0 deletions spec/lib/hubspot/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,59 @@
end
end

describe ".put_json" do
it "issues a PUT request and returns the parsed body" do
path = "/some/path"
update_options = { params: {}, body: {} }

stub_request(:put, "https://api.hubapi.com/some/path?hapikey=fake").
to_return(status: 200, body: JSON.generate(vid: 123))

response = Hubspot::Connection.put_json(path, update_options)

assert_requested(
:put,
"https://api.hubapi.com/some/path?hapikey=fake",
{
body: "{}",
headers: { "Content-Type" => "application/json" },
}
)

expect(response).to eq({ "vid" => 123 })
end

it "logs information about the request and response" do
path = "/some/path"
update_options = { params: {}, body: {} }

logger = stub_logger

stub_request(:put, "https://api.hubapi.com/some/path?hapikey=fake").
to_return(status: 200, body: JSON.generate("response body"))

Hubspot::Connection.put_json(path, update_options)

expect(logger).to have_received(:info).with(<<~MSG)
Hubspot: https://api.hubapi.com/some/path?hapikey=fake.
Body: {}.
Response: 200 "response body"
MSG
end

it "raises when the request fails" do
path = "/some/path"
update_options = { params: {}, body: {} }

stub_request(:put, "https://api.hubapi.com/some/path?hapikey=fake").
to_return(status: 401)

expect {
Hubspot::Connection.put_json(path, update_options)
}.to raise_error(Hubspot::RequestError)
end
end

context 'private methods' do
describe ".generate_url" do
let(:path){ "/test/:email/profile" }
Expand Down Expand Up @@ -123,4 +176,10 @@
end
end
end

def stub_logger
instance_double(Logger, info: true).tap do |logger|
allow(Hubspot::Config).to receive(:logger).and_return(logger)
end
end
end