Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTTPClientConfig JSON marshalling #651

Merged
merged 2 commits into from
Jun 7, 2024
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
10 changes: 8 additions & 2 deletions config/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package config

import (
"encoding/json"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -50,17 +51,22 @@ var reservedHeaders = map[string]struct{}{

// Headers represents the configuration for HTTP headers.
type Headers struct {
Headers map[string]Header `yaml:",inline" json:",inline"`
Headers map[string]Header `yaml:",inline"`
dir string
}

// Headers represents the configuration for HTTP headers.
// Header represents the configuration for a single HTTP header.
type Header struct {
Values []string `yaml:"values,omitempty" json:"values,omitempty"`
Secrets []Secret `yaml:"secrets,omitempty" json:"secrets,omitempty"`
Files []string `yaml:"files,omitempty" json:"files,omitempty"`
}

func (h Headers) MarshalJSON() ([]byte, error) {
// Inline the Headers map when serializing JSON because json encoder doesn't support "inline" directive.
return json.Marshal(h.Headers)
}

// SetDirectory records the directory to make headers file relative to the
// configuration file.
func (h *Headers) SetDirectory(dir string) {
Expand Down
86 changes: 86 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -2004,6 +2005,91 @@ func TestMarshalURLWithSecret(t *testing.T) {
}
}

func TestHTTPClientConfig_Marshal(t *testing.T) {
proxyURL, err := url.Parse("http://localhost:8080")
require.NoError(t, err)

t.Run("without HTTP headers", func(t *testing.T) {
config := &HTTPClientConfig{
ProxyConfig: ProxyConfig{
ProxyURL: URL{proxyURL},
},
}

t.Run("YAML", func(t *testing.T) {
actualYAML, err := yaml.Marshal(config)
require.NoError(t, err)
require.YAMLEq(t, `
proxy_url: "http://localhost:8080"
follow_redirects: false
enable_http2: false
http_headers: null
`, string(actualYAML))

// Unmarshalling the YAML should get the same struct in input.
actual := &HTTPClientConfig{}
require.NoError(t, yaml.Unmarshal(actualYAML, actual))
require.Equal(t, config, actual)
})

t.Run("JSON", func(t *testing.T) {
actualJSON, err := json.Marshal(config)

require.NoError(t, err)
require.JSONEq(t, `{
"proxy_url":"http://localhost:8080",
"tls_config":{"insecure_skip_verify":false},
"follow_redirects":false,
"enable_http2":false,
"http_headers":null
}`, string(actualJSON))

// Unmarshalling the JSON should get the same struct in input.
actual := &HTTPClientConfig{}
require.NoError(t, json.Unmarshal(actualJSON, actual))
require.Equal(t, config, actual)
})
})

t.Run("with HTTP headers", func(t *testing.T) {
config := &HTTPClientConfig{
ProxyConfig: ProxyConfig{
ProxyURL: URL{proxyURL},
},
HTTPHeaders: &Headers{
Headers: map[string]Header{
"X-Test": {
Values: []string{"Value-1", "Value-2"},
},
},
},
}

actualYAML, err := yaml.Marshal(config)
require.NoError(t, err)
require.YAMLEq(t, `
proxy_url: "http://localhost:8080"
follow_redirects: false
enable_http2: false
http_headers:
X-Test:
values:
- Value-1
- Value-2
`, string(actualYAML))

actualJSON, err := json.Marshal(config)
require.NoError(t, err)
require.JSONEq(t, `{
"proxy_url":"http://localhost:8080",
"tls_config":{"insecure_skip_verify":false},
"follow_redirects":false,
"enable_http2":false,
"http_headers":{"X-Test":{"values":["Value-1","Value-2"]}}
}`, string(actualJSON))
})
}

func TestOAuth2Proxy(t *testing.T) {
_, _, err := LoadHTTPConfigFile("testdata/http.conf.oauth2-proxy.good.yml")
if err != nil {
Expand Down