diff --git a/docs/middleware/request/authentication.md b/docs/middleware/request/authentication.md index 867efa187..e15fd45fd 100644 --- a/docs/middleware/request/authentication.md +++ b/docs/middleware/request/authentication.md @@ -9,8 +9,8 @@ top_name: Back to Middleware top_link: ./list --- -Basic and Token authentication are handled by Faraday::Request::BasicAuthentication -and Faraday::Request::TokenAuthentication respectively. +Basic and Token authentication are handled by `Faraday::Request::BasicAuthentication` +and `Faraday::Request::TokenAuthentication` respectively. These can be added as middleware manually or through the helper methods. ### Basic Authentication diff --git a/docs/middleware/request/json.md b/docs/middleware/request/json.md index 943edb6f7..9ba064ad9 100644 --- a/docs/middleware/request/json.md +++ b/docs/middleware/request/json.md @@ -11,7 +11,7 @@ top_name: Back to Middleware top_link: ./list --- -The `Json Request` middleware converts a `Faraday::Request#body` hash of key/value pairs into a JSON request body. +The `JSON` request middleware converts a `Faraday::Request#body` hash of key/value pairs into a JSON request body. The middleware also automatically sets the `Content-Type` header to `application/json`, processes only requests with matching Content-Type or those without a type and doesn't try to encode bodies that already are in string form. diff --git a/docs/middleware/response/json.md b/docs/middleware/response/json.md index ca24ccd18..86e1560a7 100644 --- a/docs/middleware/response/json.md +++ b/docs/middleware/response/json.md @@ -11,11 +11,11 @@ top_name: Back to Middleware top_link: ./list --- -The `Json Response` middleware parses response body into a hash of key/value pairs. +The `JSON` response middleware parses response body into a hash of key/value pairs. The behaviour can be customized with the following options: -* **parser_options:** options that will be sent to the JSON.parse method. Defaults to {} -* **content_type:** Single value or Array of response content-types that should be processed. Can be either strings or Regex. Defaults to `/\bjson$/` -* **preserve_raw:** If set to true, the original un-parsed response will be stored in the `response.env[:raw_body]` property. Defaults to `false` +* **parser_options:** options that will be sent to the JSON.parse method. Defaults to {}. +* **content_type:** Single value or Array of response content-types that should be processed. Can be either strings or Regex. Defaults to `/\bjson$/`. +* **preserve_raw:** If set to true, the original un-parsed response will be stored in the `response.env[:raw_body]` property. Defaults to `false`. ### Example Usage diff --git a/lib/faraday/request/json.rb b/lib/faraday/request/json.rb index 998522751..a17a4dd06 100644 --- a/lib/faraday/request/json.rb +++ b/lib/faraday/request/json.rb @@ -36,7 +36,7 @@ def match_content_type(env) def process_request?(env) type = request_type(env) - body?(env) && (type.empty? || MIME_TYPE_REGEX =~ type) + body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX)) end def body?(env) diff --git a/lib/faraday/response/json.rb b/lib/faraday/response/json.rb index ff36da0a1..19fa72047 100644 --- a/lib/faraday/response/json.rb +++ b/lib/faraday/response/json.rb @@ -38,7 +38,7 @@ def parse_response?(env) def process_response_type?(env) type = response_type(env) @content_types.empty? || @content_types.any? do |pattern| - pattern.is_a?(Regexp) ? type =~ pattern : type == pattern + pattern.is_a?(Regexp) ? type.match?(pattern) : type == pattern end end diff --git a/spec/faraday/response/json_spec.rb b/spec/faraday/response/json_spec.rb index 874150801..a98e8a586 100644 --- a/spec/faraday/response/json_spec.rb +++ b/spec/faraday/response/json_spec.rb @@ -4,7 +4,6 @@ let(:options) { {} } let(:headers) { {} } let(:middleware) do - puts options described_class.new(lambda { |env| Faraday::Response.new(env) }, **options)