Skip to content

Commit

Permalink
Prevent storing empty http_version on cassettes (vcr#709)
Browse files Browse the repository at this point in the history
* Accept cassetes without explicit http_version

* Remove all the empty http_version from features

Co-authored-by: Kurtis Rainbolt-Greene <[email protected]>
Co-authored-by: Olle Jonsson <[email protected]>
  • Loading branch information
3 people authored Mar 30, 2020
1 parent 6cd7072 commit 03af64f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
6 changes: 0 additions & 6 deletions features/http_libraries/em_http_request.feature
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ Feature: EM HTTP Request
body:
encoding: UTF-8
string: Hello foo
http_version:
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request:
method: get
Expand All @@ -104,7 +103,6 @@ Feature: EM HTTP Request
body:
encoding: UTF-8
string: Hello bar
http_version:
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request:
method: get
Expand All @@ -125,7 +123,6 @@ Feature: EM HTTP Request
body:
encoding: UTF-8
string: Hello bazz
http_version:
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0
"""
Expand Down Expand Up @@ -193,7 +190,6 @@ Feature: EM HTTP Request
body:
encoding: UTF-8
string: Hello foo
http_version:
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request:
method: get
Expand All @@ -214,7 +210,6 @@ Feature: EM HTTP Request
body:
encoding: UTF-8
string: Hello bar
http_version:
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request:
method: get
Expand All @@ -235,7 +230,6 @@ Feature: EM HTTP Request
body:
encoding: UTF-8
string: Hello bazz
http_version:
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/vcr/structs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ def to_hash
{
'status' => status.to_hash,
'headers' => headers,
'body' => serializable_body,
'http_version' => http_version
'body' => serializable_body
}.tap do |hash|
hash['http_version'] = http_version if http_version
hash['adapter_metadata'] = adapter_metadata unless adapter_metadata.empty?
end
end
Expand Down
32 changes: 23 additions & 9 deletions spec/lib/vcr/structs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def body_hash(key, value)
end

let(:status) { ResponseStatus.new(200, "OK") }
let(:response) { Response.new(status, { "foo" => ["bar"] }, "res body", "1.1") }
let(:response) { Response.new(status, { "foo" => ["bar"] }, "res body") }
let(:request) { Request.new(:get, "http://foo.com/", "req body", { "bar" => ["foo"] }) }
let(:recorded_at) { Time.utc(2011, 5, 4, 12, 30) }
let(:interaction) { HTTPInteraction.new(request, response, recorded_at) }
Expand All @@ -105,8 +105,7 @@ def body_hash(key, value)
'message' => 'OK'
},
'headers' => { "foo" => ["bar"] },
'body' => body_hash('string', 'res body'),
'http_version' => '1.1'
'body' => body_hash('string', 'res body')
},
'recorded_at' => "Wed, 04 May 2011 12:30:00 GMT"
}
Expand All @@ -120,12 +119,24 @@ def body_hash(key, value)
expect(HTTPInteraction.from_hash(hash).recorded_at).to eq(recorded_at)
end

it 'initializes the response http_version from the hash if it is included' do
hash['response']['http_version'] = '1.1'
interaction = HTTPInteraction.from_hash(hash)
expect(interaction.response.http_version).to eq('1.1')
end

it 'initializes the response adapter_metadata from the hash if it is included' do
hash['response']['adapter_metadata'] = { 'foo' => 12 }
interaction = HTTPInteraction.from_hash(hash)
expect(interaction.response.adapter_metadata).to eq("foo" => 12)
end

it 'works when the response http_version is missing' do
expect(hash['response'].keys).not_to include('http_version')
interaction = HTTPInteraction.from_hash(hash)
expect(interaction.response.http_version).to be_nil
end

it 'works when the response adapter_metadata is missing' do
expect(hash['response'].keys).not_to include('adapter_metadata')
interaction = HTTPInteraction.from_hash(hash)
Expand Down Expand Up @@ -274,8 +285,7 @@ def verify_encoding_error
'message' => 'OK'
},
'headers' => { "foo" => ["bar"] },
'body' => body_hash('string', 'res body'),
'http_version' => '1.1'
'body' => body_hash('string', 'res body')
})
end

Expand Down Expand Up @@ -323,13 +333,18 @@ def assert_yielded_keys(hash, *keys)
it 'yields the entries in the expected order so the hash can be serialized in that order' do
assert_yielded_keys hash, 'request', 'response', 'recorded_at'
assert_yielded_keys hash['request'], 'method', 'uri', 'body', 'headers'
assert_yielded_keys hash['response'], 'status', 'headers', 'body', 'http_version'
assert_yielded_keys hash['response'], 'status', 'headers', 'body'
assert_yielded_keys hash['response']['status'], 'code', 'message'
end

it 'yields `adapter_metadata` if it has any data' do
interaction.response.adapter_metadata['foo'] = 17
assert_yielded_keys hash['response'], 'status', 'headers', 'body', 'http_version', 'adapter_metadata'
assert_yielded_keys hash['response'], 'status', 'headers', 'body', 'adapter_metadata'
end

it 'yields `http_version` if it has any data' do
interaction.response.http_version = '1.1'
assert_yielded_keys hash['response'], 'status', 'headers', 'body', 'http_version'
end
end

Expand Down Expand Up @@ -371,8 +386,7 @@ def assert_yielded_keys(hash, *keys)
VCR::Response.new(
response_status,
headers.dup,
body.dup,
'1.1'
body.dup
)
end

Expand Down

0 comments on commit 03af64f

Please sign in to comment.