Skip to content

Commit 01f53bd

Browse files
committed
fix: Do now allow sdk header override
1 parent a3a318d commit 01f53bd

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎lib/seam/request.rb‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ def self.create_faraday_client(endpoint, auth_headers, faraday_options = {}, far
2020
}
2121

2222
options = deep_merge(default_options, faraday_options)
23+
options.delete("headers")
24+
options[:headers] = merge_headers(
25+
faraday_options[:headers] || faraday_options["headers"] || {},
26+
auth_headers,
27+
default_headers
28+
)
2329

2430
default_faraday_retry_options = {
2531
max: 2,
@@ -104,7 +110,23 @@ def self.deep_merge(hash1, hash2)
104110
result
105111
end
106112

107-
private_class_method :deep_merge
113+
def self.merge_headers(*headers_list)
114+
result = {}
115+
header_keys = {}
116+
117+
headers_list.each do |headers|
118+
headers.each do |key, value|
119+
normalized_key = key.to_s.downcase
120+
result.delete(header_keys[normalized_key]) if header_keys.key?(normalized_key)
121+
header_keys[normalized_key] = key
122+
result[key] = value
123+
end
124+
end
125+
126+
result
127+
end
128+
129+
private_class_method :deep_merge, :merge_headers
108130
end
109131
end
110132
end

‎spec/seam_client/faraday_options_spec.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@
2727
expect(seam.client.headers["seam-sdk-name"]).to eq("seamapi/ruby")
2828
end
2929

30+
it "does not let faraday_options override auth or SDK headers" do
31+
seam = described_class.new(
32+
api_key: seed["seam_apikey1_token"],
33+
endpoint: endpoint,
34+
faraday_options: {
35+
headers: {
36+
"Authorization" => "Bearer caller_token",
37+
"Content-Type" => "text/plain",
38+
"Seam-Sdk-Name" => "caller-sdk",
39+
:"seam-sdk-version" => "0.0.0"
40+
}
41+
}
42+
)
43+
44+
expect(seam.client.headers["Authorization"]).to eq("Bearer #{seed["seam_apikey1_token"]}")
45+
expect(seam.client.headers["Content-Type"]).to eq("application/json")
46+
expect(seam.client.headers["seam-sdk-name"]).to eq("seamapi/ruby")
47+
expect(seam.client.headers["seam-sdk-version"]).to eq(Seam::VERSION)
48+
end
49+
3050
it "still authorizes requests against the server" do
3151
seam = described_class.new(
3252
api_key: seed["seam_apikey1_token"],

‎spec/seam_client/headers_spec.rb‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,38 @@
2929
expect(stub).to have_been_requested
3030
end
3131

32+
it "sends the SDK and auth headers over caller-provided duplicates" do
33+
stub = stub_request(:post, "#{Seam::DEFAULT_ENDPOINT}/devices/get")
34+
.with(
35+
headers: {
36+
"Authorization" => "Bearer seam_some_api_key",
37+
"Content-Type" => "application/json",
38+
"seam-sdk-name" => "seamapi/ruby",
39+
"seam-sdk-version" => Seam::VERSION
40+
}
41+
)
42+
.to_return(
43+
status: 200,
44+
body: {device: {device_id: device_id}}.to_json,
45+
headers: {"Content-Type" => "application/json"}
46+
)
47+
48+
seam = Seam.new(
49+
api_key: "seam_some_api_key",
50+
faraday_options: {
51+
headers: {
52+
"Authorization" => "Bearer caller_token",
53+
"Content-Type" => "text/plain",
54+
"Seam-Sdk-Name" => "caller-sdk",
55+
:"seam-sdk-version" => "0.0.0"
56+
}
57+
}
58+
)
59+
seam.devices.get(device_id: device_id)
60+
61+
expect(stub).to have_been_requested
62+
end
63+
3264
it "sends the workspace header with a personal access token" do
3365
stub = stub_request(:post, "#{Seam::DEFAULT_ENDPOINT}/devices/get")
3466
.with(

0 commit comments

Comments
 (0)