Skip to content

Commit b4bd64a

Browse files
Don't mutate user-given headers (#101)
1 parent 43658bf commit b4bd64a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/rest.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ function _user_agent()
6262
end
6363

6464
# Ensures that the given headers contain the required values.
65-
function _ensure_headers!(h = HTTP.Headers())
65+
function _ensure_headers(headers=nothing)
66+
h = isnothing(headers) ? HTTP.Headers() : copy(headers)
6667
_haskeyfold(h, "accept") || push!(h, "Accept" => "application/json")
6768
_haskeyfold(h, "content-type") || push!(h, "Content-Type" => "application/json")
6869
_haskeyfold(h, "user-agent") || push!(h, "User-Agent" => _user_agent())
@@ -71,7 +72,7 @@ end
7172

7273
function get_access_token(ctx::Context, creds::ClientCredentials)::AccessToken
7374
url = _get_client_credentials_url(creds)
74-
h = _ensure_headers!()
75+
h = _ensure_headers()
7576
body = """{
7677
"client_id": $(repr(creds.client_id)),
7778
"client_secret": $(repr(creds.client_secret)),
@@ -181,7 +182,7 @@ function request(
181182
headers = h, query = nothing, body = b, kw...
182183
)::HTTP.Response
183184
isnothing(body) && (body = UInt8[])
184-
headers = _ensure_headers!(headers)
185+
headers = _ensure_headers(headers)
185186
_authenticate!(ctx, headers)
186187
opts = (;redirect = false, connection_limit = 4096)
187188
return HTTP.request(method, url, headers; query = query, body = body, opts..., kw...)

test/rest.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@ import HTTP
88
rsp = RAI.request(ctx, "GET", "https://www.example.com", headers = ["test" => "value"])
99
@test rsp isa HTTP.Response
1010
end
11+
12+
@testset "_ensure_headers" begin
13+
h1 = RAI._ensure_headers()
14+
ks = first.(h1)
15+
@test in("Accept", ks)
16+
@test in("Content-Type", ks)
17+
@test in("User-Agent", ks)
18+
# Do not mutate the given headers
19+
h2 = RAI._ensure_headers(h1)
20+
@test h1 !== h2
21+
end

0 commit comments

Comments
 (0)