|
@@ -21,6 +21,11 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
|
|
req: `{ "keep_alive": 42 }`,
|
|
req: `{ "keep_alive": 42 }`,
|
|
exp: &Duration{42 * time.Second},
|
|
exp: &Duration{42 * time.Second},
|
|
},
|
|
},
|
|
|
|
+ {
|
|
|
|
+ name: "Positive Float",
|
|
|
|
+ req: `{ "keep_alive": 42.5 }`,
|
|
|
|
+ exp: &Duration{42 * time.Second},
|
|
|
|
+ },
|
|
{
|
|
{
|
|
name: "Positive Integer String",
|
|
name: "Positive Integer String",
|
|
req: `{ "keep_alive": "42m" }`,
|
|
req: `{ "keep_alive": "42m" }`,
|
|
@@ -31,6 +36,11 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
|
|
req: `{ "keep_alive": -1 }`,
|
|
req: `{ "keep_alive": -1 }`,
|
|
exp: &Duration{math.MaxInt64},
|
|
exp: &Duration{math.MaxInt64},
|
|
},
|
|
},
|
|
|
|
+ {
|
|
|
|
+ name: "Negative Float",
|
|
|
|
+ req: `{ "keep_alive": -3.14 }`,
|
|
|
|
+ exp: &Duration{math.MaxInt64},
|
|
|
|
+ },
|
|
{
|
|
{
|
|
name: "Negative Integer String",
|
|
name: "Negative Integer String",
|
|
req: `{ "keep_alive": "-1m" }`,
|
|
req: `{ "keep_alive": "-1m" }`,
|
|
@@ -48,3 +58,50 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func TestDurationMarshalUnmarshal(t *testing.T) {
|
|
|
|
+ tests := []struct {
|
|
|
|
+ name string
|
|
|
|
+ input time.Duration
|
|
|
|
+ expected time.Duration
|
|
|
|
+ }{
|
|
|
|
+ {
|
|
|
|
+ "negative duration",
|
|
|
|
+ time.Duration(-1),
|
|
|
|
+ time.Duration(math.MaxInt64),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "positive duration",
|
|
|
|
+ time.Duration(42 * time.Second),
|
|
|
|
+ time.Duration(42 * time.Second),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "another positive duration",
|
|
|
|
+ time.Duration(42 * time.Minute),
|
|
|
|
+ time.Duration(42 * time.Minute),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "zero duration",
|
|
|
|
+ time.Duration(0),
|
|
|
|
+ time.Duration(0),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "max duration",
|
|
|
|
+ time.Duration(math.MaxInt64),
|
|
|
|
+ time.Duration(math.MaxInt64),
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, test := range tests {
|
|
|
|
+ t.Run(test.name, func(t *testing.T) {
|
|
|
|
+ b, err := json.Marshal(Duration{test.input})
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+
|
|
|
|
+ var d Duration
|
|
|
|
+ err = json.Unmarshal(b, &d)
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+
|
|
|
|
+ assert.Equal(t, test.expected, d.Duration, "input %v, marshalled %v, got %v", test.input, string(b), d.Duration)
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+}
|