Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Content-Type matcher is more inclusive #242

Merged
merged 1 commit into from
Nov 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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