diff --git a/README.md b/README.md index d96d961f..bedc7ac7 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,8 @@ RspecApiDocumentation.configure do |config| # 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. + # by default, a response body that is likely to be binary is replaced with the string + # "[binary data]" regardless of the media type. Otherwise, a response_content_type of `application/json` is pretty formatted. 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 diff --git a/lib/rspec_api_documentation/client_base.rb b/lib/rspec_api_documentation/client_base.rb index d234391b..db0560a3 100644 --- a/lib/rspec_api_documentation/client_base.rb +++ b/lib/rspec_api_documentation/client_base.rb @@ -87,12 +87,9 @@ def headers(method, path, params, request_headers) def record_response_body(response_content_type, response_body) return nil if response_body.empty? - if response_body.encoding == Encoding::ASCII_8BIT - "[binary data]" - else - formatter = RspecApiDocumentation.configuration.response_body_formatter - return formatter.call(response_content_type, response_body) - end + + formatter = RspecApiDocumentation.configuration.response_body_formatter + formatter.call(response_content_type, response_body) end def clean_out_uploaded_data(params, request_body) diff --git a/lib/rspec_api_documentation/configuration.rb b/lib/rspec_api_documentation/configuration.rb index 55054cb6..cd50524f 100644 --- a/lib/rspec_api_documentation/configuration.rb +++ b/lib/rspec_api_documentation/configuration.rb @@ -118,7 +118,9 @@ def self.add_setting(name, opts = {}) # 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/ + if response_body.encoding == Encoding::ASCII_8BIT + "[binary data]" + elsif content_type =~ /application\/.*json/ JSON.pretty_generate(JSON.parse(response_body)) else response_body