diff --git a/features/http_libraries/em_http_request.feature b/features/http_libraries/em_http_request.feature index 32e5f1f6..ba7f6a13 100644 --- a/features/http_libraries/em_http_request.feature +++ b/features/http_libraries/em_http_request.feature @@ -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 @@ -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 @@ -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 """ @@ -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 @@ -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 @@ -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 """ diff --git a/lib/vcr/structs.rb b/lib/vcr/structs.rb index 4685d9d2..a825e75b 100644 --- a/lib/vcr/structs.rb +++ b/lib/vcr/structs.rb @@ -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 diff --git a/spec/lib/vcr/structs_spec.rb b/spec/lib/vcr/structs_spec.rb index 85810635..da8c03ae 100644 --- a/spec/lib/vcr/structs_spec.rb +++ b/spec/lib/vcr/structs_spec.rb @@ -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) } @@ -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" } @@ -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) @@ -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 @@ -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 @@ -371,8 +386,7 @@ def assert_yielded_keys(hash, *keys) VCR::Response.new( response_status, headers.dup, - body.dup, - '1.1' + body.dup ) end