Skip to content

Commit f3475f0

Browse files
galiatGalia
authored and
Galia
committed
Allow users to define a proc that formats their response body. Similar 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
1 parent 8021c09 commit f3475f0

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ RspecApiDocumentation.configure do |config|
140140
# Can be :json, :xml, or a proc that will be passed the params
141141
config.post_body_formatter = Proc.new { |params| params }
142142

143+
# Change how the response body is formatted by default
144+
# Is proc that will be called with the response_content_type & response_body
145+
# by default response_content_type of `application/json` are pretty formated.
146+
config.response_body_formatter = Proc.new { |response_content_type, response_body| response_body }
147+
143148
# Change the embedded style for HTML output. This file will not be processed by
144149
# RspecApiDocumentation and should be plain CSS.
145150
config.html_embedded_css_file = nil

lib/rspec_api_documentation/client_base.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ def record_response_body(response_content_type, response_body)
8989
return nil if response_body.empty?
9090
if response_body.encoding == Encoding::ASCII_8BIT
9191
"[binary data]"
92-
elsif response_content_type =~ /application\/json/
93-
JSON.pretty_generate(JSON.parse(response_body))
9492
else
95-
response_body
93+
formatter = RspecApiDocumentation.configuration.response_body_formatter
94+
return formatter.call(response_content_type, response_body)
9695
end
9796
end
9897

lib/rspec_api_documentation/configuration.rb

+21
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ def self.add_setting(name, opts = {})
9393
# See RspecApiDocumentation::DSL::Endpoint#do_request
9494
add_setting :post_body_formatter, :default => Proc.new { |_| Proc.new { |params| params } }
9595

96+
# Change how the response body is formatted
97+
# Can be a proc that will be passed the response body
98+
#
99+
# RspecApiDocumentation.configure do |config|
100+
# config.response_body_formatter = Proc.new do |content_type, response_body|
101+
# # convert to whatever you want
102+
# response_body
103+
# end
104+
# end
105+
#
106+
# See RspecApiDocumentation::DSL::Endpoint#do_request
107+
add_setting :response_body_formatter, default: Proc.new { |_, _|
108+
Proc.new do |content_type, response_body|
109+
if content_type =~ /application\/json/
110+
JSON.pretty_generate(JSON.parse(response_body))
111+
else
112+
response_body
113+
end
114+
end
115+
}
116+
96117
def client_method=(new_client_method)
97118
RspecApiDocumentation::DSL::Resource.module_eval <<-RUBY
98119
alias :#{new_client_method} #{client_method}

spec/http_test_client_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,29 @@
133133
end
134134
end
135135
end
136+
137+
context "formating response body", :document => true do
138+
after do
139+
RspecApiDocumentation.instance_variable_set(:@configuration, RspecApiDocumentation::Configuration.new)
140+
end
141+
142+
before do
143+
RspecApiDocumentation.configure do |config|
144+
config.response_body_formatter =
145+
Proc.new do |_, response_body|
146+
response_body.upcase
147+
end
148+
end
149+
test_client.post "/greet?query=test+query", post_data, headers
150+
end
151+
152+
let(:post_data) { { :target => "nurse" }.to_json }
153+
let(:headers) { { "Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value" } }
154+
155+
it "it formats the response_body based on the defined proc" do |example|
156+
metadata = example.metadata[:requests].first
157+
expect(metadata[:response_body]).to be_present
158+
expect(metadata[:response_body]).to eq '{"HELLO":"NURSE"}'
159+
end
160+
end
136161
end

0 commit comments

Comments
 (0)