Skip to content

Commit

Permalink
Fixing already encoded bodies in plain requests
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjeffries committed Nov 21, 2017
1 parent 0c87d96 commit 364b5bf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lib/flexirest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,11 @@ def prepare_params
elsif http_method == :delete && @method[:options][:send_delete_body]
@post_params = default_params.merge(params || {})
@get_params = {}
elsif params.is_a? String
@post_params = params
@get_params = {}
else
@post_params = default_params.merge(params || {})
@post_params = (default_params || {}).merge(params || {})
@get_params = {}
end

Expand Down Expand Up @@ -362,10 +365,22 @@ def prepare_request_body(params = nil)
end
@body = ""
elsif request_body_type == :form_encoded
@body ||= (params || @post_params || {}).to_query
@body ||= if params.is_a?(String)
params
elsif @post_params.is_a?(String)
@post_params
else
(params || @post_params || {}).to_query
end
headers["Content-Type"] ||= "application/x-www-form-urlencoded"
elsif request_body_type == :json
@body ||= (params || @post_params || {}).to_json
@body ||= if params.is_a?(String)
params
elsif @post_params.is_a?(String)
@post_params
else
(params || @post_params || {}).to_json
end
headers["Content-Type"] ||= "application/json; charset=utf-8"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/flexirest/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Flexirest
VERSION = "1.5.4"
VERSION = "1.5.5"
end
12 changes: 12 additions & 0 deletions spec/lib/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ class DummyExample < Flexirest::Base
EmptyExample._request("http://api.example.com/")
end

it "allows already encoded bodies" do
Flexirest::ConnectionManager.reset!
connection = double("Connection")
allow(connection).to receive(:base_url).and_return("http://api.example.com")
expect(Flexirest::ConnectionManager).to receive(:get_connection).with("http://api.example.com/").and_return(connection)
expect(connection).
to receive(:post).
with("http://api.example.com/", "{\"test\":\"value\"}",an_instance_of(Hash)).
and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", response_headers:{}, status:200)))
EmptyExample._request("http://api.example.com/", :post, {test: "value"}.to_json, request_body_type: :json)
end

it "passes headers" do
stub_request(:get, "http://api.example.com/v1").
with(headers: {'Accept'=>'application/hal+json, application/json;q=0.5', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Connection'=>'Keep-Alive', 'Content-Type'=>'application/x-www-form-urlencoded', 'X-Something'=>'foo/bar', 'User-Agent'=>/Flexirest\//}).
Expand Down

0 comments on commit 364b5bf

Please sign in to comment.