Skip to content

Commit 2d0d8ac

Browse files
committed
Add test for request
1 parent 55054b6 commit 2d0d8ac

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

lib/ruby_http_client.rb

Lines changed: 8 additions & 10 deletions
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
@@ -142,23 +142,21 @@ def build_url(query_params: nil)
142142
def build_request(name, args)
143143
build_args(args) if args
144144
uri = build_url(query_params: @query_params)
145-
http = Net::HTTP.new(uri.host, uri.port)
146-
http = add_ssl(http)
145+
@http = add_ssl(Net::HTTP.new(uri.host, uri.port))
147146
net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize)
148-
request = net_http.new(uri.request_uri)
149-
request = build_request_headers(request)
147+
@request = build_request_headers(net_http.new(uri.request_uri))
150148
if (@request_body &&
151149
(!@request_headers.has_key?('Content-Type') ||
152150
@request_headers['Content-Type'] == 'application/json')
153151
)
154-
request.body = @request_body.to_json
155-
request['Content-Type'] = 'application/json'
152+
@request.body = @request_body.to_json
153+
@request['Content-Type'] = 'application/json'
156154
elsif !@request_body and (name.to_s == "post")
157-
request['Content-Type'] = ''
155+
@request['Content-Type'] = ''
158156
else
159-
request.body = @request_body
157+
@request.body = @request_body
160158
end
161-
make_request(http, request)
159+
make_request(@http, @request)
162160
end
163161

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

test/test_ruby_http_client.rb

Lines changed: 58 additions & 1 deletion
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
@@ -95,6 +95,63 @@ def test_build_request
9595
assert_equal(response.headers, 'headers' => 'test')
9696
end
9797

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)
153+
end
154+
98155
def add_ssl
99156
uri = URI.parse('https://localhost:4010')
100157
http = Net::HTTP.new(uri.host, uri.port)

0 commit comments

Comments
 (0)