faraday-http-cache 2.0.0 → 2.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92ef39b3697fc4f6a5d9482310bcbe449c9e8707
4
- data.tar.gz: 43dd20b8cfce84a09a94d66bfb8843ff5e229184
3
+ metadata.gz: a2147bf25b6776f94d77ce49cd1147a790978446
4
+ data.tar.gz: cd45d9ddd630c3b627595e32826629a5c1e1d848
5
5
  SHA512:
6
- metadata.gz: afd6c8d4f672f3aab445124dbb604930e35c959ad27478093d34c8cd070f474b8b7ed89d8420147a890aeeb564ff19dedd4beedfd9ffe3e4c1a9ded568e36f19
7
- data.tar.gz: c74d2527626fc7ea4b67f83063006489ebb32a86b16edff9f0e2df5e240866d9156779d93cc3680ecd22c69e988753f372140c1b5cfdc7ba3deb6caa2f1d864f
6
+ metadata.gz: 432baf735bfa450affe7fa9a88aff70667a8a37b93fcbcb490ccd75f8b69498459716f273af9b32093f6435680a4f20f9d2d1ef9212d91646eba2ef53ad73745
7
+ data.tar.gz: ceb797b25268e5ef954684f7332ed790c49db1ca0f9709be87e4722a94553d806787932d5021e4829b65952be15982338266b377b65432da441584255f8712e1
data/LICENSE CHANGED
@@ -1,5 +1,3 @@
1
- Copyright 2012-2014 Plataformatec.
2
-
3
1
  Licensed under the Apache License, Version 2.0 (the "License");
4
2
  you may not use this file except in compliance with the License.
