From f46676a5c7eada382913ae7484ef09b6ce6bc7e3 Mon Sep 17 00:00:00 2001 From: MarioRuiz Date: Tue, 19 Nov 2019 10:16:38 +0000 Subject: [PATCH] fix #17 --- lib/nice_http/http_methods.rb | 2 +- lib/nice_http/manage_request.rb | 6 +----- nice_http.gemspec | 4 ++-- spec/nice_http/post_spec.rb | 15 --------------- spec/nice_http/put_spec.rb | 10 ++++++++++ 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/lib/nice_http/http_methods.rb b/lib/nice_http/http_methods.rb index ec2ff3f..9803a87 100644 --- a/lib/nice_http/http_methods.rb +++ b/lib/nice_http/http_methods.rb @@ -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 diff --git a/lib/nice_http/manage_request.rb b/lib/nice_http/manage_request.rb index 3a6eb42..89ee6ca 100644 --- a/lib/nice_http/manage_request.rb +++ b/lib/nice_http/manage_request.rb @@ -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 @@ -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]) diff --git a/nice_http.gemspec b/nice_http.gemspec index 82a67f0..6fb1d38 100644 --- a/nice_http.gemspec +++ b/nice_http.gemspec @@ -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"] @@ -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"] diff --git a/spec/nice_http/post_spec.rb b/spec/nice_http/post_spec.rb index ed0f180..5c2fa80 100644 --- a/spec/nice_http/post_spec.rb +++ b/spec/nice_http/post_spec.rb @@ -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", diff --git a/spec/nice_http/put_spec.rb b/spec/nice_http/put_spec.rb index 1ea96f0..9e75f8a 100644 --- a/spec/nice_http/put_spec.rb +++ b/spec/nice_http/put_spec.rb @@ -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