Skip to content

Commit

Permalink
Support multiple headers, drop canonicalize names when not needed
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
  • Loading branch information
roidelapluie committed Dec 8, 2022
1 parent 59a1d6d commit bb90569
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type Headers struct {
// SetDirectory records the directory to make headers file relative to the
// configuration file.
func (h *Headers) SetDirectory(dir string) {
if h == nil {
return
}
h.dir = dir
}

Expand Down
30 changes: 30 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1680,3 +1680,33 @@ func TestHeaders(t *testing.T) {
t.Fatalf("can't fetch URL: %v", err)
}
}

func TestMultipleHeaders(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for k, v := range map[string][]string{
"One": {"value1a", "value1b", "value1c"},
"Two": {"value2a", "value2b", "value2c"},
"Three": {"value3a", "value3b", "value3c"},
} {
if !reflect.DeepEqual(r.Header.Values(k), v) {
t.Errorf("expected %v, got %v", v, r.Header.Values(k))
}
}
w.WriteHeader(http.StatusNoContent)
}))
t.Cleanup(ts.Close)

cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.headers-multiple.good.yaml")
if err != nil {
t.Fatalf("Error loading HTTP client config: %v", err)
}
client, err := NewClientFromConfig(*cfg, "test")
if err != nil {
t.Fatalf("Error creating HTTP Client: %v", err)
}

_, err = client.Get(ts.URL)
if err != nil {
t.Fatalf("can't fetch URL: %v", err)
}
}
3 changes: 3 additions & 0 deletions config/testdata/headers-file-a
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
value3a


1 change: 1 addition & 0 deletions config/testdata/headers-file-b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
value3b
1 change: 1 addition & 0 deletions config/testdata/headers-file-c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
value3c
7 changes: 7 additions & 0 deletions config/testdata/http.conf.headers-multiple.good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
http_headers:
headers:
one: [value1a, value1b, value1c]
secret_headers:
two: [value2a, value2b, value2c]
files:
three: [testdata/headers-file-a, testdata/headers-file-b, testdata/headers-file-c ]

0 comments on commit bb90569

Please sign in to comment.