diff --git a/.gitignore b/.gitignore index a26a071..77d34cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +bin *.gem *.rbc /.config @@ -28,7 +29,7 @@ build/ # for a library or gem, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: -# Gemfile.lock +Gemfile.lock # .ruby-version # .ruby-gemset diff --git a/README.md b/README.md index 337fef5..b445624 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ gem install ruby_http_client ```ruby require 'ruby_http_client' global_headers = {'Authorization' => 'Basic XXXXXXX' } -client = SendGrid::Client(host: 'base_url', request_headers: global_headers) +client = SendGrid::Client.new(host: 'base_url', request_headers: global_headers) client.your.api._(param).call.get puts response.status_code puts response.body @@ -39,7 +39,7 @@ puts response.headers ```ruby require 'ruby_http_client' global_headers = {'Authorization' => 'Basic XXXXXXX' } -client = SendGrid::Client(host: 'base_url', request_headers: global_headers) +client = SendGrid::Client.new(host: 'base_url', request_headers: global_headers) query_params = { 'hello' => 0, 'world' => 1 } request_headers = { 'X-Test' => 'test' } data = { 'some' => 1, 'awesome' => 2, 'data' => 3} diff --git a/lib/ruby_http_client.rb b/lib/ruby_http_client.rb index 18b97ba..a3f1cfd 100644 --- a/lib/ruby_http_client.rb +++ b/lib/ruby_http_client.rb @@ -19,7 +19,7 @@ def initialize(response) # A simple REST client. class Client - attr_reader :host, :request_headers, :url_path + attr_reader :host, :request_headers, :url_path, :request, :http # * *Args* : # - +host+ -> Base URL for the api. (e.g. https://api.sendgrid.com) # - +request_headers+ -> A hash of the headers you want applied on @@ -132,18 +132,21 @@ def build_url(query_params: nil) def build_request(name, args) build_args(args) if args uri = build_url(query_params: @query_params) - http = Net::HTTP.new(uri.host, uri.port) - http = add_ssl(http) + @http = add_ssl(Net::HTTP.new(uri.host, uri.port)) net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize) - request = net_http.new(uri.request_uri) - request = build_request_headers(request) - request.body = @request_body.to_json if @request_body - if request.body - request['Content-Type'] = 'application/json' - elsif !request.body and (name.to_s == "post") - request['Content-Type'] = '' + @request = build_request_headers(net_http.new(uri.request_uri)) + if (@request_body && + (!@request_headers.has_key?('Content-Type') || + @request_headers['Content-Type'] == 'application/json') + ) + @request.body = @request_body.to_json + @request['Content-Type'] = 'application/json' + elsif !@request_body and (name.to_s == "post") + @request['Content-Type'] = '' + else + @request.body = @request_body end - make_request(http, request) + make_request(@http, @request) end # Make the API call and return the response. This is separated into diff --git a/test/test_ruby_http_client.rb b/test/test_ruby_http_client.rb index 58c7235..ebf6474 100644 --- a/test/test_ruby_http_client.rb +++ b/test/test_ruby_http_client.rb @@ -1,4 +1,4 @@ -require_relative '../lib/ruby_http_client' +require 'ruby_http_client' require 'minitest/autorun' class MockResponse @@ -50,20 +50,20 @@ def test_update_headers def test_build_request_headers request = {} request = @client.build_request_headers(request) - assert_equal(@client.request_headers, request) + assert_equal(request, @client.request_headers) end def test_add_version url = '' @client.add_version(url) - assert_equal(url, "/#{@version}") + assert_equal("/#{@version}", url) end def test_build_query_params url = '' query_params = { 'limit' => 100, 'offset' => 0 } url = @client.build_query_params(url, query_params) - assert_equal(url, '?limit=100&offset=0') + assert_equal('?limit=100&offset=0', url) end def test_build_url @@ -71,28 +71,85 @@ def test_build_url params = { 'limit' => 100, 'offset' => 0 } url = URI.parse(@host + '/' + @version + '/my/path/to/the/endpoint?limit=100&offset=0') - assert_equal(url1.build_url(query_params: params), url) + assert_equal(url, url1.build_url(query_params: params)) url1 = url1.one_more params = { 'limit' => 100, 'offset' => 0 } url = URI.parse(@host + '/' + @version + '/my/path/to/the/endpoint/one_more?limit=100&offset=0') - assert_equal(url1.build_url(query_params: params), url) + assert_equal(url, url1.build_url(query_params: params)) url2 = @client.my.path._('to').the.endpoint params = { 'limit' => 100, 'offset' => 0 } url = URI.parse(@host + '/' + @version + '/my/path/to/the/endpoint?limit=100&offset=0') - assert_equal(url2.build_url(query_params: params), url) + assert_equal(url, url2.build_url(query_params: params)) end def test_build_request name = 'get' args = nil response = @client.build_request(name, args) - assert_equal(response.status_code, 200) - assert_equal(response.body, 'message' => 'success') - assert_equal(response.headers, 'headers' => 'test') + assert_equal(200, response.status_code) + assert_equal({'message' => 'success'}, response.body) + assert_equal({'headers' => 'test'}, response.headers) + end + + def test_build_request_post_empty_content_type + headers = { + } + client = MockRequest.new( + host: 'https://localhost', + request_headers: headers, + version: 'v3' + ) + args = [{'request_body' => {"hogekey" => "hogevalue"}}] + client.build_request('post', args) + assert_equal('application/json', client.request['Content-Type']) + assert_equal('{"hogekey":"hogevalue"}', client.request.body) + end + + def test_build_request_get_application_json + headers = { + 'Content-Type' => 'application/json' + } + client = MockRequest.new( + host: 'https://localhost', + request_headers: headers, + version: 'v3' + ) + client.build_request('get', nil) + assert_equal('application/json', client.request['Content-Type']) + assert_equal(nil, client.request.body) + end + + def test_build_request_post_empty_body + headers = { + 'Content-Type' => 'application/json' + } + client = MockRequest.new( + host: 'https://localhost', + request_headers: headers, + version: 'v3' + ) + client.build_request('post', nil) + assert_equal('', client.request['Content-Type']) + assert_equal(nil, client.request.body) + end + + def test_build_request_post_multipart + headers = { + 'Content-Type' => 'multipart/form-data; boundary=xYzZY' + } + client = MockRequest.new( + host: 'https://localhost', + request_headers: headers, + ) + name = 'post' + args = [{'request_body' => 'hogebody'}] + client.build_request(name, args) + assert_equal('multipart/form-data; boundary=xYzZY', client.request['Content-Type']) + assert_equal('hogebody', client.request.body) end def add_ssl @@ -105,13 +162,13 @@ def add_ssl def test__ url1 = @client._('test') - assert_equal(url1.url_path, ['test']) + assert_equal(['test'], url1.url_path) end def test_method_missing response = @client.get - assert_equal(response.status_code, 200) - assert_equal(response.body, 'message' => 'success') - assert_equal(response.headers, 'headers' => 'test') + assert_equal(200, response.status_code) + assert_equal({'message' => 'success'}, response.body) + assert_equal({'headers' => 'test'}, response.headers) end end