Skip to content

Commit

Permalink
fix #17
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Nov 19, 2019
1 parent e10a7b9 commit f46676a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/nice_http/http_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def post(*arguments)
arguments[0][:data].add_field(key, value) #add to Headers
}
resp = @http.request(arguments[0][:data])
elsif headers_t["Content-Type"].include?("application/x-www-form-urlencoded")
elsif headers_t["Content-Type"].to_s.include?("application/x-www-form-urlencoded")
encoded_form = URI.encode_www_form(arguments[0][:data])
resp = @http.request_post(path, encoded_form, headers_t)
data = resp.body
Expand Down
6 changes: 1 addition & 5 deletions lib/nice_http/manage_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def manage_request(*arguments)
elsif headers_t["Content-Type"].to_s() != ""
content_type_included = true
end
if !content_type_included and data.kind_of?(Hash)
if !content_type_included and (data.kind_of?(Hash) or data.kind_of?(Array))
headers_t["Content-Type"] = "application/json"
content_type_included = true
end
Expand Down Expand Up @@ -132,10 +132,6 @@ def manage_request(*arguments)
#todo: implement set_nested
data_arr = Array.new()
data.each_with_index { |row, indx|
unless row.kind_of?(Hash)
@logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
return :error, :error, :error
end
if arguments[0].include?(:values_for)
if arguments[0][:values_for].is_a?(Array)
data_n = row.set_values(arguments[0][:values_for][indx])
Expand Down
4 changes: 2 additions & 2 deletions nice_http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_http'
s.version = '1.7.21'
s.version = '1.7.22'
s.summary = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Get http logs and statistics automatically. Use hashes on your requests. Access JSON even easier."
s.description = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Get http logs and statistics automatically. Use hashes on your requests. Access JSON even easier."
s.authors = ["Mario Ruiz"]
Expand All @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = ["LICENSE","README.md"]
s.homepage = 'https://github.com/MarioRuiz/nice_http'
s.license = 'MIT'
s.add_runtime_dependency 'nice_hash', '~> 1.15', '>= 1.15.3'
s.add_runtime_dependency 'nice_hash', '~> 1.15', '>= 1.15.4'
s.add_development_dependency 'rspec', '~> 3.8', '>= 3.8.0'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ["lib"]
Expand Down
15 changes: 0 additions & 15 deletions spec/nice_http/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,6 @@
expect(resp.data.json(:job)).to eq ["dev", "dev"]
end

it "shows wrong format on request when not array of hashes" do
request = {
path: "/api/users",
headers: {"Content-Type": "application/json"},
data: [
{name: "morpheus", job: "leader"},
{name: "peter", job: "vicepresident"},
100,
],
}
resp = @http.post(request)
content = File.read("./nice_http.log")
expect(content).to match /Wrong format on request/
end

it "changes all values on array request when values_for is array of hashes" do
request = {
path: "/api/users",
Expand Down
10 changes: 10 additions & 0 deletions spec/nice_http/put_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@
expect(content).to match /There was a problem converting to json/
end

it 'accepts a json array on first level request' do
request = {
path: "/api/users",
data: [20, 30, 40]
}
resp = @http.post(request)
expect(resp.code).to eq 201
expect(resp.data.json).to eq [20, 30, 40]
end

#todo: add tests for headers, encoding and cookies

end

0 comments on commit f46676a

Please sign in to comment.