Skip to content

Commit

Permalink
Allow users to define a proc that formats their response body. Simila…
Browse files Browse the repository at this point in the history
…r to the post_body_formatter configuration.

Maintains backwards compatibility with pretty formatting content-types of `application/json`

the JSON API spec uses `application/vnd.api+json`
http://jsonapi.org/extensions/#extension-negotiation
  • Loading branch information
galiat authored and Galia committed Nov 10, 2015
1 parent 8021c09 commit f3475f0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ RspecApiDocumentation.configure do |config|
# Can be :json, :xml, or a proc that will be passed the params
config.post_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
# by default response_content_type of `application/json` are pretty formated.
config.response_body_formatter = Proc.new { |response_content_type, response_body| response_body }

# Change the embedded style for HTML output. This file will not be processed by
# RspecApiDocumentation and should be plain CSS.
config.html_embedded_css_file = nil
Expand Down
5 changes: 2 additions & 3 deletions lib/rspec_api_documentation/client_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,9 @@ def record_response_body(response_content_type, response_body)
return nil if response_body.empty?
if response_body.encoding == Encoding::ASCII_8BIT
"[binary data]"
elsif response_content_type =~ /application\/json/
JSON.pretty_generate(JSON.parse(response_body))
else
response_body
formatter = RspecApiDocumentation.configuration.response_body_formatter
return formatter.call(response_content_type, response_body)
end
end

Expand Down
21 changes: 21 additions & 0 deletions lib/rspec_api_documentation/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ def self.add_setting(name, opts = {})
# See RspecApiDocumentation::DSL::Endpoint#do_request
add_setting :post_body_formatter, :default => Proc.new { |_| Proc.new { |params| params } }

# Change how the response body is formatted
# Can be a proc that will be passed the response body
#
# RspecApiDocumentation.configure do |config|
# config.response_body_formatter = Proc.new do |content_type, response_body|
# # convert to whatever you want
# response_body
# end
# end
#
# See RspecApiDocumentation::DSL::Endpoint#do_request
add_setting :response_body_formatter, default: Proc.new { |_, _|
Proc.new do |content_type, response_body|
if content_type =~ /application\/json/
JSON.pretty_generate(JSON.parse(response_body))
else
response_body
end
end
}

def client_method=(new_client_method)
RspecApiDocumentation::DSL::Resource.module_eval <<-RUBY
alias :#{new_client_method} #{client_method}
Expand Down
25 changes: 25 additions & 0 deletions spec/http_test_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,29 @@
end
end
end

context "formating response body", :document => true do
after do
RspecApiDocumentation.instance_variable_set(:@configuration, RspecApiDocumentation::Configuration.new)
end

before do
RspecApiDocumentation.configure do |config|
config.response_body_formatter =
Proc.new do |_, response_body|
response_body.upcase
end
end
test_client.post "/greet?query=test+query", post_data, headers
end

let(:post_data) { { :target => "nurse" }.to_json }
let(:headers) { { "Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value" } }

it "it formats the response_body based on the defined proc" do |example|
metadata = example.metadata[:requests].first
expect(metadata[:response_body]).to be_present
expect(metadata[:response_body]).to eq '{"HELLO":"NURSE"}'
end
end
end

0 comments on commit f3475f0

Please sign in to comment.