Skip to content

Commit c027c7a

Browse files
authored
fix case sensitive headers (#392)
* fix header * add test * more tests * eh * eh * one more test * format a bit
1 parent c5480c1 commit c027c7a

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

.typos.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
"som" = "som" # ./test/ch/ecto_type_test.exs
33
"ECT" = "ECT" # ./test/ch/query_test.exs
44
"Evn" = "Evn" # ./CHANGELOG.md
5+
"Fo" = "Fo" # ./test/ch/query_test.exs

lib/ch.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ defmodule Ch do
221221
headers =
222222
options
223223
|> Keyword.get(:headers, [])
224+
|> Enum.map(fn {k, v} -> {String.downcase(k), v} end)
224225
|> put_new_header("user-agent", @user_agent)
225226
|> put_new_header("x-clickhouse-format", "RowBinaryWithNamesAndTypes")
226227

test/ch/query_test.exs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,63 @@ defmodule Ch.QueryTest do
492492
assert :proplists.get_keys(headers_continue) == :proplists.get_keys(headers_normal)
493493
end
494494

495+
test "mixed-case x-clickhouse-format overrides the default", %{
496+
conn: conn,
497+
query_options: query_options
498+
} do
499+
for header_name <- [
500+
"x-clickhouse-format",
501+
"X-ClickHouse-Format",
502+
"x-ClIcKhOuSe-FoRmAt"
503+
] do
504+
assert %Ch.Result{data: "1\n", headers: headers, names: nil, rows: nil} =
505+
Ch.query!(
506+
conn,
507+
"SELECT 1",
508+
%{},
509+
Keyword.merge(query_options, headers: [{header_name, "CSV"}])
510+
)
511+
512+
assert :proplists.get_value("x-clickhouse-format", headers) == "CSV"
513+
end
514+
end
515+
516+
test "mixed-case user-agent overrides the default", %{
517+
conn: conn,
518+
query_options: query_options
519+
} do
520+
assert %Ch.Result{rows: [["custom-agent/ABC"]]} =
521+
Ch.query!(
522+
conn,
523+
"SELECT getClientHTTPHeader('user-agent')",
524+
%{},
525+
Keyword.merge(query_options,
526+
headers: [{"User-Agent", "custom-agent/ABC"}],
527+
settings: [allow_get_client_http_header: 1]
528+
)
529+
)
530+
end
531+
532+
test "first x-clickhouse-format header wins regardless of case", %{
533+
conn: conn,
534+
query_options: query_options
535+
} do
536+
assert %Ch.Result{data: "1\n", headers: headers} =
537+
Ch.query!(
538+
conn,
539+
"SELECT 1",
540+
%{},
541+
Keyword.merge(query_options,
542+
headers: [
543+
{"X-ClickHouse-Format", "CSV"},
544+
{"x-clickhouse-format", "JSONEachRow"}
545+
]
546+
)
547+
)
548+
549+
assert :proplists.get_value("x-clickhouse-format", headers) == "CSV"
550+
end
551+
495552
test "connection works after failure in execute", %{conn: conn, query_options: query_options} do
496553
assert {:error, %Ch.Error{}} = Ch.query(conn, "wat", [], query_options)
497554
assert [[42]] = Ch.query!(conn, "SELECT 42", [], query_options).rows

0 commit comments

Comments
 (0)