Skip to content

Commit 3183099

Browse files
authored
Merge pull request #651 from pracucci/show-http-header-issues
Fix HTTPClientConfig JSON marshalling
2 parents 92fc65e + d310c4a commit 3183099

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

config/headers.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package config
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"net/http"
2223
"os"
@@ -50,17 +51,22 @@ var reservedHeaders = map[string]struct{}{
5051

5152
// Headers represents the configuration for HTTP headers.
5253
type Headers struct {
53-
Headers map[string]Header `yaml:",inline" json:",inline"`
54+
Headers map[string]Header `yaml:",inline"`
5455
dir string
5556
}
5657

57-
// Headers represents the configuration for HTTP headers.
58+
// Header represents the configuration for a single HTTP header.
5859
type Header struct {
5960
Values []string `yaml:"values,omitempty" json:"values,omitempty"`
6061
Secrets []Secret `yaml:"secrets,omitempty" json:"secrets,omitempty"`
6162
Files []string `yaml:"files,omitempty" json:"files,omitempty"`
6263
}
6364

65+
func (h Headers) MarshalJSON() ([]byte, error) {
66+
// Inline the Headers map when serializing JSON because json encoder doesn't support "inline" directive.
67+
return json.Marshal(h.Headers)
68+
}
69+
6470
// SetDirectory records the directory to make headers file relative to the
6571
// configuration file.
6672
func (h *Headers) SetDirectory(dir string) {

config/http_config_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"testing"
3636
"time"
3737

38+
"github.com/stretchr/testify/require"
3839
"gopkg.in/yaml.v2"
3940
)
4041

@@ -2004,6 +2005,91 @@ func TestMarshalURLWithSecret(t *testing.T) {
20042005
}
20052006
}
20062007

2008+
func TestHTTPClientConfig_Marshal(t *testing.T) {
2009+
proxyURL, err := url.Parse("http://localhost:8080")
2010+
require.NoError(t, err)
2011+
2012+
t.Run("without HTTP headers", func(t *testing.T) {
2013+
config := &HTTPClientConfig{
2014+
ProxyConfig: ProxyConfig{
2015+
ProxyURL: URL{proxyURL},
2016+
},
2017+
}
2018+
2019+
t.Run("YAML", func(t *testing.T) {
2020+
actualYAML, err := yaml.Marshal(config)
2021+
require.NoError(t, err)
2022+
require.YAMLEq(t, `
2023+
proxy_url: "http://localhost:8080"
2024+
follow_redirects: false
2025+
enable_http2: false
2026+
http_headers: null
2027+
`, string(actualYAML))
2028+
2029+
// Unmarshalling the YAML should get the same struct in input.
2030+
actual := &HTTPClientConfig{}
2031+
require.NoError(t, yaml.Unmarshal(actualYAML, actual))
2032+
require.Equal(t, config, actual)
2033+
})
2034+
2035+
t.Run("JSON", func(t *testing.T) {
2036+
actualJSON, err := json.Marshal(config)
2037+
2038+
require.NoError(t, err)
2039+
require.JSONEq(t, `{
2040+
"proxy_url":"http://localhost:8080",
2041+
"tls_config":{"insecure_skip_verify":false},
2042+
"follow_redirects":false,
2043+
"enable_http2":false,
2044+
"http_headers":null
2045+
}`, string(actualJSON))
2046+
2047+
// Unmarshalling the JSON should get the same struct in input.
2048+
actual := &HTTPClientConfig{}
2049+
require.NoError(t, json.Unmarshal(actualJSON, actual))
2050+
require.Equal(t, config, actual)
2051+
})
2052+
})
2053+
2054+
t.Run("with HTTP headers", func(t *testing.T) {
2055+
config := &HTTPClientConfig{
2056+
ProxyConfig: ProxyConfig{
2057+
ProxyURL: URL{proxyURL},
2058+
},
2059+
HTTPHeaders: &Headers{
2060+
Headers: map[string]Header{
2061+
"X-Test": {
2062+
Values: []string{"Value-1", "Value-2"},
2063+
},
2064+
},
2065+
},
2066+
}
2067+
2068+
actualYAML, err := yaml.Marshal(config)
2069+
require.NoError(t, err)
2070+
require.YAMLEq(t, `
2071+
proxy_url: "http://localhost:8080"
2072+
follow_redirects: false
2073+
enable_http2: false
2074+
http_headers:
2075+
X-Test:
2076+
values:
2077+
- Value-1
2078+
- Value-2
2079+
`, string(actualYAML))
2080+
2081+
actualJSON, err := json.Marshal(config)
2082+
require.NoError(t, err)
2083+
require.JSONEq(t, `{
2084+
"proxy_url":"http://localhost:8080",
2085+
"tls_config":{"insecure_skip_verify":false},
2086+
"follow_redirects":false,
2087+
"enable_http2":false,
2088+
"http_headers":{"X-Test":{"values":["Value-1","Value-2"]}}
2089+
}`, string(actualJSON))
2090+
})
2091+
}
2092+
20072093
func TestOAuth2Proxy(t *testing.T) {
20082094
_, _, err := LoadHTTPConfigFile("testdata/http.conf.oauth2-proxy.good.yml")
20092095
if err != nil {

0 commit comments

Comments
 (0)