faraday 1.9.2 → 1.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/faraday/connection.rb +5 -5
- data/lib/faraday/deprecate.rb +109 -0
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request.rb +7 -6
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +2 -4
- data/lib/faraday/response.rb +3 -1
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +2 -0
- data/spec/faraday/deprecate_spec.rb +147 -0
- data/spec/faraday/request/instrumentation_spec.rb +7 -5
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/response/json_spec.rb +119 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/support/webmock_rack_app.rb +2 -1
- metadata +10 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc4d9e8162e07b207c4479907d77a4a4e0319b8f5d18bb1ce772eee108c0ed2a
|
4
|
+
data.tar.gz: c0753fcc74b610d2a4e0c5859917a5c98a0f177a7c83ba4c056e164dc0b37881
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d44910fd02c440547aa02843300dc0328b06697fe51d12406a8fc3216ee1cddef953f826bf807cedc71c14fcdb1aa28bd1e434693d45c8ffb8491706da81c6dc
|
7
|
+
data.tar.gz: 9b65773cc2275e0fdaddea26886b2e42befe982979d31ba24b816e679deeb496133ba6ca09d4cf5ba9695916cf20031982fad6df27bd056c052010b5e3d6cea1
|
data/lib/faraday/connection.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'faraday/deprecate'
|
4
|
+
|
3
5
|
module Faraday
|
4
6
|
# Connection objects manage the default properties and the middleware
|
5
7
|
# stack for fulfilling an HTTP request.
|
@@ -297,14 +299,12 @@ module Faraday
|
|
297
299
|
#
|
298
300
|
# @return [void]
|
299
301
|
def basic_auth(login, pass)
|
300
|
-
warn <<~TEXT
|
301
|
-
WARNING: `Faraday::Connection#basic_auth` is deprecated; it will be removed in version 2.0.
|
302
|
-
While initializing your connection, use `#request(:basic_auth, ...)` instead.
|
303
|
-
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
|
304
|
-
TEXT
|
305
302
|
set_authorization_header(:basic_auth, login, pass)
|
306
303
|
end
|
307
304
|
|
305
|
+
extend Faraday::Deprecate
|
306
|
+
deprecate :basic_auth, '#request(:basic_auth, ...)', '2.0'
|
307
|
+
|
308
308
|
# Sets up the Authorization header with the given token.
|
309
309
|
#
|
310
310
|
# @param token [String]
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
# @param new_klass [Class] new Klass to use
|
5
|
+
#
|
6
|
+
# @return [Class] A modified version of new_klass that warns on
|
7
|
+
# usage about deprecation.
|
8
|
+
# @see Faraday::Deprecate
|
9
|
+
module DeprecatedClass
|
10
|
+
def self.proxy_class(origclass, ver = '1.0')
|
11
|
+
proxy = Class.new(origclass) do
|
12
|
+
const_set('ORIG_CLASS', origclass)
|
13
|
+
|
14
|
+
class << self
|
15
|
+
extend Faraday::Deprecate
|
16
|
+
|
17
|
+
def ===(other)
|
18
|
+
(superclass == const_get('ORIG_CLASS') && other.is_a?(superclass)) || super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
proxy.singleton_class.send(:deprecate, :new, "#{origclass}.new", ver)
|
23
|
+
proxy.singleton_class.send(:deprecate, :inherited, origclass.name, ver)
|
24
|
+
proxy
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Deprecation using semver instead of date, based on Gem::Deprecate
|
29
|
+
# Provides a single method +deprecate+ to be used to declare when
|
30
|
+
# something is going away.
|
31
|
+
#
|
32
|
+
# class Legacy
|
33
|
+
# def self.klass_method
|
34
|
+
# # ...
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# def instance_method
|
38
|
+
# # ...
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# extend Faraday::Deprecate
|
42
|
+
# deprecate :instance_method, "X.z", '1.0'
|
43
|
+
#
|
44
|
+
# class << self
|
45
|
+
# extend Faraday::Deprecate
|
46
|
+
# deprecate :klass_method, :none, '1.0'
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
module Deprecate
|
50
|
+
def self.skip # :nodoc:
|
51
|
+
@skip ||= begin
|
52
|
+
case ENV['FARADAY_DEPRECATE'].to_s.downcase
|
53
|
+
when '1', 'warn' then :warn
|
54
|
+
else :skip
|
55
|
+
end
|
56
|
+
end
|
57
|
+
@skip == :skip
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.skip=(value) # :nodoc:
|
61
|
+
@skip = value ? :skip : :warn
|
62
|
+
end
|
63
|
+
|
64
|
+
# Temporarily turn off warnings. Intended for tests only.
|
65
|
+
def skip_during
|
66
|
+
original = Faraday::Deprecate.skip
|
67
|
+
Faraday::Deprecate.skip = true
|
68
|
+
yield
|
69
|
+
ensure
|
70
|
+
Faraday::Deprecate.skip = original
|
71
|
+
end
|
72
|
+
|
73
|
+
# Simple deprecation method that deprecates +name+ by wrapping it up
|
74
|
+
# in a dummy method. It warns on each call to the dummy method
|
75
|
+
# telling the user of +repl+ (unless +repl+ is :none) and the
|
76
|
+
# semver that it is planned to go away.
|
77
|
+
# @param name [Symbol] the method symbol to deprecate
|
78
|
+
# @param repl [#to_s, :none] the replacement to use, when `:none` it will
|
79
|
+
# alert the user that no replacemtent is present.
|
80
|
+
# @param ver [String] the semver the method will be removed.
|
81
|
+
def deprecate(name, repl, ver)
|
82
|
+
class_eval do
|
83
|
+
gem_ver = Gem::Version.new(ver)
|
84
|
+
old = "_deprecated_#{name}"
|
85
|
+
alias_method old, name
|
86
|
+
define_method name do |*args, &block|
|
87
|
+
mod = is_a? Module
|
88
|
+
target = mod ? "#{self}." : "#{self.class}#"
|
89
|
+
target_message = if name == :inherited
|
90
|
+
"Inheriting #{self}"
|
91
|
+
else
|
92
|
+
"#{target}#{name}"
|
93
|
+
end
|
94
|
+
|
95
|
+
msg = [
|
96
|
+
"NOTE: #{target_message} is deprecated",
|
97
|
+
repl == :none ? ' with no replacement' : "; use #{repl} instead. ",
|
98
|
+
"It will be removed in or after version #{gem_ver}",
|
99
|
+
"\n#{target}#{name} called from #{Gem.location_of_caller.join(':')}"
|
100
|
+
]
|
101
|
+
warn "#{msg.join}." unless Faraday::Deprecate.skip
|
102
|
+
send old, *args, &block
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
module_function :deprecate, :skip_during
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class Request
|
7
|
+
# Request middleware that encodes the body as JSON.
|
8
|
+
#
|
9
|
+
# Processes only requests with matching Content-type or those without a type.
|
10
|
+
# If a request doesn't have a type but has a body, it sets the Content-type
|
11
|
+
# to JSON MIME-type.
|
12
|
+
#
|
13
|
+
# Doesn't try to encode bodies that already are in string form.
|
14
|
+
class Json < Middleware
|
15
|
+
MIME_TYPE = 'application/json'
|
16
|
+
MIME_TYPE_REGEX = %r{^application/(vnd\..+\+)?json$}.freeze
|
17
|
+
|
18
|
+
def on_request(env)
|
19
|
+
match_content_type(env) do |data|
|
20
|
+
env[:body] = encode(data)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def encode(data)
|
27
|
+
::JSON.generate(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
def match_content_type(env)
|
31
|
+
return unless process_request?(env)
|
32
|
+
|
33
|
+
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
34
|
+
yield env[:body] unless env[:body].respond_to?(:to_str)
|
35
|
+
end
|
36
|
+
|
37
|
+
def process_request?(env)
|
38
|
+
type = request_type(env)
|
39
|
+
body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX))
|
40
|
+
end
|
41
|
+
|
42
|
+
def body?(env)
|
43
|
+
(body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
|
44
|
+
end
|
45
|
+
|
46
|
+
def request_type(env)
|
47
|
+
type = env[:request_headers][CONTENT_TYPE].to_s
|
48
|
+
type = type.split(';', 2).first if type.index(';')
|
49
|
+
type
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Faraday::Request.register_middleware(json: Faraday::Request::Json)
|
data/lib/faraday/request.rb
CHANGED
@@ -44,7 +44,8 @@ module Faraday
|
|
44
44
|
:TokenAuthentication,
|
45
45
|
'token_authentication'
|
46
46
|
],
|
47
|
-
instrumentation: [:Instrumentation, 'instrumentation']
|
47
|
+
instrumentation: [:Instrumentation, 'instrumentation'],
|
48
|
+
json: [:Json, 'json']
|
48
49
|
|
49
50
|
# @param request_method [String]
|
50
51
|
# @yield [request] for block customization, if block given
|
@@ -138,11 +139,11 @@ module Faraday
|
|
138
139
|
# @param serialised [Hash] the serialised object.
|
139
140
|
def marshal_load(serialised)
|
140
141
|
self.http_method = serialised[:http_method]
|
141
|
-
self.body
|
142
|
-
self.headers
|
143
|
-
self.path
|
144
|
-
self.params
|
145
|
-
self.options
|
142
|
+
self.body = serialised[:body]
|
143
|
+
self.headers = serialised[:headers]
|
144
|
+
self.path = serialised[:path]
|
145
|
+
self.params = serialised[:params]
|
146
|
+
self.options = serialised[:options]
|
146
147
|
end
|
147
148
|
|
148
149
|
# @return [Env] the Env for this Request
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class Response
|
7
|
+
# Parse response bodies as JSON.
|
8
|
+
class Json < Middleware
|
9
|
+
def initialize(app = nil, options = {})
|
10
|
+
super(app)
|
11
|
+
@parser_options = options[:parser_options]
|
12
|
+
@content_types = Array(options[:content_type] || /\bjson$/)
|
13
|
+
@preserve_raw = options[:preserve_raw]
|
14
|
+
end
|
15
|
+
|
16
|
+
def on_complete(env)
|
17
|
+
process_response(env) if parse_response?(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def process_response(env)
|
23
|
+
env[:raw_body] = env[:body] if @preserve_raw
|
24
|
+
env[:body] = parse(env[:body])
|
25
|
+
rescue StandardError, SyntaxError => e
|
26
|
+
raise Faraday::ParsingError.new(e, env[:response])
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse(body)
|
30
|
+
::JSON.parse(body, @parser_options || {}) unless body.strip.empty?
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse_response?(env)
|
34
|
+
process_response_type?(env) &&
|
35
|
+
env[:body].respond_to?(:to_str)
|
36
|
+
end
|
37
|
+
|
38
|
+
def process_response_type?(env)
|
39
|
+
type = response_type(env)
|
40
|
+
@content_types.empty? || @content_types.any? do |pattern|
|
41
|
+
pattern.is_a?(Regexp) ? type.match?(pattern) : type == pattern
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def response_type(env)
|
46
|
+
type = env[:response_headers][CONTENT_TYPE].to_s
|
47
|
+
type = type.split(';', 2).first if type.index(';')
|
48
|
+
type
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Faraday::Response.register_middleware(json: Faraday::Response::Json)
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'forwardable'
|
4
|
+
require 'logger'
|
4
5
|
require 'faraday/logging/formatter'
|
5
6
|
|
6
7
|
module Faraday
|
@@ -11,10 +12,7 @@ module Faraday
|
|
11
12
|
class Logger < Middleware
|
12
13
|
def initialize(app, logger = nil, options = {})
|
13
14
|
super(app)
|
14
|
-
logger ||=
|
15
|
-
require 'logger'
|
16
|
-
::Logger.new($stdout)
|
17
|
-
end
|
15
|
+
logger ||= ::Logger.new($stdout)
|
18
16
|
formatter_class = options.delete(:formatter) || Logging::Formatter
|
19
17
|
@formatter = formatter_class.new(logger: logger, options: options)
|
20
18
|
yield @formatter if block_given?
|
data/lib/faraday/response.rb
CHANGED
@@ -22,7 +22,8 @@ module Faraday
|
|
22
22
|
|
23
23
|
register_middleware File.expand_path('response', __dir__),
|
24
24
|
raise_error: [:RaiseError, 'raise_error'],
|
25
|
-
logger: [:Logger, 'logger']
|
25
|
+
logger: [:Logger, 'logger'],
|
26
|
+
json: [:Json, 'json']
|
26
27
|
|
27
28
|
def initialize(env = nil)
|
28
29
|
@env = Env.from(env) if env
|
@@ -42,6 +43,7 @@ module Faraday
|
|
42
43
|
def headers
|
43
44
|
finished? ? env.response_headers : {}
|
44
45
|
end
|
46
|
+
|
45
47
|
def_delegator :headers, :[]
|
46
48
|
|
47
49
|
def body
|
data/lib/faraday/version.rb
CHANGED
data/lib/faraday.rb
CHANGED
@@ -0,0 +1,147 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::DeprecatedClass do
|
4
|
+
class SampleClass < StandardError
|
5
|
+
attr_accessor :foo
|
6
|
+
|
7
|
+
def initialize(foo = nil)
|
8
|
+
@foo = foo || :foo
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
SampleDeprecatedClass = Faraday::DeprecatedClass.proxy_class(SampleClass)
|
13
|
+
|
14
|
+
it 'does not raise error for deprecated classes but prints an error message' do
|
15
|
+
error_message, foobar = with_warn_squelching { SampleDeprecatedClass.new(:foo_bar) }
|
16
|
+
expect(foobar).to be_a(SampleClass)
|
17
|
+
expect(foobar.foo).to eq(:foo_bar)
|
18
|
+
expect(error_message).to match(
|
19
|
+
Regexp.new(
|
20
|
+
'NOTE: SampleDeprecatedClass.new is deprecated; '\
|
21
|
+
'use SampleClass.new instead. It will be removed in or after version 1.0'
|
22
|
+
)
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'does not raise an error for inherited error-namespaced classes but prints an error message' do
|
27
|
+
error_message, = with_warn_squelching { Class.new(SampleDeprecatedClass) }
|
28
|
+
|
29
|
+
expect(error_message).to match(
|
30
|
+
Regexp.new(
|
31
|
+
'NOTE: Inheriting SampleDeprecatedClass is deprecated; '\
|
32
|
+
'use SampleClass instead. It will be removed in or after version 1.0'
|
33
|
+
)
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'allows backward-compatible class to be subclassed' do
|
38
|
+
expect do
|
39
|
+
with_warn_squelching { Class.new(SampleDeprecatedClass) }
|
40
|
+
end.not_to raise_error
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'allows rescuing of a current error with a deprecated error' do
|
44
|
+
expect { raise SampleClass, nil }.to raise_error(SampleDeprecatedClass)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'allows rescuing of a current error with a current error' do
|
48
|
+
expect { raise SampleClass, nil }.to raise_error(SampleClass)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'allows rescuing of a deprecated error with a deprecated error' do
|
52
|
+
expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleDeprecatedClass)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'allows rescuing of a deprecated error with a current error' do
|
56
|
+
expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleClass)
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'match behavior' do
|
60
|
+
class SampleDeprecatedClassA < SampleDeprecatedClass; end
|
61
|
+
class SampleDeprecatedClassB < SampleDeprecatedClass; end
|
62
|
+
|
63
|
+
class SampleDeprecatedClassAX < SampleDeprecatedClassA; end
|
64
|
+
|
65
|
+
class SampleClassA < SampleClass; end
|
66
|
+
|
67
|
+
describe 'undeprecated class' do
|
68
|
+
it 'is === to instance of deprecated class' do
|
69
|
+
expect(SampleDeprecatedClass.new.is_a?(SampleClass)).to be true
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'is === to instance of subclass of deprecated class' do
|
73
|
+
expect(SampleDeprecatedClassA.new.is_a?(SampleClass)).to be true
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'is === to instance of subclass of subclass of deprecated class' do
|
77
|
+
expect(SampleDeprecatedClassAX.new.is_a?(SampleClass)).to be true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'subclass of undeprecated class' do
|
82
|
+
it 'is not === to instance of undeprecated class' do
|
83
|
+
expect(SampleClass.new.is_a?(SampleClassA)).to be false
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'is not === to instance of deprecated class' do
|
87
|
+
expect(SampleDeprecatedClass.new.is_a?(SampleClassA)).to be false
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'deprecated class' do
|
92
|
+
it 'is === to instance of undeprecated class' do
|
93
|
+
expect(SampleDeprecatedClass.new.is_a?(SampleClass)).to be true
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'is === to instance of subclass of undeprecated class' do
|
97
|
+
expect(SampleClassA.superclass == SampleDeprecatedClass.superclass).to be true
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'is === to instance of subclass of deprecated class' do
|
101
|
+
expect(SampleDeprecatedClassA.new.is_a?(SampleDeprecatedClass)).to be true
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'is === to instance of subclass of subclass of deprecated class' do
|
105
|
+
expect(SampleDeprecatedClassAX.new.is_a?(SampleDeprecatedClass)).to be true
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe 'subclass of deprecated class' do
|
110
|
+
it 'is not === to instance of subclass of undeprecated class' do
|
111
|
+
expect(SampleClass.new.is_a?(SampleDeprecatedClassA)).to be false
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'is not === to instance of another subclass of deprecated class' do
|
115
|
+
expect(SampleDeprecatedClassB.new.is_a?(SampleDeprecatedClassA)).to be false
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'is === to instance of its subclass' do
|
119
|
+
expect(SampleDeprecatedClassAX.new.is_a?(SampleDeprecatedClassA)).to be true
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'is === to instance of deprecated class' do
|
123
|
+
expect(SampleDeprecatedClassB.new.is_a?(SampleDeprecatedClass)).to be true
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe 'subclass of subclass of deprecated class' do
|
128
|
+
it 'is not === to instance of subclass of another subclass of deprecated class' do
|
129
|
+
expect(SampleDeprecatedClassB.new.is_a?(SampleDeprecatedClassAX)).to be false
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'is not === to instance of its superclass' do
|
133
|
+
expect(SampleDeprecatedClass.new.is_a?(SampleDeprecatedClassA)).to be false
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def with_warn_squelching
|
139
|
+
stderr_catcher = StringIO.new
|
140
|
+
original_stderr = $stderr
|
141
|
+
$stderr = stderr_catcher
|
142
|
+
result = yield if block_given?
|
143
|
+
[stderr_catcher.tap(&:rewind).string, result]
|
144
|
+
ensure
|
145
|
+
$stderr = original_stderr
|
146
|
+
end
|
147
|
+
end
|
@@ -30,11 +30,13 @@ RSpec.describe Faraday::Request::Instrumentation do
|
|
30
30
|
|
31
31
|
it { expect(options.name).to eq('request.faraday') }
|
32
32
|
it 'defaults to ActiveSupport::Notifications' do
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
begin
|
34
|
+
res = options.instrumenter
|
35
|
+
rescue NameError => e
|
36
|
+
expect(e.to_s).to match('ActiveSupport')
|
37
|
+
else
|
38
|
+
expect(res).to eq(ActiveSupport::Notifications)
|
39
|
+
end
|
38
40
|
end
|
39
41
|
|
40
42
|
it 'instruments with default name' do
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Request::Json do
|
4
|
+
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }) }
|
5
|
+
|
6
|
+
def process(body, content_type = nil)
|
7
|
+
env = { body: body, request_headers: Faraday::Utils::Headers.new }
|
8
|
+
env[:request_headers]['content-type'] = content_type if content_type
|
9
|
+
middleware.call(Faraday::Env.from(env)).env
|
10
|
+
end
|
11
|
+
|
12
|
+
def result_body
|
13
|
+
result[:body]
|
14
|
+
end
|
15
|
+
|
16
|
+
def result_type
|
17
|
+
result[:request_headers]['content-type']
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'no body' do
|
21
|
+
let(:result) { process(nil) }
|
22
|
+
|
23
|
+
it "doesn't change body" do
|
24
|
+
expect(result_body).to be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "doesn't add content type" do
|
28
|
+
expect(result_type).to be_nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'empty body' do
|
33
|
+
let(:result) { process('') }
|
34
|
+
|
35
|
+
it "doesn't change body" do
|
36
|
+
expect(result_body).to be_empty
|
37
|
+
end
|
38
|
+
|
39
|
+
it "doesn't add content type" do
|
40
|
+
expect(result_type).to be_nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'string body' do
|
45
|
+
let(:result) { process('{"a":1}') }
|
46
|
+
|
47
|
+
it "doesn't change body" do
|
48
|
+
expect(result_body).to eq('{"a":1}')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'adds content type' do
|
52
|
+
expect(result_type).to eq('application/json')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'object body' do
|
57
|
+
let(:result) { process(a: 1) }
|
58
|
+
|
59
|
+
it 'encodes body' do
|
60
|
+
expect(result_body).to eq('{"a":1}')
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'adds content type' do
|
64
|
+
expect(result_type).to eq('application/json')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'empty object body' do
|
69
|
+
let(:result) { process({}) }
|
70
|
+
|
71
|
+
it 'encodes body' do
|
72
|
+
expect(result_body).to eq('{}')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'object body with json type' do
|
77
|
+
let(:result) { process({ a: 1 }, 'application/json; charset=utf-8') }
|
78
|
+
|
79
|
+
it 'encodes body' do
|
80
|
+
expect(result_body).to eq('{"a":1}')
|
81
|
+
end
|
82
|
+
|
83
|
+
it "doesn't change content type" do
|
84
|
+
expect(result_type).to eq('application/json; charset=utf-8')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'object body with vendor json type' do
|
89
|
+
let(:result) { process({ a: 1 }, 'application/vnd.myapp.v1+json; charset=utf-8') }
|
90
|
+
|
91
|
+
it 'encodes body' do
|
92
|
+
expect(result_body).to eq('{"a":1}')
|
93
|
+
end
|
94
|
+
|
95
|
+
it "doesn't change content type" do
|
96
|
+
expect(result_type).to eq('application/vnd.myapp.v1+json; charset=utf-8')
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'object body with incompatible type' do
|
101
|
+
let(:result) { process({ a: 1 }, 'application/xml; charset=utf-8') }
|
102
|
+
|
103
|
+
it "doesn't change body" do
|
104
|
+
expect(result_body).to eq(a: 1)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "doesn't change content type" do
|
108
|
+
expect(result_type).to eq('application/xml; charset=utf-8')
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Response::Json, type: :response do
|
4
|
+
let(:options) { {} }
|
5
|
+
let(:headers) { {} }
|
6
|
+
let(:middleware) do
|
7
|
+
described_class.new(lambda { |env|
|
8
|
+
Faraday::Response.new(env)
|
9
|
+
}, **options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def process(body, content_type = 'application/json', options = {})
|
13
|
+
env = {
|
14
|
+
body: body, request: options,
|
15
|
+
request_headers: Faraday::Utils::Headers.new,
|
16
|
+
response_headers: Faraday::Utils::Headers.new(headers)
|
17
|
+
}
|
18
|
+
env[:response_headers]['content-type'] = content_type if content_type
|
19
|
+
yield(env) if block_given?
|
20
|
+
middleware.call(Faraday::Env.from(env))
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'no type matching' do
|
24
|
+
it "doesn't change nil body" do
|
25
|
+
expect(process(nil).body).to be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'nullifies empty body' do
|
29
|
+
expect(process('').body).to be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'parses json body' do
|
33
|
+
response = process('{"a":1}')
|
34
|
+
expect(response.body).to eq('a' => 1)
|
35
|
+
expect(response.env[:raw_body]).to be_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with preserving raw' do
|
40
|
+
let(:options) { { preserve_raw: true } }
|
41
|
+
|
42
|
+
it 'parses json body' do
|
43
|
+
response = process('{"a":1}')
|
44
|
+
expect(response.body).to eq('a' => 1)
|
45
|
+
expect(response.env[:raw_body]).to eq('{"a":1}')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with default regexp type matching' do
|
50
|
+
it 'parses json body of correct type' do
|
51
|
+
response = process('{"a":1}', 'application/x-json')
|
52
|
+
expect(response.body).to eq('a' => 1)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'ignores json body of incorrect type' do
|
56
|
+
response = process('{"a":1}', 'text/json-xml')
|
57
|
+
expect(response.body).to eq('{"a":1}')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with array type matching' do
|
62
|
+
let(:options) { { content_type: %w[a/b c/d] } }
|
63
|
+
|
64
|
+
it 'parses json body of correct type' do
|
65
|
+
expect(process('{"a":1}', 'a/b').body).to be_a(Hash)
|
66
|
+
expect(process('{"a":1}', 'c/d').body).to be_a(Hash)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'ignores json body of incorrect type' do
|
70
|
+
expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'chokes on invalid json' do
|
75
|
+
expect { process('{!') }.to raise_error(Faraday::ParsingError)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'includes the response on the ParsingError instance' do
|
79
|
+
begin
|
80
|
+
process('{') { |env| env[:response] = Faraday::Response.new }
|
81
|
+
raise 'Parsing should have failed.'
|
82
|
+
rescue Faraday::ParsingError => e
|
83
|
+
expect(e.response).to be_a(Faraday::Response)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'HEAD responses' do
|
88
|
+
it "nullifies the body if it's only one space" do
|
89
|
+
response = process(' ')
|
90
|
+
expect(response.body).to be_nil
|
91
|
+
end
|
92
|
+
|
93
|
+
it "nullifies the body if it's two spaces" do
|
94
|
+
response = process(' ')
|
95
|
+
expect(response.body).to be_nil
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'JSON options' do
|
100
|
+
let(:body) { '{"a": 1}' }
|
101
|
+
let(:result) { { a: 1 } }
|
102
|
+
let(:options) do
|
103
|
+
{
|
104
|
+
parser_options: {
|
105
|
+
symbolize_names: true
|
106
|
+
}
|
107
|
+
}
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'passes relevant options to JSON parse' do
|
111
|
+
expect(::JSON).to receive(:parse)
|
112
|
+
.with(body, options[:parser_options])
|
113
|
+
.and_return(result)
|
114
|
+
|
115
|
+
response = process(body)
|
116
|
+
expect(response.body).to eq(result)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -38,6 +38,8 @@ require 'pry'
|
|
38
38
|
|
39
39
|
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
|
40
40
|
|
41
|
+
Faraday::Deprecate.skip = false
|
42
|
+
|
41
43
|
RSpec.configure do |config|
|
42
44
|
# rspec-expectations config goes here. You can use an alternate
|
43
45
|
# assertion/expectation library such as wrong or the stdlib/minitest
|
@@ -41,7 +41,8 @@ class WebmockRackApp
|
|
41
41
|
|
42
42
|
def req_headers(env)
|
43
43
|
http_headers = env.select { |k, _| k.start_with?('HTTP_') }
|
44
|
-
.
|
44
|
+
.map { |k, v| [k[5..-1], v] }
|
45
|
+
.to_h
|
45
46
|
|
46
47
|
special_headers = Faraday::Adapter::Rack::SPECIAL_HEADERS
|
47
48
|
http_headers.merge(env.select { |k, _| special_headers.include?(k) })
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.10.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "@technoweenie"
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2022-
|
13
|
+
date: 2022-08-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday-em_http
|
@@ -186,6 +186,7 @@ files:
|
|
186
186
|
- lib/faraday/autoload.rb
|
187
187
|
- lib/faraday/connection.rb
|
188
188
|
- lib/faraday/dependency_loader.rb
|
189
|
+
- lib/faraday/deprecate.rb
|
189
190
|
- lib/faraday/encoders/flat_params_encoder.rb
|
190
191
|
- lib/faraday/encoders/nested_params_encoder.rb
|
191
192
|
- lib/faraday/error.rb
|
@@ -205,9 +206,11 @@ files:
|
|
205
206
|
- lib/faraday/request/authorization.rb
|
206
207
|
- lib/faraday/request/basic_authentication.rb
|
207
208
|
- lib/faraday/request/instrumentation.rb
|
209
|
+
- lib/faraday/request/json.rb
|
208
210
|
- lib/faraday/request/token_authentication.rb
|
209
211
|
- lib/faraday/request/url_encoded.rb
|
210
212
|
- lib/faraday/response.rb
|
213
|
+
- lib/faraday/response/json.rb
|
211
214
|
- lib/faraday/response/logger.rb
|
212
215
|
- lib/faraday/response/raise_error.rb
|
213
216
|
- lib/faraday/utils.rb
|
@@ -228,6 +231,7 @@ files:
|
|
228
231
|
- spec/faraday/adapter_spec.rb
|
229
232
|
- spec/faraday/composite_read_io_spec.rb
|
230
233
|
- spec/faraday/connection_spec.rb
|
234
|
+
- spec/faraday/deprecate_spec.rb
|
231
235
|
- spec/faraday/error_spec.rb
|
232
236
|
- spec/faraday/middleware_spec.rb
|
233
237
|
- spec/faraday/options/env_spec.rb
|
@@ -239,8 +243,10 @@ files:
|
|
239
243
|
- spec/faraday/rack_builder_spec.rb
|
240
244
|
- spec/faraday/request/authorization_spec.rb
|
241
245
|
- spec/faraday/request/instrumentation_spec.rb
|
246
|
+
- spec/faraday/request/json_spec.rb
|
242
247
|
- spec/faraday/request/url_encoded_spec.rb
|
243
248
|
- spec/faraday/request_spec.rb
|
249
|
+
- spec/faraday/response/json_spec.rb
|
244
250
|
- spec/faraday/response/logger_spec.rb
|
245
251
|
- spec/faraday/response/middleware_spec.rb
|
246
252
|
- spec/faraday/response/raise_error_spec.rb
|
@@ -262,7 +268,7 @@ licenses:
|
|
262
268
|
- MIT
|
263
269
|
metadata:
|
264
270
|
homepage_uri: https://lostisland.github.io/faraday
|
265
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.
|
271
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.10.1
|
266
272
|
source_code_uri: https://github.com/lostisland/faraday
|
267
273
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
268
274
|
post_install_message:
|
@@ -274,7 +280,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
274
280
|
requirements:
|
275
281
|
- - ">="
|
276
282
|
- !ruby/object:Gem::Version
|
277
|
-
version: '2.
|
283
|
+
version: '2.4'
|
278
284
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
279
285
|
requirements:
|
280
286
|
- - ">="
|