From 0f17254d4a4dc21448950dba9ee28a2e7408a58d Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Wed, 23 Nov 2016 11:55:24 +0100 Subject: [PATCH] Carry #579: Faraday::Utils::Headers YAML can be serialized (#634) * Bootstrap method that fixes deserializing with YAML * YAML encoding of Header * Utils.rb: Avoid relying on the #names method --- lib/faraday/utils.rb | 8 ++++++++ test/utils_test.rb | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/faraday/utils.rb b/lib/faraday/utils.rb index 66d3855f..11cdfa03 100644 --- a/lib/faraday/utils.rb +++ b/lib/faraday/utils.rb @@ -108,6 +108,14 @@ module Faraday } end + def init_with(coder) + @names = coder['names'] + end + + def encode_with(coder) + coder['names'] = @names + end + protected def names diff --git a/test/utils_test.rb b/test/utils_test.rb index ec5dc2a9..58f96a84 100644 --- a/test/utils_test.rb +++ b/test/utils_test.rb @@ -63,5 +63,15 @@ class TestUtils < Faraday::TestCase Faraday::Utils.default_uri_parser = old_parser end end + + # YAML parsing + + def test_headers_yaml_roundtrip + headers = Faraday::Utils::Headers.new('User-Agent' => 'safari', 'Content-type' => 'text/html') + result = YAML.load(headers.to_yaml) + + assert result.include?('user-agent'), 'Unable to hydrate to a correct Headers' + assert result.include?('content-type'), 'Unable to hydrate to a correct Headers' + end end