66 "time"
77
88 "github.com/stretchr/testify/assert"
9+ "github.com/stretchr/testify/require"
910 yaml "gopkg.in/yaml.v2"
1011)
1112
@@ -110,14 +111,13 @@ counters:
110111
111112}
112113
113- func TestUnmarshalOriginRequestConfig (t * testing.T ) {
114- raw := []byte (`
114+ var rawConfig = []byte (`
115115{
116- "connectTimeout": 10000000000 ,
117- "tlsTimeout": 30000000000 ,
118- "tcpKeepAlive": 30000000000 ,
116+ "connectTimeout": 10 ,
117+ "tlsTimeout": 30 ,
118+ "tcpKeepAlive": 30 ,
119119 "noHappyEyeballs": true,
120- "keepAliveTimeout": 60000000000 ,
120+ "keepAliveTimeout": 60 ,
121121 "keepAliveConnections": 10,
122122 "httpHostHeader": "app.tunnel.com",
123123 "originServerName": "app.tunnel.com",
@@ -142,13 +142,41 @@ func TestUnmarshalOriginRequestConfig(t *testing.T) {
142142 ]
143143}
144144` )
145+
146+ func TestMarshalUnmarshalOriginRequest (t * testing.T ) {
147+ testCases := []struct {
148+ name string
149+ marshalFunc func (in interface {}) (out []byte , err error )
150+ unMarshalFunc func (in []byte , out interface {}) (err error )
151+ baseUnit time.Duration
152+ }{
153+ {"json" , json .Marshal , json .Unmarshal , time .Second },
154+ {"yaml" , yaml .Marshal , yaml .Unmarshal , time .Nanosecond },
155+ }
156+
157+ for _ , tc := range testCases {
158+ t .Run (tc .name , func (t * testing.T ) {
159+ assertConfig (t , tc .marshalFunc , tc .unMarshalFunc , tc .baseUnit )
160+ })
161+ }
162+ }
163+
164+ func assertConfig (
165+ t * testing.T ,
166+ marshalFunc func (in interface {}) (out []byte , err error ),
167+ unMarshalFunc func (in []byte , out interface {}) (err error ),
168+ baseUnit time.Duration ,
169+ ) {
145170 var config OriginRequestConfig
146- assert .NoError (t , json .Unmarshal (raw , & config ))
147- assert .Equal (t , time .Second * 10 , * config .ConnectTimeout )
148- assert .Equal (t , time .Second * 30 , * config .TLSTimeout )
149- assert .Equal (t , time .Second * 30 , * config .TCPKeepAlive )
171+ var config2 OriginRequestConfig
172+
173+ assert .NoError (t , unMarshalFunc (rawConfig , & config ))
174+
175+ assert .Equal (t , baseUnit * 10 , config .ConnectTimeout .Duration )
176+ assert .Equal (t , baseUnit * 30 , config .TLSTimeout .Duration )
177+ assert .Equal (t , baseUnit * 30 , config .TCPKeepAlive .Duration )
150178 assert .Equal (t , true , * config .NoHappyEyeballs )
151- assert .Equal (t , time . Second * 60 , * config .KeepAliveTimeout )
179+ assert .Equal (t , baseUnit * 60 , config .KeepAliveTimeout . Duration )
152180 assert .Equal (t , 10 , * config .KeepAliveConnections )
153181 assert .Equal (t , "app.tunnel.com" , * config .HTTPHostHeader )
154182 assert .Equal (t , "app.tunnel.com" , * config .OriginServerName )
@@ -176,4 +204,12 @@ func TestUnmarshalOriginRequestConfig(t *testing.T) {
176204 },
177205 }
178206 assert .Equal (t , ipRules , config .IPRules )
207+
208+ // validate that serializing and deserializing again matches the deserialization from raw string
209+ result , err := marshalFunc (config )
210+ require .NoError (t , err )
211+ err = unMarshalFunc (result , & config2 )
212+ require .NoError (t , err )
213+
214+ require .Equal (t , config2 , config )
179215}
0 commit comments