11package clickhouse
22
33import (
4+ "context"
45 "net/http"
56 "testing"
67)
@@ -20,3 +21,81 @@ func TestCreateHTTPRoundTripper(t *testing.T) {
2021 t .Fatal ("TransportFn not called" )
2122 }
2223}
24+
25+ func TestApplyOptionsToRequest_HostHeader (t * testing.T ) {
26+ tests := []struct {
27+ name string
28+ headers map [string ]string
29+ expectedHost string
30+ expectedInMap map [string ]string
31+ unexpectedInMap []string
32+ }{
33+ {
34+ name : "Host header sets req.Host" ,
35+ headers : map [string ]string {"Host" : "my-service.example.com" },
36+ expectedHost : "my-service.example.com" ,
37+ unexpectedInMap : []string {"Host" },
38+ },
39+ {
40+ name : "lowercase host header sets req.Host" ,
41+ headers : map [string ]string {"host" : "my-service.example.com" },
42+ expectedHost : "my-service.example.com" ,
43+ unexpectedInMap : []string {"Host" },
44+ },
45+ {
46+ name : "uppercase HOST header sets req.Host" ,
47+ headers : map [string ]string {"HOST" : "my-service.example.com" },
48+ expectedHost : "my-service.example.com" ,
49+ unexpectedInMap : []string {"Host" },
50+ },
51+ {
52+ name : "Host header with other headers" ,
53+ headers : map [string ]string {
54+ "Host" : "my-service.example.com" ,
55+ "X-Custom-Token" : "abc123" ,
56+ },
57+ expectedHost : "my-service.example.com" ,
58+ expectedInMap : map [string ]string {"X-Custom-Token" : "abc123" },
59+ unexpectedInMap : []string {"Host" },
60+ },
61+ {
62+ name : "no Host header leaves req.Host unchanged" ,
63+ headers : map [string ]string {"X-Custom" : "value" },
64+ expectedHost : "localhost:8123" ,
65+ expectedInMap : map [string ]string {"X-Custom" : "value" },
66+ },
67+ }
68+
69+ for _ , tt := range tests {
70+ t .Run (tt .name , func (t * testing.T ) {
71+ req , err := http .NewRequest (http .MethodPost , "http://localhost:8123/" , nil )
72+ if err != nil {
73+ t .Fatalf ("failed to create request: %s" , err )
74+ }
75+
76+ opts := & Options {
77+ HttpHeaders : tt .headers ,
78+ }
79+
80+ if err := applyOptionsToRequest (context .Background (), req , opts ); err != nil {
81+ t .Fatalf ("applyOptionsToRequest failed: %s" , err )
82+ }
83+
84+ if req .Host != tt .expectedHost {
85+ t .Errorf ("req.Host = %q, want %q" , req .Host , tt .expectedHost )
86+ }
87+
88+ for k , v := range tt .expectedInMap {
89+ if got := req .Header .Get (k ); got != v {
90+ t .Errorf ("req.Header[%q] = %q, want %q" , k , got , v )
91+ }
92+ }
93+
94+ for _ , k := range tt .unexpectedInMap {
95+ if got := req .Header .Get (k ); got != "" {
96+ t .Errorf ("req.Header[%q] = %q, want it absent" , k , got )
97+ }
98+ }
99+ })
100+ }
101+ }
0 commit comments