faraday_middleware 0.9.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +16 -22
- data/lib/faraday_middleware/backwards_compatibility.rb +16 -12
- data/lib/faraday_middleware/gzip.rb +90 -0
- data/lib/faraday_middleware/instrumentation.rb +9 -3
- data/lib/faraday_middleware/rack_compatible.rb +18 -11
- data/lib/faraday_middleware/redirect_limit_reached.rb +16 -0
- data/lib/faraday_middleware/request/encode_json.rb +12 -9
- data/lib/faraday_middleware/request/method_override.rb +7 -6
- data/lib/faraday_middleware/request/oauth.rb +13 -10
- data/lib/faraday_middleware/request/oauth2.rb +37 -12
- data/lib/faraday_middleware/response/caching.rb +67 -15
- data/lib/faraday_middleware/response/chunked.rb +10 -6
- data/lib/faraday_middleware/response/follow_redirects.rb +85 -70
- data/lib/faraday_middleware/response/mashify.rb +2 -0
- data/lib/faraday_middleware/response/parse_dates.rb +6 -3
- data/lib/faraday_middleware/response/parse_json.rb +10 -10
- data/lib/faraday_middleware/response/parse_marshal.rb +3 -1
- data/lib/faraday_middleware/response/parse_xml.rb +4 -2
- data/lib/faraday_middleware/response/parse_yaml.rb +27 -3
- data/lib/faraday_middleware/response/rashify.rb +3 -1
- data/lib/faraday_middleware/response_middleware.rb +27 -18
- data/lib/faraday_middleware/version.rb +4 -1
- data/lib/faraday_middleware.rb +22 -16
- metadata +10 -65
- data/CHANGELOG.md +0 -10
- data/CONTRIBUTING.md +0 -46
- data/Rakefile +0 -28
- data/faraday_middleware.gemspec +0 -23
- data/lib/faraday_middleware/addressable_patch.rb +0 -20
- data/spec/caching_spec.rb +0 -170
- data/spec/chunked_spec.rb +0 -78
- data/spec/encode_json_spec.rb +0 -95
- data/spec/follow_redirects_spec.rb +0 -221
- data/spec/helper.rb +0 -59
- data/spec/mashify_spec.rb +0 -70
- data/spec/method_override_spec.rb +0 -92
- data/spec/oauth2_spec.rb +0 -118
- data/spec/oauth_spec.rb +0 -151
- data/spec/parse_dates_spec.rb +0 -44
- data/spec/parse_json_spec.rb +0 -112
- data/spec/parse_marshal_spec.rb +0 -16
- data/spec/parse_xml_spec.rb +0 -71
- data/spec/parse_yaml_spec.rb +0 -53
- data/spec/rashify_spec.rb +0 -47
@@ -1,42 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
|
5
|
+
# Main FaradayMiddleware module.
|
3
6
|
module FaradayMiddleware
|
4
7
|
# Internal: The base class for middleware that parses responses.
|
5
8
|
class ResponseMiddleware < Faraday::Middleware
|
6
|
-
CONTENT_TYPE = 'Content-Type'
|
9
|
+
CONTENT_TYPE = 'Content-Type'
|
7
10
|
|
8
11
|
class << self
|
9
12
|
attr_accessor :parser
|
10
13
|
end
|
11
14
|
|
12
15
|
# Store a Proc that receives the body and returns the parsed result.
|
13
|
-
def self.define_parser(parser = nil)
|
14
|
-
@parser = parser ||
|
16
|
+
def self.define_parser(parser = nil, &block)
|
17
|
+
@parser = parser ||
|
18
|
+
block ||
|
19
|
+
raise(ArgumentError, 'Define parser with a block')
|
15
20
|
end
|
16
21
|
|
17
22
|
def self.inherited(subclass)
|
18
23
|
super
|
19
|
-
subclass.load_error =
|
20
|
-
subclass.parser =
|
24
|
+
subclass.load_error = load_error if subclass.respond_to? :load_error=
|
25
|
+
subclass.parser = parser
|
21
26
|
end
|
22
27
|
|
23
28
|
def initialize(app = nil, options = {})
|
24
29
|
super(app)
|
25
30
|
@options = options
|
31
|
+
@parser_options = options[:parser_options]
|
26
32
|
@content_types = Array(options[:content_type])
|
27
33
|
end
|
28
34
|
|
29
35
|
def call(environment)
|
30
36
|
@app.call(environment).on_complete do |env|
|
31
|
-
if process_response_type?(response_type(env))
|
32
|
-
process_response(env)
|
33
|
-
end
|
37
|
+
process_response(env) if process_response_type?(response_type(env)) && parse_response?(env)
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
37
41
|
def process_response(env)
|
38
42
|
env[:raw_body] = env[:body] if preserve_raw?(env)
|
39
43
|
env[:body] = parse(env[:body])
|
44
|
+
rescue Faraday::ParsingError => e
|
45
|
+
raise Faraday::ParsingError.new(e.wrapped_exception, env[:response])
|
40
46
|
end
|
41
47
|
|
42
48
|
# Parse the response body.
|
@@ -45,10 +51,12 @@ module FaradayMiddleware
|
|
45
51
|
def parse(body)
|
46
52
|
if self.class.parser
|
47
53
|
begin
|
48
|
-
self.class.parser.call(body)
|
49
|
-
rescue StandardError, SyntaxError =>
|
50
|
-
raise
|
51
|
-
|
54
|
+
self.class.parser.call(body, @parser_options)
|
55
|
+
rescue StandardError, SyntaxError => e
|
56
|
+
raise e if e.is_a?(SyntaxError) &&
|
57
|
+
e.class.name != 'Psych::SyntaxError'
|
58
|
+
|
59
|
+
raise Faraday::ParsingError, e
|
52
60
|
end
|
53
61
|
else
|
54
62
|
body
|
@@ -62,9 +70,9 @@ module FaradayMiddleware
|
|
62
70
|
end
|
63
71
|
|
64
72
|
def process_response_type?(type)
|
65
|
-
@content_types.empty?
|
73
|
+
@content_types.empty? || @content_types.any? do |pattern|
|
66
74
|
pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
|
67
|
-
|
75
|
+
end
|
68
76
|
end
|
69
77
|
|
70
78
|
def parse_response?(env)
|
@@ -81,17 +89,18 @@ module FaradayMiddleware
|
|
81
89
|
attr_accessor :preserve_raw
|
82
90
|
|
83
91
|
def to_hash
|
84
|
-
super.update(:
|
92
|
+
super.update(preserve_raw: preserve_raw)
|
85
93
|
end
|
86
94
|
|
87
95
|
def each
|
88
96
|
return to_enum(:each) unless block_given?
|
97
|
+
|
89
98
|
super
|
90
99
|
yield :preserve_raw, preserve_raw
|
91
100
|
end
|
92
101
|
|
93
102
|
def fetch(key, *args)
|
94
|
-
if
|
103
|
+
if key == :preserve_raw
|
95
104
|
value = __send__(key)
|
96
105
|
value.nil? ? args.fetch(0) : value
|
97
106
|
else
|
@@ -102,9 +111,9 @@ module FaradayMiddleware
|
|
102
111
|
|
103
112
|
if defined?(Faraday::RequestOptions)
|
104
113
|
begin
|
105
|
-
Faraday::RequestOptions.from(:
|
114
|
+
Faraday::RequestOptions.from(preserve_raw: true)
|
106
115
|
rescue NoMethodError
|
107
|
-
Faraday::RequestOptions.
|
116
|
+
Faraday::RequestOptions.include OptionsExtension
|
108
117
|
end
|
109
118
|
end
|
110
119
|
end
|
data/lib/faraday_middleware.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
|
5
|
+
# Main FaradayMiddleware module.
|
3
6
|
module FaradayMiddleware
|
4
7
|
autoload :OAuth, 'faraday_middleware/request/oauth'
|
5
8
|
autoload :OAuth2, 'faraday_middleware/request/oauth2'
|
@@ -15,31 +18,34 @@ module FaradayMiddleware
|
|
15
18
|
autoload :Caching, 'faraday_middleware/response/caching'
|
16
19
|
autoload :Chunked, 'faraday_middleware/response/chunked'
|
17
20
|
autoload :RackCompatible, 'faraday_middleware/rack_compatible'
|
21
|
+
autoload :RedirectLimitReached, 'faraday_middleware/redirect_limit_reached'
|
18
22
|
autoload :FollowRedirects, 'faraday_middleware/response/follow_redirects'
|
19
23
|
autoload :Instrumentation, 'faraday_middleware/instrumentation'
|
24
|
+
autoload :Gzip, 'faraday_middleware/gzip'
|
20
25
|
|
21
26
|
if Faraday::Middleware.respond_to? :register_middleware
|
22
27
|
Faraday::Request.register_middleware \
|
23
|
-
:
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
28
|
+
oauth: -> { OAuth },
|
29
|
+
oauth2: -> { OAuth2 },
|
30
|
+
json: -> { EncodeJson },
|
31
|
+
method_override: -> { MethodOverride }
|
27
32
|
|
28
33
|
Faraday::Response.register_middleware \
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
33
|
-
:
|
34
|
-
:
|
35
|
-
:
|
36
|
-
:
|
37
|
-
:
|
38
|
-
:
|
39
|
-
:
|
34
|
+
mashify: -> { Mashify },
|
35
|
+
rashify: -> { Rashify },
|
36
|
+
json: -> { ParseJson },
|
37
|
+
json_fix: -> { ParseJson::MimeTypeFix },
|
38
|
+
xml: -> { ParseXml },
|
39
|
+
marshal: -> { ParseMarshal },
|
40
|
+
yaml: -> { ParseYaml },
|
41
|
+
dates: -> { ParseDates },
|
42
|
+
caching: -> { Caching },
|
43
|
+
follow_redirects: -> { FollowRedirects },
|
44
|
+
chunked: -> { Chunked }
|
40
45
|
|
41
46
|
Faraday::Middleware.register_middleware \
|
42
|
-
:
|
47
|
+
instrumentation: -> { Instrumentation },
|
48
|
+
gzip: -> { Gzip }
|
43
49
|
end
|
44
50
|
end
|
45
51
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Michaels-Ober
|
@@ -9,43 +9,23 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-10-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
requirements:
|
18
|
-
- - ">="
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: 0.7.4
|
21
|
-
- - "<"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: '0.10'
|
24
|
-
type: :runtime
|
25
|
-
prerelease: false
|
26
|
-
version_requirements: !ruby/object:Gem::Requirement
|
27
|
-
requirements:
|
28
|
-
- - ">="
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: 0.7.4
|
31
|
-
- - "<"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0.10'
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: bundler
|
36
16
|
requirement: !ruby/object:Gem::Requirement
|
37
17
|
requirements:
|
38
18
|
- - "~>"
|
39
19
|
- !ruby/object:Gem::Version
|
40
20
|
version: '1.0'
|
41
|
-
type: :
|
21
|
+
type: :runtime
|
42
22
|
prerelease: false
|
43
23
|
version_requirements: !ruby/object:Gem::Requirement
|
44
24
|
requirements:
|
45
25
|
- - "~>"
|
46
26
|
- !ruby/object:Gem::Version
|
47
27
|
version: '1.0'
|
48
|
-
description:
|
28
|
+
description:
|
49
29
|
email:
|
50
30
|
- sferik@gmail.com
|
51
31
|
- wynn.netherland@gmail.com
|
@@ -53,17 +33,14 @@ executables: []
|
|
53
33
|
extensions: []
|
54
34
|
extra_rdoc_files: []
|
55
35
|
files:
|
56
|
-
- CHANGELOG.md
|
57
|
-
- CONTRIBUTING.md
|
58
36
|
- LICENSE.md
|
59
37
|
- README.md
|
60
|
-
- Rakefile
|
61
|
-
- faraday_middleware.gemspec
|
62
38
|
- lib/faraday_middleware.rb
|
63
|
-
- lib/faraday_middleware/addressable_patch.rb
|
64
39
|
- lib/faraday_middleware/backwards_compatibility.rb
|
40
|
+
- lib/faraday_middleware/gzip.rb
|
65
41
|
- lib/faraday_middleware/instrumentation.rb
|
66
42
|
- lib/faraday_middleware/rack_compatible.rb
|
43
|
+
- lib/faraday_middleware/redirect_limit_reached.rb
|
67
44
|
- lib/faraday_middleware/request/encode_json.rb
|
68
45
|
- lib/faraday_middleware/request/method_override.rb
|
69
46
|
- lib/faraday_middleware/request/oauth.rb
|
@@ -80,21 +57,6 @@ files:
|
|
80
57
|
- lib/faraday_middleware/response/rashify.rb
|
81
58
|
- lib/faraday_middleware/response_middleware.rb
|
82
59
|
- lib/faraday_middleware/version.rb
|
83
|
-
- spec/caching_spec.rb
|
84
|
-
- spec/chunked_spec.rb
|
85
|
-
- spec/encode_json_spec.rb
|
86
|
-
- spec/follow_redirects_spec.rb
|
87
|
-
- spec/helper.rb
|
88
|
-
- spec/mashify_spec.rb
|
89
|
-
- spec/method_override_spec.rb
|
90
|
-
- spec/oauth2_spec.rb
|
91
|
-
- spec/oauth_spec.rb
|
92
|
-
- spec/parse_dates_spec.rb
|
93
|
-
- spec/parse_json_spec.rb
|
94
|
-
- spec/parse_marshal_spec.rb
|
95
|
-
- spec/parse_xml_spec.rb
|
96
|
-
- spec/parse_yaml_spec.rb
|
97
|
-
- spec/rashify_spec.rb
|
98
60
|
homepage: https://github.com/lostisland/faraday_middleware
|
99
61
|
licenses:
|
100
62
|
- MIT
|
@@ -107,32 +69,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
69
|
requirements:
|
108
70
|
- - ">="
|
109
71
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
72
|
+
version: '2.3'
|
111
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
74
|
requirements:
|
113
75
|
- - ">="
|
114
76
|
- !ruby/object:Gem::Version
|
115
|
-
version:
|
77
|
+
version: '0'
|
116
78
|
requirements: []
|
117
|
-
|
118
|
-
rubygems_version: 2.2.0
|
79
|
+
rubygems_version: 3.0.3.1
|
119
80
|
signing_key:
|
120
81
|
specification_version: 4
|
121
82
|
summary: Various middleware for Faraday
|
122
|
-
test_files:
|
123
|
-
- spec/caching_spec.rb
|
124
|
-
- spec/chunked_spec.rb
|
125
|
-
- spec/encode_json_spec.rb
|
126
|
-
- spec/follow_redirects_spec.rb
|
127
|
-
- spec/helper.rb
|
128
|
-
- spec/mashify_spec.rb
|
129
|
-
- spec/method_override_spec.rb
|
130
|
-
- spec/oauth2_spec.rb
|
131
|
-
- spec/oauth_spec.rb
|
132
|
-
- spec/parse_dates_spec.rb
|
133
|
-
- spec/parse_json_spec.rb
|
134
|
-
- spec/parse_marshal_spec.rb
|
135
|
-
- spec/parse_xml_spec.rb
|
136
|
-
- spec/parse_yaml_spec.rb
|
137
|
-
- spec/rashify_spec.rb
|
138
|
-
has_rdoc:
|
83
|
+
test_files: []
|
data/CHANGELOG.md
DELETED
data/CONTRIBUTING.md
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
## Contributing
|
2
|
-
In the spirit of [free software][free-sw], **everyone** is encouraged to help
|
3
|
-
improve this project.
|
4
|
-
|
5
|
-
[free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
|
6
|
-
|
7
|
-
Here are some ways *you* can contribute:
|
8
|
-
|
9
|
-
* by using alpha, beta, and prerelease versions
|
10
|
-
* by reporting bugs
|
11
|
-
* by suggesting new features
|
12
|
-
* by writing or editing documentation
|
13
|
-
* by writing specifications
|
14
|
-
* by writing code (**no patch is too small**: fix typos, add comments, clean up
|
15
|
-
inconsistent whitespace)
|
16
|
-
* by refactoring code
|
17
|
-
* by fixing [issues][]
|
18
|
-
* by reviewing patches
|
19
|
-
|
20
|
-
[issues]: https://github.com/lostisland/faraday_middleware/issues
|
21
|
-
|
22
|
-
## Submitting an Issue
|
23
|
-
We use the [GitHub issue tracker][issues] to track bugs and features. Before
|
24
|
-
submitting a bug report or feature request, check to make sure it hasn't
|
25
|
-
already been submitted. When submitting a bug report, please include a [Gist][]
|
26
|
-
that includes a stack trace and any details that may be necessary to reproduce
|
27
|
-
the bug, including your gem version, Ruby version, and operating system.
|
28
|
-
Ideally, a bug report should include a pull request with failing specs.
|
29
|
-
|
30
|
-
[gist]: https://gist.github.com/
|
31
|
-
|
32
|
-
## Submitting a Pull Request
|
33
|
-
1. [Fork the repository.][fork]
|
34
|
-
2. [Create a topic branch.][branch]
|
35
|
-
3. Add specs for your unimplemented feature or bug fix.
|
36
|
-
4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
|
37
|
-
5. Implement your feature or bug fix.
|
38
|
-
6. Run `bundle exec rake spec`. If your specs fail, return to step 5.
|
39
|
-
7. Run `open coverage/index.html`. If your changes are not completely covered
|
40
|
-
by your tests, return to step 3.
|
41
|
-
8. Add, commit, and push your changes.
|
42
|
-
9. [Submit a pull request.][pr]
|
43
|
-
|
44
|
-
[fork]: http://help.github.com/fork-a-repo/
|
45
|
-
[branch]: http://learn.github.com/p/branching.html
|
46
|
-
[pr]: http://help.github.com/send-pull-requests/
|
data/Rakefile
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
ruby_19 = RUBY_VERSION > '1.9'
|
2
|
-
ruby_mri = !defined?(RUBY_ENGINE) || 'ruby' == RUBY_ENGINE
|
3
|
-
default_gemfile = ENV['BUNDLE_GEMFILE'] =~ /Gemfile$/
|
4
|
-
|
5
|
-
if ruby_19 && ruby_mri && default_gemfile
|
6
|
-
task :default => [:enable_coverage, :spec, :quality]
|
7
|
-
else
|
8
|
-
task :default => [:spec]
|
9
|
-
end
|
10
|
-
|
11
|
-
require 'bundler'
|
12
|
-
Bundler::GemHelper.install_tasks
|
13
|
-
|
14
|
-
require 'rspec/core/rake_task'
|
15
|
-
RSpec::Core::RakeTask.new(:spec)
|
16
|
-
|
17
|
-
task :enable_coverage do
|
18
|
-
ENV['COVERAGE'] = 'yes'
|
19
|
-
end
|
20
|
-
|
21
|
-
desc %(Check code quality metrics with Cane)
|
22
|
-
task :quality do
|
23
|
-
sh 'cane',
|
24
|
-
'--abc-max=15',
|
25
|
-
'--style-measure=110',
|
26
|
-
'--gte=coverage/covered_percent,98.9',
|
27
|
-
'--max-violations=0'
|
28
|
-
end
|
data/faraday_middleware.gemspec
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'faraday_middleware/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.add_dependency 'faraday', ['>= 0.7.4', '< 0.10']
|
8
|
-
spec.add_development_dependency 'bundler', '~> 1.0'
|
9
|
-
spec.authors = ["Erik Michaels-Ober", "Wynn Netherland"]
|
10
|
-
spec.description = %q{Various middleware for Faraday}
|
11
|
-
spec.email = ['sferik@gmail.com', 'wynn.netherland@gmail.com']
|
12
|
-
spec.files = %w(CHANGELOG.md CONTRIBUTING.md LICENSE.md README.md Rakefile faraday_middleware.gemspec)
|
13
|
-
spec.files += Dir.glob("lib/**/*.rb")
|
14
|
-
spec.files += Dir.glob("spec/**/*")
|
15
|
-
spec.homepage = 'https://github.com/lostisland/faraday_middleware'
|
16
|
-
spec.licenses = ['MIT']
|
17
|
-
spec.name = 'faraday_middleware'
|
18
|
-
spec.require_paths = ['lib']
|
19
|
-
spec.required_rubygems_version = '>= 1.3.5'
|
20
|
-
spec.summary = spec.description
|
21
|
-
spec.test_files += Dir.glob("spec/**/*")
|
22
|
-
spec.version = FaradayMiddleware::VERSION
|
23
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'addressable/uri'
|
2
|
-
|
3
|
-
# feature-detect the bug
|
4
|
-
unless Addressable::URI.parse('/?a=1&b=2') === '/?b=2&a=1'
|
5
|
-
# fix `normalized_query` by sorting query key-value pairs
|
6
|
-
# (rejected: https://github.com/sporkmonger/addressable/issues/28)
|
7
|
-
class Addressable::URI
|
8
|
-
alias normalized_query_without_ordering_fix normalized_query
|
9
|
-
|
10
|
-
def normalized_query
|
11
|
-
fresh = @normalized_query.nil?
|
12
|
-
query = normalized_query_without_ordering_fix
|
13
|
-
if query && fresh
|
14
|
-
@normalized_query = query.split('&', -1).sort_by {|q| q[0..(q.index('=')||-1)] }.join('&')
|
15
|
-
else
|
16
|
-
query
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/spec/caching_spec.rb
DELETED
@@ -1,170 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'forwardable'
|
3
|
-
require 'fileutils'
|
4
|
-
require 'rack/cache'
|
5
|
-
require 'faraday'
|
6
|
-
require 'faraday_middleware/response/caching'
|
7
|
-
require 'faraday_middleware/rack_compatible'
|
8
|
-
|
9
|
-
describe FaradayMiddleware::Caching do
|
10
|
-
before do
|
11
|
-
@cache = TestCache.new
|
12
|
-
request_count = 0
|
13
|
-
response = lambda { |env|
|
14
|
-
[200, {'Content-Type' => 'text/plain'}, "request:#{request_count+=1}"]
|
15
|
-
}
|
16
|
-
|
17
|
-
@conn = Faraday.new do |b|
|
18
|
-
b.use CachingLint
|
19
|
-
b.use FaradayMiddleware::Caching, @cache, options
|
20
|
-
b.adapter :test do |stub|
|
21
|
-
stub.get('/', &response)
|
22
|
-
stub.get('/?foo=bar', &response)
|
23
|
-
stub.post('/', &response)
|
24
|
-
stub.get('/other', &response)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
let(:options) { {} }
|
30
|
-
|
31
|
-
extend Forwardable
|
32
|
-
def_delegators :@conn, :get, :post
|
33
|
-
|
34
|
-
it 'caches get requests' do
|
35
|
-
expect(get('/').body).to eq('request:1')
|
36
|
-
expect(get('/').body).to eq('request:1')
|
37
|
-
expect(get('/other').body).to eq('request:2')
|
38
|
-
expect(get('/other').body).to eq('request:2')
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'includes request params in the response' do
|
42
|
-
get('/') # make cache
|
43
|
-
response = get('/')
|
44
|
-
expect(response.env[:method]).to eq(:get)
|
45
|
-
expect(response.env[:url].request_uri).to eq('/')
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'caches requests with query params' do
|
49
|
-
expect(get('/').body).to eq('request:1')
|
50
|
-
expect(get('/?foo=bar').body).to eq('request:2')
|
51
|
-
expect(get('/?foo=bar').body).to eq('request:2')
|
52
|
-
expect(get('/').body).to eq('request:1')
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'does not cache post requests' do
|
56
|
-
expect(post('/').body).to eq('request:1')
|
57
|
-
expect(post('/').body).to eq('request:2')
|
58
|
-
expect(post('/').body).to eq('request:3')
|
59
|
-
end
|
60
|
-
|
61
|
-
context ":ignore_params" do
|
62
|
-
let(:options) { {:ignore_params => %w[ utm_source utm_term ]} }
|
63
|
-
|
64
|
-
it "strips ignored parameters from cache_key" do
|
65
|
-
expect(get('/').body).to eq('request:1')
|
66
|
-
expect(get('/?utm_source=a').body).to eq('request:1')
|
67
|
-
expect(get('/?utm_source=a&utm_term=b').body).to eq('request:1')
|
68
|
-
expect(get('/?utm_source=a&utm_term=b&foo=bar').body).to eq('request:2')
|
69
|
-
expect(get('/?foo=bar').body).to eq('request:2')
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
class TestCache < Hash
|
74
|
-
def read(key)
|
75
|
-
if cached = self[key]
|
76
|
-
Marshal.load(cached)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def write(key, data)
|
81
|
-
self[key] = Marshal.dump(data)
|
82
|
-
end
|
83
|
-
|
84
|
-
def fetch(key)
|
85
|
-
read(key) || yield.tap { |data| write(key, data) }
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
class CachingLint < Struct.new(:app)
|
90
|
-
def call(env)
|
91
|
-
app.call(env).on_complete do
|
92
|
-
raise "no headers" unless env[:response_headers].is_a? Hash
|
93
|
-
raise "no response" unless env[:response].is_a? Faraday::Response
|
94
|
-
# raise "env not identical" unless env[:response].env.object_id == env.object_id
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
# RackCompatible + Rack::Cache
|
101
|
-
describe FaradayMiddleware::RackCompatible, 'caching' do
|
102
|
-
include FileUtils
|
103
|
-
|
104
|
-
CACHE_DIR = File.expand_path('../../tmp/cache', __FILE__)
|
105
|
-
|
106
|
-
before do
|
107
|
-
rm_r CACHE_DIR if File.exists? CACHE_DIR
|
108
|
-
# force reinitializing cache dirs
|
109
|
-
Rack::Cache::Storage.instance.clear
|
110
|
-
|
111
|
-
request_count = 0
|
112
|
-
response = lambda { |env|
|
113
|
-
[200, { 'Content-Type' => 'text/plain',
|
114
|
-
'Cache-Control' => 'public, max-age=900',
|
115
|
-
},
|
116
|
-
"request:#{request_count+=1}"]
|
117
|
-
}
|
118
|
-
|
119
|
-
@conn = Faraday.new do |b|
|
120
|
-
b.use RackErrorsComplainer
|
121
|
-
|
122
|
-
b.use FaradayMiddleware::RackCompatible, Rack::Cache::Context,
|
123
|
-
:metastore => "file:#{CACHE_DIR}/rack/meta",
|
124
|
-
:entitystore => "file:#{CACHE_DIR}/rack/body",
|
125
|
-
:verbose => true
|
126
|
-
|
127
|
-
b.adapter :test do |stub|
|
128
|
-
stub.get('/', &response)
|
129
|
-
stub.post('/', &response)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
extend Forwardable
|
135
|
-
def_delegators :@conn, :get, :post
|
136
|
-
|
137
|
-
it 'caches get requests' do
|
138
|
-
response = get('/', :user_agent => 'test')
|
139
|
-
expect(response.body).to eq('request:1')
|
140
|
-
expect(response.env[:method]).to eq(:get)
|
141
|
-
expect(response.status).to eq(200)
|
142
|
-
|
143
|
-
response = get('/', :user_agent => 'test')
|
144
|
-
expect(response.body).to eq('request:1')
|
145
|
-
expect(response['content-type']).to eq('text/plain')
|
146
|
-
expect(response.env[:method]).to eq(:get)
|
147
|
-
expect(response.env[:request].respond_to?(:fetch)).to be_true
|
148
|
-
expect(response.status). to eq(200)
|
149
|
-
|
150
|
-
expect(post('/').body).to eq('request:2')
|
151
|
-
end
|
152
|
-
|
153
|
-
it 'does not cache post requests' do
|
154
|
-
expect(get('/').body).to eq('request:1')
|
155
|
-
expect(post('/').body).to eq('request:2')
|
156
|
-
expect(post('/').body).to eq('request:3')
|
157
|
-
end
|
158
|
-
|
159
|
-
# middleware to check whether "rack.errors" is free of error reports
|
160
|
-
class RackErrorsComplainer < Struct.new(:app)
|
161
|
-
def call(env)
|
162
|
-
response = app.call(env)
|
163
|
-
error_stream = env[:rack_errors]
|
164
|
-
if error_stream.respond_to?(:string) && error_stream.string.include?('error')
|
165
|
-
raise %(unexpected error in 'rack.errors': %p) % error_stream.string
|
166
|
-
end
|
167
|
-
response
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|