Skip to content

Commit 40cb4c1

Browse files
Merge pull request #5 from awwa/contenttype
Ability to set the Content-Type header
2 parents 6057567 + b4ff13e commit 40cb4c1

File tree

4 files changed

+89
-28
lines changed

4 files changed

+89
-28
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
bin
12
*.gem
23
*.rbc
34
/.config
@@ -28,7 +29,7 @@ build/
2829

2930
# for a library or gem, you might want to ignore these files since the code is
3031
# intended to run in multiple environments; otherwise, check them in:
31-
# Gemfile.lock
32+
Gemfile.lock
3233
# .ruby-version
3334
# .ruby-gemset
3435

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ gem install ruby_http_client
2727
```ruby
2828
require 'ruby_http_client'
2929
global_headers = {'Authorization' => 'Basic XXXXXXX' }
30-
client = SendGrid::Client(host: 'base_url', request_headers: global_headers)
30+
client = SendGrid::Client.new(host: 'base_url', request_headers: global_headers)
3131
client.your.api._(param).call.get
3232
puts response.status_code
3333
puts response.body
@@ -39,7 +39,7 @@ puts response.headers
3939
```ruby
4040
require 'ruby_http_client'
4141
global_headers = {'Authorization' => 'Basic XXXXXXX' }
42-
client = SendGrid::Client(host: 'base_url', request_headers: global_headers)
42+
client = SendGrid::Client.new(host: 'base_url', request_headers: global_headers)
4343
query_params = { 'hello' => 0, 'world' => 1 }
4444
request_headers = { 'X-Test' => 'test' }
4545
data = { 'some' => 1, 'awesome' => 2, 'data' => 3}

lib/ruby_http_client.rb

+14-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def initialize(response)
1919

2020
# A simple REST client.
2121
class Client
22-
attr_reader :host, :request_headers, :url_path
22+
attr_reader :host, :request_headers, :url_path, :request, :http
2323
# * *Args* :
2424
# - +host+ -> Base URL for the api. (e.g. https://api.sendgrid.com)
2525
# - +request_headers+ -> A hash of the headers you want applied on
@@ -132,18 +132,21 @@ def build_url(query_params: nil)
132132
def build_request(name, args)
133133
build_args(args) if args
134134
uri = build_url(query_params: @query_params)
135-
http = Net::HTTP.new(uri.host, uri.port)
136-
http = add_ssl(http)
135+
@http = add_ssl(Net::HTTP.new(uri.host, uri.port))
137136
net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize)
138-
request = net_http.new(uri.request_uri)
139-
request = build_request_headers(request)
140-
request.body = @request_body.to_json if @request_body
141-
if request.body
142-
request['Content-Type'] = 'application/json'
143-
elsif !request.body and (name.to_s == "post")
144-
request['Content-Type'] = ''
137+
@request = build_request_headers(net_http.new(uri.request_uri))
138+
if (@request_body &&
139+
(!@request_headers.has_key?('Content-Type') ||
140+
@request_headers['Content-Type'] == 'application/json')
141+
)
142+
@request.body = @request_body.to_json
143+
@request['Content-Type'] = 'application/json'
144+
elsif !@request_body and (name.to_s == "post")
145+
@request['Content-Type'] = ''
146+
else
147+
@request.body = @request_body
145148
end
146-
make_request(http, request)
149+
make_request(@http, @request)
147150
end
148151

149152
# Make the API call and return the response. This is separated into

test/test_ruby_http_client.rb

+71-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative '../lib/ruby_http_client'
1+
require 'ruby_http_client'
22
require 'minitest/autorun'
33

44
class MockResponse
@@ -50,49 +50,106 @@ def test_update_headers
5050
def test_build_request_headers
5151
request = {}
5252
request = @client.build_request_headers(request)
53-
assert_equal(@client.request_headers, request)
53+
assert_equal(request, @client.request_headers)
5454
end
5555

5656
def test_add_version
5757
url = ''
5858
@client.add_version(url)
59-
assert_equal(url, "/#{@version}")
59+
assert_equal("/#{@version}", url)
6060
end
6161

6262
def test_build_query_params
6363
url = ''
6464
query_params = { 'limit' => 100, 'offset' => 0 }
6565
url = @client.build_query_params(url, query_params)
66-
assert_equal(url, '?limit=100&offset=0')
66+
assert_equal('?limit=100&offset=0', url)
6767
end
6868

