Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) {
if err != nil {
return nil, nil, err
}
cfg.SetDirectory(filepath.Dir(filepath.Dir(filename)))
cfg.SetDirectory(filepath.Dir(filename))
return cfg, content, nil
}

Expand Down
29 changes: 29 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2720,3 +2720,32 @@ func TestMultipleHeaders(t *testing.T) {
_, err = client.Get(ts.URL)
require.NoErrorf(t, err, "can't fetch URL: %v", err)
}

// TestLoadHTTPConfigFileResolvesPathsRelativeToConfigFile ensures that relative
// file paths inside a config loaded with LoadHTTPConfigFile are resolved
// relative to the directory containing the config file, as documented by the
// SetDirectory contract.
func TestLoadHTTPConfigFileResolvesPathsRelativeToConfigFile(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "header-value", r.Header.Get("X-Test"))
w.WriteHeader(http.StatusNoContent)
}))
t.Cleanup(ts.Close)

dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "header-value"), []byte("header-value\n"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "http.yml"), []byte(`http_headers:
X-Test:
files: [header-value]
`), 0o644))

cfg, _, err := LoadHTTPConfigFile(filepath.Join(dir, "http.yml"))
require.NoErrorf(t, err, "Error loading HTTP client config: %v", err)
require.Equal(t, filepath.Join(dir, "header-value"), cfg.HTTPHeaders.Headers["X-Test"].Files[0])

client, err := NewClientFromConfig(*cfg, "test")
require.NoErrorf(t, err, "Error creating HTTP Client: %v", err)

_, err = client.Get(ts.URL)
require.NoErrorf(t, err, "can't fetch URL: %v", err)
}
2 changes: 1 addition & 1 deletion config/testdata/http.conf.basic-auth.bad-username.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
basic_auth:
username: user
username_file: testdata/basic-auth-username
username_file: basic-auth-username
password: foo
2 changes: 1 addition & 1 deletion config/testdata/http.conf.basic-auth.good.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
basic_auth:
username: user
password_file: testdata/basic-auth-password
password_file: basic-auth-password
2 changes: 1 addition & 1 deletion config/testdata/http.conf.basic-auth.too-much.bad.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
basic_auth:
username: user
password: foo
password_file: testdata/basic-auth-password
password_file: basic-auth-password
4 changes: 2 additions & 2 deletions config/testdata/http.conf.basic-auth.username-file.good.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
basic_auth:
username_file: testdata/basic-auth-username
password_file: testdata/basic-auth-password
username_file: basic-auth-username
password_file: basic-auth-password
2 changes: 1 addition & 1 deletion config/testdata/http.conf.headers-multiple.good.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ http_headers:
values: [value2a]
secrets: [value2b, value2c]
three:
files: [testdata/headers-file-a, testdata/headers-file-b, testdata/headers-file-c]
files: [headers-file-a, headers-file-b, headers-file-c]
2 changes: 1 addition & 1 deletion config/testdata/http.conf.headers.good.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ http_headers:
two:
secrets: [value2]
three:
files: [testdata/headers-file]
files: [headers-file]
Loading