Skip to content

Commit

Permalink
fix cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinschweikert committed Jan 14, 2025
1 parent 84b2cfd commit aa97836
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
alias CurlReq.Request

import CurlReq
import CurlReq.Request
18 changes: 7 additions & 11 deletions lib/curl_req/req.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ defmodule CurlReq.Req do
|> Req.merge(url: request.url)
|> Req.merge(method: request.method)

cookies =
request.cookies
|> Enum.map(fn {key, val} -> "#{key}=#{val}" end)
|> Enum.join(";")

req =
case request.user_agent do
:req -> req
Expand Down Expand Up @@ -151,12 +146,13 @@ defmodule CurlReq.Req do
req -> Req.Request.put_header(req, key, values)
end

req =
if cookies != "" do
Req.Request.put_header(req, "cookie", cookies)
else
req
end
cookies =
request.cookies
|> Enum.map(fn {key, val} ->
IO.iodata_to_binary([key, "=", val])
end)

req = if cookies != [], do: Req.Request.put_header(req, "cookie", cookies), else: req

proxy =
if request.proxy do
Expand Down
4 changes: 2 additions & 2 deletions lib/curl_req/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ defmodule CurlReq.Request do
{"user-agent", [user_agent | _]} ->
put_user_agent(request, user_agent)

{"cookie", [cookies | _]} ->
for cookie <- String.split(cookies, ";"), reduce: request do
{"cookie", cookies} ->
for cookie <- cookies, reduce: request do
request ->
[key, value] = String.split(cookie, "=")
put_cookie(request, key, value)
Expand Down
14 changes: 13 additions & 1 deletion test/curl_req_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ defmodule CurlReqTest do
}
end

test "complex cookie" do
request =
~CURL(curl --header 'Cookie: TealeafAkaSid=JA-JSAXRCLjKYhjV9IXTzYUbcV1Lnhqf; sapphire=1; visitorId=0184E4601D5A020183FFBB133 80347CE; GuestLocation=33196|25.660|-80.440|FL|US' -X GET https://example.com)

cookies = request.headers["cookie"]

assert "TealeafAkaSid=JA-JSAXRCLjKYhjV9IXTzYUbcV1Lnhqf" in cookies
assert "sapphire=1" in cookies
assert "visitorId=0184E4601D5A020183FFBB133 80347CE" in cookies
assert "GuestLocation=33196|25.660|-80.440|FL|US" in cookies
end

test "multiple headers with body" do
assert ~CURL(curl -H "accept-encoding: gzip" -H "authorization: Bearer 6e8f18e6-141b-4d12-8397-7e7791d92ed4:lon" -H "content-type: application/json" -H "user-agent: req/0.4.14" -d "{\"input\":[{\"leadFormFields\":{\"Company\":\"k\",\"Country\":\"DZ\",\"Email\":\"k\",\"FirstName\":\"k\",\"Industry\":\"CTO\",\"LastName\":\"k\",\"Phone\":\"k\",\"PostalCode\":\"1234ZZ\",\"jobspecialty\":\"engineer\",\"message\":\"I would like to know if Roche delivers to The Netherlands.\"}}],\"formId\":4318}" -X POST "https://example.com/rest/v1/leads/submitForm.json") ==
%Req.Request{
Expand Down Expand Up @@ -405,7 +417,7 @@ defmodule CurlReqTest do
assert ~CURL(http://example.com -b "name1=value1; name2=value2") ==
%Req.Request{
url: URI.parse("http://example.com"),
headers: %{"cookie" => ["name1=value1;name2=value2"]}
headers: %{"cookie" => ["name1=value1", "name2=value2"]}
}
end

Expand Down

0 comments on commit aa97836

Please sign in to comment.