Skip to content

Commit

Permalink
Merge pull request #244 from galiat/rename-post-body-formatter
Browse files Browse the repository at this point in the history
rename config post_body_formatter to be request_body_formatter
  • Loading branch information
oestrich committed Dec 11, 2015
2 parents 878ee79 + bd38fcb commit 3a1d1ee
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ RspecApiDocumentation.configure do |config|

# Change how the post body is formatted by default, you can still override by `raw_post`
# Can be :json, :xml, or a proc that will be passed the params
config.post_body_formatter = Proc.new { |params| params }
config.request_body_formatter = Proc.new { |params| params }

# Change how the response body is formatted by default
# Is proc that will be called with the response_content_type & response_body
Expand Down
7 changes: 5 additions & 2 deletions lib/rspec_api_documentation/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,21 @@ def self.add_setting(name, opts = {})
add_setting :response_headers_to_include, :default => nil
add_setting :html_embedded_css_file, :default => nil

# renamed to request_body_formatter. here for backwards compatibility
add_setting :post_body_formatter, :default => nil

# Change how the post body is formatted by default, you can still override by `raw_post`
# Can be :json, :xml, or a proc that will be passed the params
#
# RspecApiDocumentation.configure do |config|
# config.post_body_formatter = Proc.new do |params|
# config.request_body_formatter = Proc.new do |params|
# # convert to whatever you want
# params.to_s
# end
# end
#
# See RspecApiDocumentation::DSL::Endpoint#do_request
add_setting :post_body_formatter, :default => Proc.new { |_| Proc.new { |params| params } }
add_setting :request_body_formatter, :default => Proc.new { |_| RspecApiDocumentation.configuration.post_body_formatter || Proc.new { |params| params } }

# Change how the response body is formatted
# Can be a proc that will be passed the response body
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec_api_documentation/dsl/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def do_request(extra_params = {})
if respond_to?(:raw_post)
params_or_body = raw_post
else
formatter = RspecApiDocumentation.configuration.post_body_formatter
formatter = RspecApiDocumentation.configuration.request_body_formatter
case formatter
when :json
params_or_body = params.empty? ? nil : params.to_json
Expand Down
2 changes: 1 addition & 1 deletion spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
its(:html_embedded_css_file) { should be_nil }

specify "post body formatter" do
expect(configuration.post_body_formatter.call({ :page => 1})).to eq({ :page => 1 })
expect(configuration.request_body_formatter.call({ :page => 1})).to eq({ :page => 1 })
end
end

Expand Down
16 changes: 13 additions & 3 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@
get "/orders" do
specify "formatting by json without parameters" do
RspecApiDocumentation.configure do |config|
config.post_body_formatter = :json
config.request_body_formatter = :json
end

expect(client).to receive(method).with(path, nil, nil)
Expand All @@ -530,6 +530,16 @@
let(:page) { 1 }

specify "formatting by json" do
RspecApiDocumentation.configure do |config|
config.request_body_formatter = :json
end

expect(client).to receive(method).with(path, { :page => 1 }.to_json , nil)

do_request
end

specify "formatting by json via legacy config" do
RspecApiDocumentation.configure do |config|
config.post_body_formatter = :json
end
Expand All @@ -541,7 +551,7 @@

specify "formatting by xml" do
RspecApiDocumentation.configure do |config|
config.post_body_formatter = :xml
config.request_body_formatter = :xml
end

expect(client).to receive(method).with(path, { :page => 1 }.to_xml , nil)
Expand All @@ -551,7 +561,7 @@

specify "formatting by proc" do
RspecApiDocumentation.configure do |config|
config.post_body_formatter = Proc.new do |params|
config.request_body_formatter = Proc.new do |params|
{ :from => "a proc" }.to_json
end
end
Expand Down

0 comments on commit 3a1d1ee

Please sign in to comment.