webmock 3.41.0 → 3.42.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
  SHA256:
3
- metadata.gz: 1e9683794e823cf95915c85c61925ffbae1c8577b62376c3c08dcf42c856adcd
4
- data.tar.gz: 46a910e0d7118b4da7083aaa7488f4a3c7f4854926f9afbc37707ea4163f9732
3
+ metadata.gz: 9bff4bef02b737edabff51c5a68f52b06ad2ee9b5280f11fd0b97ed93b85d13d
4
+ data.tar.gz: c9e3641f5610080b0c8968e2d8169b25ff0f78df0880e5ea07355fbc96012add
5
5
  SHA512:
6
- metadata.gz: d0dfedb9926e3aceefe66c995e2b27e59a2b7410dc1b371ebc82e8008565b08ef0606ac97248a83e7d9d190e90f10c878b78ab518d300993a0088b859f790530
7
- data.tar.gz: 915547cab2193d2170cbc28ec663bc8ff7a63f3fe8352ed1aa6da491cf987ea2a28dd7ea5260e4965ea87131d8b4c8a844523ebf918826cdd6614a715c57ab99
6
+ metadata.gz: 511313f001e32b777eacaf1666b51775f0690c63fc61551f12835dc55c3be54490c8111f21eaa3a4f772a3b26fed1a2f349bd39f352b6014d5509511c25681f5
7
+ data.tar.gz: a3623cc22f1c9719739fd3a804546acc619ef2a04624f343d162fbcb1ba49db5a3399a619fb5c6e0ae2842ab6f52e1d4e1a5affe366fa7ffa42149403bbf7063
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ # 3.42.0
4
+
5
+ * Fix VCR failing to replay responses recorded with the `auto_inflate` feature of the `http` gem (#1154)
6
+
7
+ Previously, for a real request made with `HTTP.use(:auto_inflate)`, `after_request` callbacks got the inflated body together with the `Content-Encoding: gzip` header of the compressed one. VCR, hooked into WebMock, recorded both, and replaying the cassette raised `Zlib::DataError: incorrect header check`, because the feature tried to inflate the body again. With VCR's `decode_compressed_response` option, recording raised `Zlib::GzipFile::Error` instead.
8
+
9
+ Callbacks now get the inflated body without the `Content-Encoding` header, as they do for a body that Net::HTTP inflates. A `Content-Length` header, when the response has one, gives the size of the inflated body. The response the client returns is unchanged, and so are the headers of a response with an empty body, such as one to a HEAD request.
10
+
11
+ A cassette recorded this way replays the response with those headers, while the real response keeps the headers of the compressed body.
12
+
13
+ This doesn't cover a client that inflates with a subclass of the feature, or responses returned by the `caching` feature of `http` 6. Callbacks get those as before.
14
+
15
+ Cassettes recorded with older versions that have the `Content-Encoding` header on an inflated body still fail. Record them again, or remove that header from their responses. Their `Content-Length` is still that of the compressed body.
16
+
3
17
  # 3.41.0
4
18
 
5
19
  WebMock now parses JSON request bodies with Ruby's `JSON.parse`, instead of its old parser, which converted JSON to YAML and failed on or misread some valid JSON. Every body pattern that matched on 3.40.0 still matches. If one matches only as the old parser reads the body, WebMock prints a deprecation warning, because 4.0 drops the old parser and the pattern won't match there.
@@ -7,6 +7,9 @@ module HTTP
7
7
 
8
8
  webmock_response.status = [status.to_i, reason]
9
9
 
10
+ # The caller gets the body before it is read, to see what it reads from.
11
+ body = self.body
12
+ yield body if block_given?
10
13
  webmock_response.body = body.to_s
11
14
  # This call is used to reset the body of the response to enable it to be streamed if necessary.
12
15
  # The `body.to_s` call above reads the body, which allows WebMock to trigger any registered callbacks.
@@ -87,10 +87,68 @@ module HTTP
87
87
  def perform
88
88
  return unless ::WebMock.net_connect_allowed?(request_signature.uri)
89
89
  response = @perform.call
90
- invoke_callbacks(response.to_webmock, real_request: true)
90
+ invoke_callbacks(webmock_response_for(response), real_request: true)
91
91
  response
92
92
  end
93
93
 
94
+ # The response that callbacks get for a real one. HTTP.rb's auto_inflate
95
+ # feature inflates the body and keeps the headers of the compressed one.
96
+ # WebMock replays a stub through the features, so a stub returning these
97
+ # headers with the inflated body, such as a VCR cassette, would be
98
+ # inflated again. Callbacks get headers that describe the inflated body.
99
+ def webmock_response_for(response)
100
+ return response.to_webmock unless ::WebMock::CallbackRegistry.any_callbacks? && auto_inflate?
101
+
102
+ inflated = false
103
+ webmock_response = response.to_webmock { |body| inflated = reads_from_inflater?(body) }
104
+ describe_inflated_body(webmock_response) if inflated
105
+ webmock_response
106
+ end
107
+
108
+ # Whether the client inflates with HTTP.rb's own feature. A subclass may
109
+ # do anything with the headers and the body, so its responses are left
110
+ # as they are.
111
+ def auto_inflate?
112
+ return false unless defined?(::HTTP::Features::AutoInflate)
113
+
114
+ features = @options.features.values.grep(::HTTP::Features::AutoInflate)
115
+ features.any? && features.all? { |feature| exactly?(::HTTP::Features::AutoInflate, feature) }
116
+ end
117
+
118
+ # Only evidence counts, so that callbacks never get other headers for a
119
+ # body that is still compressed. HTTP.rb shows inflation only in the
120
+ # stream the body reads from, which its logging feature may wrap. With
121
+ # HTTP.rb 6's caching feature the body is a String, which shows nothing.
122
+ # A subclass may do anything with what it reads, so it shows nothing too.
123
+ def reads_from_inflater?(body)
124
+ return false unless defined?(::HTTP::Response::Inflater) && exactly?(::HTTP::Response::Body, body)
125
+
126
+ stream = body.instance_variable_get(:@stream)
127
+ if defined?(::HTTP::Features::Logging::BodyLogger) && exactly?(::HTTP::Features::Logging::BodyLogger, stream)
128
+ stream = stream.instance_variable_get(:@stream)
129
+ end
130
+ exactly?(::HTTP::Response::Inflater, stream)
131
+ end
132
+
133
+ CLASS_OF = ::Kernel.instance_method(:class)
134
+ private_constant :CLASS_OF
135
+
136
+ # Asks Ruby and not the object, which may say anything of itself.
137
+ def exactly?(klass, object)
138
+ klass.equal?(CLASS_OF.bind(object).call)
139
+ end
140
+
141
+ # No Content-Encoding, as for a body Net::HTTP inflates, and its own
142
+ # length. A response with an empty body, such as one to HEAD, keeps its
143
+ # headers: they tell of a body that isn't there to be inflated again.
144
+ def describe_inflated_body(webmock_response)
145
+ return if webmock_response.body.empty?
146
+
147
+ headers = webmock_response.headers
148
+ headers.delete("Content-Encoding")
149
+ headers["Content-Length"] = webmock_response.body.bytesize.to_s if headers.key?("Content-Length")
150
+ end
151
+
94
152
  def halt
95
153
  raise ::WebMock::NetConnectNotAllowedError.new request_signature
96
154
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebMock
4
- VERSION = '3.41.0' unless defined?(::WebMock::VERSION)
4
+ VERSION = '3.42.0' unless defined?(::WebMock::VERSION)
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.41.0
4
+ version: 3.42.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -363,9 +363,9 @@ licenses:
363
363
  - MIT
364
364
  metadata:
365
365
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
366
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.41.0/CHANGELOG.md
367
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.41.0
368
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.41.0
366
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.42.0/CHANGELOG.md
367
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.42.0
368
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.42.0
369
369
  wiki_uri: https://github.com/bblimke/webmock/wiki
370
370
  rdoc_options: []
371
371
  require_paths: