Skip to content

Commit 3f36f74

Browse files
f-merchingor13
authored andcommitted
Inherit custom headers (#280)
1 parent e5325d1 commit 3f36f74

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/json_api_client/resource.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def with_headers(headers)
156156
#
157157
# @return [Hash] Headers
158158
def custom_headers
159-
_header_store.to_h
159+
return _header_store.to_h if superclass == Object
160+
161+
superclass.custom_headers.merge(_header_store.to_h)
160162
end
161163

162164
# Returns the requestor for this resource class

test/unit/custom_header_test.rb

+18
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ def test_class_method_headers
5959
end
6060
end
6161

62+
def test_custom_headers_are_inherited
63+
stub_request(:get, "http://example.com/custom_header_resources/1")
64+
.with(headers: {"X-My-Header" => "asdf"})
65+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
66+
data: {
67+
type: "custom_header_resources",
68+
id: "1",
69+
attributes: {
70+
title: "Rails is Omakase"
71+
}
72+
}
73+
}.to_json)
74+
75+
JsonApiClient::Resource.with_headers(x_my_header: "asdf") do
76+
CustomHeaderResource.find(1)
77+
end
78+
end
79+
6280
def test_multiple_threads
6381
thread_count = 10
6482

0 commit comments

Comments
 (0)