5
3
  You may obtain a copy of the License at
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Faraday Http Cache
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/plataformatec/faraday-http-cache.svg?branch=master)](https://travis-ci.org/plataformatec/faraday-http-cache)
3
+ [![Build Status](https://secure.travis-ci.org/sourcelevel/faraday-http-cache.svg?branch=master)](https://travis-ci.org/sourcelevel/faraday-http-cache)
4
4
 
5
5
  a [Faraday](https://github.com/lostisland/faraday) middleware that respects HTTP cache,
6
6
  by checking expiration and validation of the stored responses.
@@ -53,9 +53,16 @@ This type of store **might not be persisted across multiple processes or connect
53
53
  so it is probably not suitable for most production environments.
54
54
  Make sure that you configure a store that is suitable for you.
55
55
 
56
- the stdlib `JSON` module is used for serialization by default.
57
- If you expect to be dealing with images, you can use [Marshal][marshal] instead, or
58
- if you want to use another json library like `oj` or `yajl-ruby`.
56
+ The stdlib `JSON` module is used for serialization by default, which can struggle with unicode
57
+ characters in responses. For example, if your JSON returns `"name": "Raül"` then you might see
58
+ errors like:
59
+
60
+ ```
61
+ Response could not be serialized: "\xC3" from ASCII-8BIT to UTF-8. Try using Marshal to serialize.
62
+ ```
63
+
64
+ For full unicode support, or if you expect to be dealing with images, you can use
65
+ [Marshal][marshal] instead. Alternatively you could use another json library like `oj` or `yajl-ruby`.
59
66
 
60
67
  ```ruby
61
68
  client = Faraday.new do |builder|
@@ -120,7 +127,7 @@ end
120
127
 
121
128
  ## See it live
122
129
 
123
- You can clone this repository, install it's dependencies with Bundler (run `bundle install`) and
130
+ You can clone this repository, install its dependencies with Bundler (run `bundle install`) and
124
131
  execute the files under the `examples` directory to see a sample of the middleware usage.
125
132
 
126
133
  ## What gets cached?
@@ -153,6 +160,7 @@ client.get('http://site/api/some-private-resource') # => will be cached
153
160
 
154
161
  ## License
155
162
 
156
- Copyright (c) 2012-2014 Plataformatec. See LICENSE file.
163
+ Copyright (c) 2012-2018 Plataformatec.
164
+ Copyright (c) 2019 SourceLevel and contributors.
157
165
 
158
166
  [marshal]: http://www.ruby-doc.org/core-2.0/Marshal.html
@@ -19,7 +19,7 @@ module Faraday
19
19
  #
20
20
  # # Using the middleware with a simple client:
21
21
  # client = Faraday.new do |builder|
22
- # builder.user :http_cache, store: my_store_backend
22
+ # builder.use :http_cache, store: my_store_backend
23
23
  # builder.adapter Faraday.default_adapter
24
24
  # end
25
25
  #
@@ -61,7 +61,7 @@ module Faraday
61
61
  # The response was cached and the server has validated it with a 304 response.
62
62
  :valid,
63
63
 
64
- # The response was cache but was revalidated by the sserver.
64
+ # The response was cached but was not revalidated by the server.
65
65
  :invalid,
66
66
 
67
67
  # No response was found in the cache.
@@ -70,8 +70,8 @@ module Faraday
70
70
  # The response can't be cached.
71
71
  :uncacheable,
72
72
 
73
- # The request decided to ignore the cache.
74
- :bypass
73
+ # The request was cached but need to be revalidated by the server.
74
+ :must_revalidate,
75
75
  ].freeze
76
76
 
77
77
  # Public: Initializes a new HttpCache middleware.
@@ -132,15 +132,7 @@ module Faraday
132
132
  response = nil
133
133
 
134
134
  if @request.cacheable?
135
- response = if @request.no_cache?
136
- trace :bypass
137
- @app.call(env).on_complete do |fresh_env|
138
- response = Response.new(create_response(fresh_env))
139
- store(response)
140
- end
141
- else
142
- process(env)
143
- end
135
+ response = process(env)
144
136
  else
145
137
  trace :unacceptable
146
138
  response = @app.call(env)
@@ -194,10 +186,11 @@ module Faraday
194
186
 
195
187
  return fetch(env) if entry.nil?
196
188
 
197
- if entry.fresh?
189
+ if entry.fresh? && !@request.no_cache?
198
190
  response = entry.to_response(env)
199
191
  trace :fresh
200
192
  else
193
+ trace :must_revalidate
201
194
  response = validate(entry, env)
202
195
  end
203
196
 
@@ -39,7 +39,7 @@ module Faraday
39
39
  def serializable_hash
40
40
  {
41
41
  method: @method,
42
- url: @url,
42
+ url: @url.to_s,
43
43
  headers: @headers
44
44
  }
45
45
  end
@@ -50,7 +50,7 @@ module Faraday
50
50
  #
51
51
  # Returns true if the response is fresh, otherwise false.
52
52
  def fresh?
53
- !cache_control.must_revalidate? && !cache_control.no_cache? && ttl && ttl > 0
53
+ !cache_control.no_cache? && ttl && ttl > 0
54
54
  end
55
55
 
56
56
  # Internal: Checks if the Response returned a 'Not Modified' status.
@@ -56,7 +56,7 @@ module Faraday
56
56
  entries << entry
57
57
 
58
58
  cache.write(key, entries)
59
- rescue Encoding::UndefinedConversionError => e
59
+ rescue ::Encoding::UndefinedConversionError => e
60
60
  warn "Response could not be serialized: #{e.message}. Try using Marshal to serialize."
61
61
  raise e
62
62
  end
@@ -180,9 +180,12 @@ describe Faraday::HttpCache do
180
180
  end
181
181
 
182
182
  context 'when the request has a "no-cache" directive' do
183
- it 'by-passes the cache' do
184
- client.get('get', nil, 'Cache-Control' => 'no-cache')
185
- expect(client.get('get', nil, 'Cache-Control' => 'no-cache').body).to eq('2')
183
+ it 'revalidates the cache' do
184
+ expect(client.get('etag').body).to eq('1')
185
+ expect(client.get('etag', nil, 'Cache-Control' => 'no-cache').body).to eq('1')
186
+
187
+ expect(client.get('get', nil).body).to eq('2')
188
+ expect(client.get('etag', nil, 'Cache-Control' => 'no-cache').body).to eq('3')
186
189
  end
187
190
 
188
191
  it 'caches the response' do
@@ -224,7 +227,7 @@ describe Faraday::HttpCache do
224
227
 
225
228
  it 'logs that the request with "Last-Modified" was revalidated' do
226
229
  client.get('timestamped')
227
- expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /timestamped] valid, store') }
230
+ expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /timestamped] must_revalidate, valid, store') }
228
231
  expect(client.get('timestamped').body).to eq('1')
229
232
  end
230
233
 
@@ -235,7 +238,7 @@ describe Faraday::HttpCache do
235
238
 
236
239
  it 'logs that the request with "ETag" was revalidated' do
237
240
  client.get('etag')
238
- expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /etag] valid, store') }
241
+ expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /etag] must_revalidate, valid, store') }
239
242
  expect(client.get('etag').body).to eq('1')
240
243
  end
241
244
 
@@ -274,6 +277,11 @@ describe Faraday::HttpCache do
274
277
  expect(first_vary).not_to eql(second_vary)
275
278
  end
276
279
 
280
+ it 'caches non-stale response with "must-revalidate" directive' do
281
+ client.get('must-revalidate')
282
+ expect(client.get('must-revalidate').body).to eq('1')
283
+ end
284
+
277
285
  it 'raises an error when misconfigured' do
278
286
  expect {
279
287
  client = Faraday.new(url: ENV['FARADAY_SERVER']) do |stack|
@@ -89,8 +89,16 @@ describe Faraday::HttpCache::Response do
89
89
  expect(response).not_to be_fresh
90
90
  end
91
91
 
92
- it 'is not fresh if Cache Control has "must-revalidate"' do
92
+ it 'is fresh if the response contains "must-revalidate" and is not stale' do
93
93
  date = (Time.now - 200).httpdate
94
+ headers = { 'Cache-Control' => 'public, max-age=23880, must-revalidate, no-transform', 'Date' => date }
95
+ response = Faraday::HttpCache::Response.new(response_headers: headers)
96
+
97
+ expect(response).to be_fresh
98
+ end
99
+
100
+ it 'is not fresh if Cache Control has "must-revalidate" and is stale' do
101
+ date = (Time.now - 500).httpdate
94
102
  headers = { 'Cache-Control' => 'max-age=400, must-revalidate', 'Date' => date }
95
103
  response = Faraday::HttpCache::Response.new(response_headers: headers)
96
104
 
@@ -55,7 +55,7 @@ describe Faraday::HttpCache::Storage do
55
55
 
56
56
  expect {
57
57
  storage.write(request, response)
58
- }.to raise_error(Encoding::UndefinedConversionError)
58
+ }.to raise_error(::Encoding::UndefinedConversionError)
59
59
  expect(logger).to have_received(:warn).with(
60
60
  'Response could not be serialized: "\xE2" from ASCII-8BIT to UTF-8. Try using Marshal to serialize.'
61
61
  )
@@ -88,6 +88,10 @@ class TestApp < Sinatra::Base
88
88
  [200, { 'Date' => settings.yesterday, 'Expires' => settings.yesterday }, increment_counter]
89
89
  end
90
90
 
91
+ get '/must-revalidate' do
92
+ [200, { 'Date' => Time.now.httpdate, 'Cache-Control' => 'public, max-age=23880, must-revalidate, no-transform' }, increment_counter]
93
+ end
94
+
91
95
  get '/timestamped' do
92
96
  settings.counter += 1
93
97
  header = settings.counter > 2 ? '1' : '2'
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-http-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Mazza
8
+ - George Guimarães
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2016-11-16 00:00:00.000000000 Z
12
+ date: 2020-04-06 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: faraday
@@ -26,7 +27,7 @@ dependencies:
26
27
  version: '0.8'
27
28
  description: Middleware to handle HTTP caching
28
29
  email:
29
- - opensource@plataformatec.com.br
30
+ - opensource@sourcelevel.io
30
31
  executables: []
31
32
  extensions: []
32
33
  extra_rdoc_files: []
@@ -52,7 +53,7 @@ files:
52
53
  - spec/support/test_app.rb
53
54
  - spec/support/test_server.rb
54
55
  - spec/validation_spec.rb
55
- homepage: https://github.com/plataformatec/faraday-http-cache
56
+ homepage: https://github.com/sourcelevel/faraday-http-cache
56
57
  licenses:
57
58
  - Apache 2.0
58
59
  metadata: {}
@@ -72,21 +73,21 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
73
  version: '0'
73
74
  requirements: []
74
75
  rubyforge_project:
75
- rubygems_version: 2.5.1
76
+ rubygems_version: 2.6.8
76
77
  signing_key:
77
78
  specification_version: 4
78
79
  summary: A Faraday middleware that stores and validates cache expiration.
79
80
  test_files:
80
- - spec/binary_spec.rb
81
- - spec/cache_control_spec.rb
82
- - spec/http_cache_spec.rb
83
- - spec/instrumentation_spec.rb
84
- - spec/json_spec.rb
85
- - spec/request_spec.rb
86
- - spec/response_spec.rb
87
81
  - spec/spec_helper.rb
82
+ - spec/validation_spec.rb
83
+ - spec/json_spec.rb
84
+ - spec/instrumentation_spec.rb
88
85
  - spec/storage_spec.rb
89
- - spec/support/empty.png
86
+ - spec/http_cache_spec.rb
87
+ - spec/binary_spec.rb
90
88
  - spec/support/test_app.rb
89
+ - spec/support/empty.png
91
90
  - spec/support/test_server.rb
92
- - spec/validation_spec.rb
91
+ - spec/request_spec.rb
92
+ - spec/cache_control_spec.rb
93
+ - spec/response_spec.rb