@@ -35,6 +35,7 @@ import (
35
35
"testing"
36
36
"time"
37
37
38
+ "github.com/stretchr/testify/require"
38
39
"gopkg.in/yaml.v2"
39
40
)
40
41
@@ -2004,6 +2005,91 @@ func TestMarshalURLWithSecret(t *testing.T) {
2004
2005
}
2005
2006
}
2006
2007
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
+
2007
2093
func TestOAuth2Proxy (t * testing.T ) {
2008
2094
_ , _ , err := LoadHTTPConfigFile ("testdata/http.conf.oauth2-proxy.good.yml" )
2009
2095
if err != nil {
0 commit comments