6969
def test_build_url
7070
url1 = @client.my.path.to.the.endpoint
7171
params = { 'limit' => 100, 'offset' => 0 }
7272
url = URI.parse(@host + '/' + @version +
7373
'/my/path/to/the/endpoint?limit=100&offset=0')
74-
assert_equal(url1.build_url(query_params: params), url)
74+
assert_equal(url, url1.build_url(query_params: params))
7575

7676
url1 = url1.one_more
7777
params = { 'limit' => 100, 'offset' => 0 }
7878
url = URI.parse(@host + '/' + @version +
7979
'/my/path/to/the/endpoint/one_more?limit=100&offset=0')
80-
assert_equal(url1.build_url(query_params: params), url)
80+
assert_equal(url, url1.build_url(query_params: params))
8181

8282
url2 = @client.my.path._('to').the.endpoint
8383
params = { 'limit' => 100, 'offset' => 0 }
8484
url = URI.parse(@host + '/' + @version +
8585
'/my/path/to/the/endpoint?limit=100&offset=0')
86-
assert_equal(url2.build_url(query_params: params), url)
86+
assert_equal(url, url2.build_url(query_params: params))
8787
end
8888

8989
def test_build_request
9090
name = 'get'
9191
args = nil
9292
response = @client.build_request(name, args)
93-
assert_equal(response.status_code, 200)
94-
assert_equal(response.body, 'message' => 'success')
95-
assert_equal(response.headers, 'headers' => 'test')
93+
assert_equal(200, response.status_code)
94+
assert_equal({'message' => 'success'}, response.body)
95+
assert_equal({'headers' => 'test'}, response.headers)
96+
end
97+
98+
def test_build_request_post_empty_content_type
99+
headers = {
100+
}
101+
client = MockRequest.new(
102+
host: 'https://localhost',
103+
request_headers: headers,
104+
version: 'v3'
105+
)
106+
args = [{'request_body' => {"hogekey" => "hogevalue"}}]
107+
client.build_request('post', args)
108+
assert_equal('application/json', client.request['Content-Type'])
109+
assert_equal('{"hogekey":"hogevalue"}', client.request.body)
110+
end
111+
112+
def test_build_request_get_application_json
113+
headers = {
114+
'Content-Type' => 'application/json'
115+
}
116+
client = MockRequest.new(
117+
host: 'https://localhost',
118+
request_headers: headers,
119+
version: 'v3'
120+
)
121+
client.build_request('get', nil)
122+
assert_equal('application/json', client.request['Content-Type'])
123+
assert_equal(nil, client.request.body)
124+
end
125+
126+
def test_build_request_post_empty_body
127+
headers = {
128+
'Content-Type' => 'application/json'
129+
}
130+
client = MockRequest.new(
131+
host: 'https://localhost',
132+
request_headers: headers,
133+
version: 'v3'
134+
)
135+
client.build_request('post', nil)
136+
assert_equal('', client.request['Content-Type'])
137+
assert_equal(nil, client.request.body)
138+
end
139+
140+
def test_build_request_post_multipart
141+
headers = {
142+
'Content-Type' => 'multipart/form-data; boundary=xYzZY'
143+
}
144+
client = MockRequest.new(
145+
host: 'https://localhost',
146+
request_headers: headers,
147+
)
148+
name = 'post'
149+
args = [{'request_body' => 'hogebody'}]
150+
client.build_request(name, args)
151+
assert_equal('multipart/form-data; boundary=xYzZY', client.request['Content-Type'])
152+
assert_equal('hogebody', client.request.body)
96153
end
97154

98155
def add_ssl
@@ -105,13 +162,13 @@ def add_ssl
105162

106163
def test__
107164
url1 = @client._('test')
108-
assert_equal(url1.url_path, ['test'])
165+
assert_equal(['test'], url1.url_path)
109166
end
110167

111168
def test_method_missing
112169
response = @client.get
113-
assert_equal(response.status_code, 200)
114-
assert_equal(response.body, 'message' => 'success')
115-
assert_equal(response.headers, 'headers' => 'test')
170+
assert_equal(200, response.status_code)
171+
assert_equal({'message' => 'success'}, response.body)
172+
assert_equal({'headers' => 'test'}, response.headers)
116173
end
117174
end

0 commit comments

Comments
 (0)