diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index 4ef7313..67476b9 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -184,10 +184,11 @@ def create!(attributes = {}) # @param headers [Hash] The headers to send along # @param block [Block] The block where headers will be set for def with_headers(headers) + previous_headers = self._custom_headers || {} self._custom_headers = headers yield ensure - self._custom_headers = {} + self._custom_headers = previous_headers end # The current custom headers to send with any request made by this diff --git a/test/unit/custom_header_test.rb b/test/unit/custom_header_test.rb index cb6217e..6a2dd5b 100644 --- a/test/unit/custom_header_test.rb +++ b/test/unit/custom_header_test.rb @@ -23,6 +23,39 @@ def test_can_set_custom_headers end end + def test_can_nest_with_headers + stub_request(:get, "http://example.com/custom_header_resources/1") + .with(headers: {"X-My-Header" => "nested_value"}) + .to_return(headers: {content_type: "application/vnd.api+json"}, body: { + data: { + type: "custom_header_resources", + id: "1", + attributes: { + title: "Rails is Omakase" + } + } + }.to_json) + + stub_request(:get, "http://example.com/custom_header_resources/2") + .with(headers: {"X-My-Header" => "first_value"}) + .to_return(headers: {content_type: "application/vnd.api+json"}, body: { + data: { + type: "custom_header_resources", + id: "2", + attributes: { + title: "Hello World" + } + } + }.to_json) + + CustomHeaderResource.with_headers(x_my_header: "first_value") do + CustomHeaderResource.with_headers(x_my_header: "nested_value") do + CustomHeaderResource.find(1) + end + CustomHeaderResource.find(2) + end + end + def test_can_set_custom_accept_headers stub_request(:get, "http://example.com/custom_header_resources/1") .with(headers: {"Accept" => "application/vnd.api+json,application/vnd.api+json;version=2"})